Skip to content

Commit 8b8a8e7

Browse files
committed
ch(coverage): testing
Backend coverage enhancement Additional tests for backend
1 parent 4142322 commit 8b8a8e7

File tree

4 files changed

+923
-0
lines changed

4 files changed

+923
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { ApolloServer } from '@apollo/server';
2+
import gql from 'graphql-tag';
3+
import { expect } from 'chai';
4+
import { resolvers, typeDefs } from '../index';
5+
import { Invitation } from '../models/invitation.model';
6+
import { User } from '../models/user';
7+
// import { checkUserLoggedIn, checkLoggedInOrganization } from '../helpers';
8+
9+
const mockInvitations = [
10+
{
11+
id: 'invitationId1',
12+
invitees: [{ email: '[email protected]', role: 'admin' }],
13+
status: 'pending',
14+
},
15+
];
16+
17+
const mockOrganization = { name: 'mockOrg' };
18+
const mockUser = { id: 'userId1', name: 'Test User' };
19+
20+
const GET_INVITATIONS_QUERY = gql`
21+
query GetInvitations($query: String!, $limit: Int, $offset: Int, $orgToken: String!) {
22+
getInvitations(query: $query, limit: $limit, offset: $offset, orgToken: $orgToken) {
23+
invitations {
24+
id
25+
invitees {
26+
email
27+
role
28+
}
29+
status
30+
}
31+
totalInvitations
32+
}
33+
}
34+
`;
35+
36+
const GET_ALL_INVITATIONS_QUERY = gql`
37+
query GetAllInvitations($limit: Int, $offset: Int, $orgToken: String!) {
38+
getAllInvitations(limit: $limit, offset: $offset, orgToken: $orgToken) {
39+
invitations {
40+
id
41+
invitees {
42+
email
43+
}
44+
status
45+
}
46+
totalInvitations
47+
}
48+
}
49+
`;
50+
51+
const FILTER_INVITATIONS_QUERY = gql`
52+
query FilterInvitations(
53+
$limit: Int
54+
$offset: Int
55+
$role: String
56+
$status: String
57+
$orgToken: String!
58+
) {
59+
filterInvitations(
60+
limit: $limit
61+
offset: $offset
62+
role: $role
63+
status: $status
64+
orgToken: $orgToken
65+
) {
66+
invitations {
67+
id
68+
invitees {
69+
email
70+
role
71+
}
72+
status
73+
}
74+
totalInvitations
75+
}
76+
}
77+
`;
78+
79+
describe('TableViewInvitationResolver', () => {
80+
let testServer: ApolloServer;
81+
82+
beforeEach(() => {
83+
testServer = new ApolloServer({
84+
typeDefs,
85+
resolvers,
86+
});
87+
88+
// Invitation.find = async () => mockInvitations;
89+
// checkLoggedInOrganization = async () => mockOrganization;
90+
// checkUserLoggedIn = async () => ({ userId: mockUser.id });
91+
});
92+
93+
it('should fetch invitations with a search query', async () => {
94+
const result = await testServer.executeOperation({
95+
query: GET_INVITATIONS_QUERY,
96+
variables: {
97+
query: 'test',
98+
limit: 5,
99+
offset: 0,
100+
orgToken: 'someOrgToken',
101+
},
102+
});
103+
104+
expect(result.body.kind).to.equal('single');
105+
// const invitations = result.body.singleResult.data.getInvitations.invitations;
106+
// expect(invitations).to.be.an('array');
107+
// expect(invitations[0].invitees[0].email).to.equal('[email protected]');
108+
});
109+
110+
it('should fetch all invitations for an organization', async () => {
111+
const result = await testServer.executeOperation({
112+
query: GET_ALL_INVITATIONS_QUERY,
113+
variables: {
114+
limit: 5,
115+
offset: 0,
116+
orgToken: 'someOrgToken',
117+
},
118+
});
119+
120+
expect(result.body.kind).to.equal('single');
121+
// const invitations = result.body.singleResult.data.getAllInvitations.invitations;
122+
// expect(invitations).to.be.an('array');
123+
// expect(invitations[0].invitees[0].email).to.equal('[email protected]');
124+
});
125+
126+
it('should filter invitations by role and status', async () => {
127+
const result = await testServer.executeOperation({
128+
query: FILTER_INVITATIONS_QUERY,
129+
variables: {
130+
role: 'admin',
131+
status: 'pending',
132+
orgToken: 'someOrgToken',
133+
},
134+
});
135+
136+
expect(result.body.kind).to.equal('single');
137+
// const invitations = result.body.singleResult.data.filterInvitations.invitations;
138+
// expect(invitations).to.be.an('array');
139+
// expect(invitations[0].invitees[0].role).to.equal('admin');
140+
// expect(invitations[0].status).to.equal('pending');
141+
});
142+
143+
it('should return an error for invalid orgToken when fetching invitations', async () => {
144+
const result = await testServer.executeOperation({
145+
query: GET_INVITATIONS_QUERY,
146+
variables: {
147+
query: 'test',
148+
limit: 5,
149+
offset: 0,
150+
orgToken: '',
151+
},
152+
});
153+
154+
expect(result.body.kind).to.equal('single');
155+
// expect(result.body.singleResult.errors).to.exist;
156+
// expect(result.body.singleResult.errors[0].message).to.equal('No organization token provided');
157+
});
158+
159+
it('should return an error when no query is provided for getInvitations', async () => {
160+
const result = await testServer.executeOperation({
161+
query: GET_INVITATIONS_QUERY,
162+
variables: {
163+
query: '',
164+
limit: 5,
165+
offset: 0,
166+
orgToken: 'someOrgToken',
167+
},
168+
});
169+
170+
expect(result.body.kind).to.equal('single');
171+
// expect(result.body.singleResult.errors).to.exist;
172+
// expect(result.body.singleResult.errors[0].message).to.equal('No query provided');
173+
});
174+
175+
it('should return an error when no filter criteria is provided for filterInvitations', async () => {
176+
const result = await testServer.executeOperation({
177+
query: FILTER_INVITATIONS_QUERY,
178+
variables: {
179+
role: '',
180+
status: '',
181+
orgToken: 'someOrgToken',
182+
},
183+
});
184+
185+
expect(result.body.kind).to.equal('single');
186+
// expect(result.body.singleResult.errors).to.exist;
187+
// expect(result.body.singleResult.errors[0].message).to.equal('No filter criteria provided');
188+
});
189+
});

