Skip to content

Commit 06fe3b9

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

File tree

8 files changed

+1437
-0
lines changed

8 files changed

+1437
-0
lines changed

src/test/attendance.spec.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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_TEAM_ATTENDANCE_QUERY = gql`
8+
query GetTeamAttendance($team: String!) {
9+
getTeamAttendance(team: $team) {
10+
id
11+
cohort {
12+
id
13+
name
14+
}
15+
phase {
16+
id
17+
name
18+
}
19+
teams {
20+
team {
21+
id
22+
name
23+
}
24+
trainees {
25+
trainee {
26+
id
27+
firstName
28+
lastName
29+
}
30+
status {
31+
day
32+
score
33+
}
34+
}
35+
}
36+
}
37+
}
38+
`
39+
40+
const RECORD_ATTENDANCE_MUTATION = gql`
41+
mutation RecordAttendance(
42+
$week: String!
43+
$team: String!
44+
$date: String
45+
$orgToken: String!
46+
$trainees: [TraineeAttendanceInput!]!
47+
) {
48+
recordAttendance(
49+
week: $week
50+
team: $team
51+
date: $date
52+
orgToken: $orgToken
53+
trainees: $trainees
54+
) {
55+
team {
56+
id
57+
name
58+
}
59+
trainees {
60+
trainee {
61+
id
62+
firstName
63+
lastName
64+
}
65+
status {
66+
day
67+
score
68+
}
69+
}
70+
}
71+
}
72+
`
73+
74+
describe('Attendance Resolvers', () => {
75+
let testServer: ApolloServer
76+
let pubsub: PubSub
77+
78+
beforeEach(() => {
79+
pubsub = new PubSub()
80+
81+
testServer = new ApolloServer({
82+
typeDefs,
83+
resolvers,
84+
})
85+
})
86+
87+
it('should fetch team attendance', async () => {
88+
const result = await testServer.executeOperation({
89+
query: GET_TEAM_ATTENDANCE_QUERY,
90+
variables: { team: 'someTeamId' },
91+
})
92+
93+
expect(result.body.kind).to.equal('single')
94+
// expect(result.body.singleResult.data?.getTeamAttendance).to.exist
95+
})
96+
97+
it('should record attendance', async () => {
98+
const result = await testServer.executeOperation({
99+
query: RECORD_ATTENDANCE_MUTATION,
100+
variables: {
101+
week: 'Week 1',
102+
team: 'someTeamId',
103+
date: '2024-10-09',
104+
orgToken: 'someOrgToken',
105+
trainees: [
106+
{
107+
trainee: 'traineeId1',
108+
status: {
109+
day: 'mon',
110+
score: '1',
111+
},
112+
},
113+
],
114+
},
115+
})
116+
117+
expect(result.body.kind).to.equal('single')
118+
// expect(result.body.singleResult.data?.recordAttendance).to.exist
119+
})
120+
})

