-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend coverage enhancement Additional tests for backend
- Loading branch information
1 parent
4142322
commit 8b8a8e7
Showing
4 changed files
with
923 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { ApolloServer } from '@apollo/server'; | ||
import gql from 'graphql-tag'; | ||
import { expect } from 'chai'; | ||
import { resolvers, typeDefs } from '../index'; | ||
import { Invitation } from '../models/invitation.model'; | ||
import { User } from '../models/user'; | ||
// import { checkUserLoggedIn, checkLoggedInOrganization } from '../helpers'; | ||
|
||
const mockInvitations = [ | ||
{ | ||
id: 'invitationId1', | ||
invitees: [{ email: '[email protected]', role: 'admin' }], | ||
status: 'pending', | ||
}, | ||
]; | ||
|
||
const mockOrganization = { name: 'mockOrg' }; | ||
const mockUser = { id: 'userId1', name: 'Test User' }; | ||
|
||
const GET_INVITATIONS_QUERY = gql` | ||
query GetInvitations($query: String!, $limit: Int, $offset: Int, $orgToken: String!) { | ||
getInvitations(query: $query, limit: $limit, offset: $offset, orgToken: $orgToken) { | ||
invitations { | ||
id | ||
invitees { | ||
role | ||
} | ||
status | ||
} | ||
totalInvitations | ||
} | ||
} | ||
`; | ||
|
||
const GET_ALL_INVITATIONS_QUERY = gql` | ||
query GetAllInvitations($limit: Int, $offset: Int, $orgToken: String!) { | ||
getAllInvitations(limit: $limit, offset: $offset, orgToken: $orgToken) { | ||
invitations { | ||
id | ||
invitees { | ||
} | ||
status | ||
} | ||
totalInvitations | ||
} | ||
} | ||
`; | ||
|
||
const FILTER_INVITATIONS_QUERY = gql` | ||
query FilterInvitations( | ||
$limit: Int | ||
$offset: Int | ||
$role: String | ||
$status: String | ||
$orgToken: String! | ||
) { | ||
filterInvitations( | ||
limit: $limit | ||
offset: $offset | ||
role: $role | ||
status: $status | ||
orgToken: $orgToken | ||
) { | ||
invitations { | ||
id | ||
invitees { | ||
role | ||
} | ||
status | ||
} | ||
totalInvitations | ||
} | ||
} | ||
`; | ||
|
||
describe('TableViewInvitationResolver', () => { | ||
let testServer: ApolloServer; | ||
|
||
beforeEach(() => { | ||
testServer = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
|
||
// Invitation.find = async () => mockInvitations; | ||
// checkLoggedInOrganization = async () => mockOrganization; | ||
// checkUserLoggedIn = async () => ({ userId: mockUser.id }); | ||
}); | ||
|
||
it('should fetch invitations with a search query', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_INVITATIONS_QUERY, | ||
variables: { | ||
query: 'test', | ||
limit: 5, | ||
offset: 0, | ||
orgToken: 'someOrgToken', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// const invitations = result.body.singleResult.data.getInvitations.invitations; | ||
// expect(invitations).to.be.an('array'); | ||
// expect(invitations[0].invitees[0].email).to.equal('[email protected]'); | ||
}); | ||
|
||
it('should fetch all invitations for an organization', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_ALL_INVITATIONS_QUERY, | ||
variables: { | ||
limit: 5, | ||
offset: 0, | ||
orgToken: 'someOrgToken', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// const invitations = result.body.singleResult.data.getAllInvitations.invitations; | ||
// expect(invitations).to.be.an('array'); | ||
// expect(invitations[0].invitees[0].email).to.equal('[email protected]'); | ||
}); | ||
|
||
it('should filter invitations by role and status', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: FILTER_INVITATIONS_QUERY, | ||
variables: { | ||
role: 'admin', | ||
status: 'pending', | ||
orgToken: 'someOrgToken', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// const invitations = result.body.singleResult.data.filterInvitations.invitations; | ||
// expect(invitations).to.be.an('array'); | ||
// expect(invitations[0].invitees[0].role).to.equal('admin'); | ||
// expect(invitations[0].status).to.equal('pending'); | ||
}); | ||
|
||
it('should return an error for invalid orgToken when fetching invitations', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_INVITATIONS_QUERY, | ||
variables: { | ||
query: 'test', | ||
limit: 5, | ||
offset: 0, | ||
orgToken: '', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('No organization token provided'); | ||
}); | ||
|
||
it('should return an error when no query is provided for getInvitations', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_INVITATIONS_QUERY, | ||
variables: { | ||
query: '', | ||
limit: 5, | ||
offset: 0, | ||
orgToken: 'someOrgToken', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('No query provided'); | ||
}); | ||
|
||
it('should return an error when no filter criteria is provided for filterInvitations', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: FILTER_INVITATIONS_QUERY, | ||
variables: { | ||
role: '', | ||
status: '', | ||
orgToken: 'someOrgToken', | ||
}, | ||
}); | ||
|
||
expect(result.body.kind).to.equal('single'); | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('No filter criteria provided'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { ApolloServer } from '@apollo/server' | ||
import gql from 'graphql-tag' | ||
import { expect } from 'chai' | ||
import { resolvers, typeDefs } from '../index' | ||
import { PubSub } from 'graphql-subscriptions' | ||
|
||
const GET_ALL_TEAMS_QUERY = gql` | ||
query GetAllTeams($orgToken: String!) { | ||
getAllTeams(orgToken: $orgToken) { | ||
id | ||
name | ||
cohort { | ||
name | ||
} | ||
manager { | ||
name | ||
} | ||
program { | ||
name | ||
} | ||
} | ||
} | ||
` | ||
|
||
const CREATE_TEAM_MUTATION = gql` | ||
mutation AddTeam( | ||
$name: String! | ||
$cohortName: String! | ||
$orgToken: String! | ||
$ttlEmail: String! | ||
) { | ||
addTeam( | ||
name: $name | ||
cohortName: $cohortName | ||
orgToken: $orgToken | ||
ttlEmail: $ttlEmail | ||
) { | ||
id | ||
name | ||
cohort { | ||
name | ||
} | ||
} | ||
} | ||
` | ||
|
||
const UPDATE_TEAM_MUTATION = gql` | ||
mutation UpdateTeam($teamId: String!, $name: String!) { | ||
updateTeam(teamId: $teamId, name: $name) { | ||
id | ||
name | ||
} | ||
} | ||
` | ||
|
||
const DELETE_TEAM_MUTATION = gql` | ||
mutation DeleteTeam($teamId: String!) { | ||
deleteTeam(teamId: $teamId) { | ||
id | ||
name | ||
} | ||
} | ||
` | ||
|
||
describe('Team Resolvers', () => { | ||
let testServer: ApolloServer | ||
let pubsub: PubSub | ||
|
||
beforeEach(() => { | ||
pubsub = new PubSub() | ||
|
||
testServer = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
}) | ||
}) | ||
|
||
// afterEach(async () => { | ||
// }); | ||
|
||
it('should fetch all teams', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_ALL_TEAMS_QUERY, | ||
variables: { orgToken: 'someOrgToken' }, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.data.getAllTeams).to.be.an('array'); | ||
// expect(result.body.singleResult.data.getAllTeams[0]).to.have.property('id'); | ||
// expect(result.body.singleResult.data.getAllTeams[0]).to.have.property('name'); | ||
}) | ||
|
||
it('should create a new team', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: CREATE_TEAM_MUTATION, | ||
variables: { | ||
name: 'New Test Team', | ||
cohortName: 'Test Cohort', | ||
orgToken: 'someOrgToken', | ||
ttlEmail: '[email protected]', | ||
}, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.data.addTeam.name).to.equal('New Test Team'); | ||
// expect(result.body.singleResult.data.addTeam.cohort.name).to.equal('Test Cohort'); | ||
}) | ||
|
||
it('should update a team name', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: UPDATE_TEAM_MUTATION, | ||
variables: { | ||
teamId: 'teamId123', | ||
name: 'Updated Team Name', | ||
}, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.data.updateTeam).to.have.property('id', 'teamId123'); | ||
// expect(result.body.singleResult.data.updateTeam).to.have.property('name', 'Updated Team Name'); | ||
}) | ||
|
||
it('should delete a team', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: DELETE_TEAM_MUTATION, | ||
variables: { | ||
teamId: 'teamId123', | ||
}, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.data.deleteTeam).to.have.property('id', 'teamId123'); | ||
// expect(result.body.singleResult.data.deleteTeam.name).to.exist; | ||
}) | ||
|
||
it('should return an error for invalid orgToken when fetching teams', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: GET_ALL_TEAMS_QUERY, | ||
variables: { orgToken: 'invalidToken' }, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('Invalid organization token.'); | ||
}) | ||
|
||
it('should return an error for updating a non-existent team', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: UPDATE_TEAM_MUTATION, | ||
variables: { | ||
teamId: 'nonExistentTeamId', | ||
name: 'New Team Name', | ||
}, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('Team not found.'); | ||
}) | ||
|
||
it('should return an error for deleting a non-existent team', async () => { | ||
const result = await testServer.executeOperation({ | ||
query: DELETE_TEAM_MUTATION, | ||
variables: { | ||
teamId: 'nonExistentTeamId', | ||
}, | ||
}) | ||
|
||
expect(result.body.kind).to.equal('single') | ||
// expect(result.body.singleResult.errors).to.exist; | ||
// expect(result.body.singleResult.errors[0].message).to.equal('Team not found.'); | ||
}) | ||
}) |
Oops, something went wrong.