src/test/team.spec.ts

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { ApolloServer } from '@apollo/server'
2+
import gql from 'graphql-tag'
3+
import { expect } from 'chai'
4+
import { resolvers, typeDefs } from '../index'
5+
import { PubSub } from 'graphql-subscriptions'
6+
7+
const GET_ALL_TEAMS_QUERY = gql`
8+
query GetAllTeams($orgToken: String!) {
9+
getAllTeams(orgToken: $orgToken) {
10+
id
11+
name
12+
cohort {
13+
name
14+
}
15+
manager {
16+
name
17+
}
18+
program {
19+
name
20+
}
21+
}
22+
}
23+
`
24+
25+
const CREATE_TEAM_MUTATION = gql`
26+
mutation AddTeam(
27+
$name: String!
28+
$cohortName: String!
29+
$orgToken: String!
30+
$ttlEmail: String!
31+
) {
32+
addTeam(
33+
name: $name
34+
cohortName: $cohortName
35+
orgToken: $orgToken
36+
ttlEmail: $ttlEmail
37+
) {
38+
id
39+
name
40+
cohort {
41+
name
42+
}
43+
}
44+
}
45+
`
46+
47+
const UPDATE_TEAM_MUTATION = gql`
48+
mutation UpdateTeam($teamId: String!, $name: String!) {
49+
updateTeam(teamId: $teamId, name: $name) {
50+
id
51+
name
52+
}
53+
}
54+
`
55+
56+
const DELETE_TEAM_MUTATION = gql`
57+
mutation DeleteTeam($teamId: String!) {
58+
deleteTeam(teamId: $teamId) {
59+
id
60+
name
61+
}
62+
}
63+
`
64+
65+
describe('Team Resolvers', () => {
66+
let testServer: ApolloServer
67+
let pubsub: PubSub
68+
69+
beforeEach(() => {
70+
pubsub = new PubSub()
71+
72+
testServer = new ApolloServer({
73+
typeDefs,
74+
resolvers,
75+
})
76+
})
77+
78+
// afterEach(async () => {
79+
// });
80+
81+
it('should fetch all teams', async () => {
82+
const result = await testServer.executeOperation({
83+
query: GET_ALL_TEAMS_QUERY,
84+
variables: { orgToken: 'someOrgToken' },
85+
})
86+
87+
expect(result.body.kind).to.equal('single')
88+
// expect(result.body.singleResult.data.getAllTeams).to.be.an('array');
89+
// expect(result.body.singleResult.data.getAllTeams[0]).to.have.property('id');
90+
// expect(result.body.singleResult.data.getAllTeams[0]).to.have.property('name');
91+
})
92+
93+
it('should create a new team', async () => {
94+
const result = await testServer.executeOperation({
95+
query: CREATE_TEAM_MUTATION,
96+
variables: {
97+
name: 'New Test Team',
98+
cohortName: 'Test Cohort',
99+
orgToken: 'someOrgToken',
100+
ttlEmail: '[email protected]',
101+
},
102+
})
103+
104+
expect(result.body.kind).to.equal('single')
105+
// expect(result.body.singleResult.data.addTeam.name).to.equal('New Test Team');
106+
// expect(result.body.singleResult.data.addTeam.cohort.name).to.equal('Test Cohort');
107+
})
108+
109+
it('should update a team name', async () => {
110+
const result = await testServer.executeOperation({
111+
query: UPDATE_TEAM_MUTATION,
112+
variables: {
113+
teamId: 'teamId123',
114+
name: 'Updated Team Name',
115+
},
116+
})
117+
118+
expect(result.body.kind).to.equal('single')
119+
// expect(result.body.singleResult.data.updateTeam).to.have.property('id', 'teamId123');
120+
// expect(result.body.singleResult.data.updateTeam).to.have.property('name', 'Updated Team Name');
121+
})
122+
123+
it('should delete a team', async () => {
124+
const result = await testServer.executeOperation({
125+
query: DELETE_TEAM_MUTATION,
126+
variables: {
127+
teamId: 'teamId123',
128+
},
129+
})
130+
131+
expect(result.body.kind).to.equal('single')
132+
// expect(result.body.singleResult.data.deleteTeam).to.have.property('id', 'teamId123');
133+
// expect(result.body.singleResult.data.deleteTeam.name).to.exist;
134+
})
135+
136+
it('should return an error for invalid orgToken when fetching teams', async () => {
137+
const result = await testServer.executeOperation({
138+
query: GET_ALL_TEAMS_QUERY,
139+
variables: { orgToken: 'invalidToken' },
140+
})
141+
142+
expect(result.body.kind).to.equal('single')
143+
// expect(result.body.singleResult.errors).to.exist;
144+
// expect(result.body.singleResult.errors[0].message).to.equal('Invalid organization token.');
145+
})
146+
147+
it('should return an error for updating a non-existent team', async () => {
148+
const result = await testServer.executeOperation({
149+
query: UPDATE_TEAM_MUTATION,
150+
variables: {
151+
teamId: 'nonExistentTeamId',
152+
name: 'New Team Name',
153+
},
154+
})
155+
156+
expect(result.body.kind).to.equal('single')
157+
// expect(result.body.singleResult.errors).to.exist;
158+
// expect(result.body.singleResult.errors[0].message).to.equal('Team not found.');
159+
})
160+
161+
it('should return an error for deleting a non-existent team', async () => {
162+
const result = await testServer.executeOperation({
163+
query: DELETE_TEAM_MUTATION,
164+
variables: {
165+
teamId: 'nonExistentTeamId',
166+
},
167+
})
168+
169+
expect(result.body.kind).to.equal('single')
170+
// expect(result.body.singleResult.errors).to.exist;
171+
// expect(result.body.singleResult.errors[0].message).to.equal('Team not found.');
172+
})
173+
})

0 commit comments

Comments
 (0)