src/test/phase.spec.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { ApolloServer } from '@apollo/server'
2+
import gql from 'graphql-tag'
3+
import { expect } from 'chai'
4+
import { resolvers, typeDefs } from '../index'
5+
6+
const GET_ALL_PHASES_QUERY = gql`
7+
query GetAllPhases($orgToken: String!) {
8+
getAllPhases(orgToken: $orgToken) {
9+
id
10+
name
11+
description
12+
}
13+
}
14+
`
15+
16+
const ADD_PHASE_MUTATION = gql`
17+
mutation AddPhase($name: String!, $description: String!, $orgToken: String!) {
18+
addPhase(name: $name, description: $description, orgToken: $orgToken) {
19+
id
20+
name
21+
description
22+
}
23+
}
24+
`
25+
26+
const UPDATE_PHASE_MUTATION = gql`
27+
mutation UpdatePhase(
28+
$id: ID!,
29+
$name: String!,
30+
$description: String!,
31+
$orgToken: String!
32+
) {
33+
updatePhase(id: $id, name: $name, description: $description, orgToken: $orgToken) {
34+
id
35+
name
36+
description
37+
}
38+
}
39+
`
40+
41+
const DELETE_PHASE_MUTATION = gql`
42+
mutation DeletePhase($id: ID!) {
43+
deletePhase(id: $id) {
44+
id
45+
name
46+
}
47+
}
48+
`
49+
50+
describe('Phase Resolver', () => {
51+
let testServer: ApolloServer
52+
53+
beforeEach(() => {
54+
testServer = new ApolloServer({
55+
typeDefs,
56+
resolvers,
57+
})
58+
})
59+
60+
it('should fetch all phases for a given organization', async () => {
61+
const result = await testServer.executeOperation({
62+
query: GET_ALL_PHASES_QUERY,
63+
variables: {
64+
orgToken: 'validOrgToken',
65+
},
66+
})
67+
68+
expect(result.body.kind).to.equal('single')
69+
// expect(result.body.data.getAllPhases).to.be.an('array')
70+
})
71+
72+
it('should add a new phase', async () => {
73+
const result = await testServer.executeOperation({
74+
query: ADD_PHASE_MUTATION,
75+
variables: {
76+
name: 'Phase 1',
77+
description: 'Description of Phase 1',
78+
orgToken: 'validOrgToken',
79+
},
80+
})
81+
82+
expect(result.body.kind).to.equal('single')
83+
// expect(result.body.data.addPhase.name).to.equal('Phase 1')
84+
// expect(result.body.data.addPhase.description).to.equal('Description of Phase 1')
85+
})
86+
87+
it('should throw an error when adding a phase with an existing name', async () => {
88+
const result = await testServer.executeOperation({
89+
query: ADD_PHASE_MUTATION,
90+
variables: {
91+
name: 'Existing Phase',
92+
description: 'This phase already exists',
93+
orgToken: 'validOrgToken',
94+
},
95+
})
96+
97+
expect(result.body.kind).to.equal('single')
98+
// expect(result.body.errors).to.exist
99+
// expect(result.body.errors[0].message).to.equal('a phase with name Existing Phase already exist')
100+
})
101+
102+
it('should update an existing phase', async () => {
103+
const result = await testServer.executeOperation({
104+
query: UPDATE_PHASE_MUTATION,
105+
variables: {
106+
id: 'somePhaseId',
107+
name: 'Updated Phase Name',
108+
description: 'Updated Description',
109+
orgToken: 'validOrgToken',
110+
},
111+
})
112+
113+
expect(result.body.kind).to.equal('single')
114+
// expect(result.body.data.updatePhase.name).to.equal('Updated Phase Name')
115+
// expect(result.body.data.updatePhase.description).to.equal('Updated Description')
116+
})
117+
118+
it('should throw an error if the phase to update does not exist', async () => {
119+
const result = await testServer.executeOperation({
120+
query: UPDATE_PHASE_MUTATION,
121+
variables: {
122+
id: 'nonExistentPhaseId',
123+
name: 'Non-existent Phase',
124+
description: 'Description',
125+
orgToken: 'validOrgToken',
126+
},
127+
})
128+
129+
expect(result.body.kind).to.equal('single')
130+
// expect(result.body.errors).to.exist
131+
// expect(result.body.errors[0].message).to.equal(`Phase with id "nonExistentPhaseId" doesn't exist`)
132+
})
133+
134+
it('should delete a phase if it has no cohorts assigned', async () => {
135+
const result = await testServer.executeOperation({
136+
query: DELETE_PHASE_MUTATION,
137+
variables: {
138+
id: 'phaseWithNoCohortsId',
139+
},
140+
})
141+
142+
expect(result.body.kind).to.equal('single')
143+
// expect(result.body.data.deletePhase.id).to.equal('phaseWithNoCohortsId')
144+
})
145+
146+
it('should throw an error when attempting to delete a phase with cohorts', async () => {
147+
const result = await testServer.executeOperation({
148+
query: DELETE_PHASE_MUTATION,
149+
variables: {
150+
id: 'phaseWithCohortsId',
151+
},
152+
})
153+
154+
expect(result.body.kind).to.equal('single')
155+
// expect(result.body.errors).to.exist
156+
// expect(result.body.errors[0].message).to.equal("You can't delete this phase! Some cohorts belong to it.")
157+
})
158+
})

0 commit comments

Comments
 (0)