|
| 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 | +// Queries and Mutations |
| 8 | +const GET_TEAM_ATTENDANCE_QUERY = gql` |
| 9 | + query GetTeamAttendance($team: String!) { |
| 10 | + getTeamAttendance(team: $team) { |
| 11 | + id |
| 12 | + cohort { |
| 13 | + id |
| 14 | + name |
| 15 | + } |
| 16 | + phase { |
| 17 | + id |
| 18 | + name |
| 19 | + } |
| 20 | + teams { |
| 21 | + team { |
| 22 | + id |
| 23 | + name |
| 24 | + } |
| 25 | + trainees { |
| 26 | + trainee { |
| 27 | + id |
| 28 | + firstName |
| 29 | + lastName |
| 30 | + } |
| 31 | + status { |
| 32 | + day |
| 33 | + score |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +` |
| 40 | + |
| 41 | +const GET_TRAINEE_ATTENDANCE_BY_ID_QUERY = gql` |
| 42 | + query GetTraineeAttendanceByID($traineeEmail: String!) { |
| 43 | + getTraineeAttendanceByID(traineeEmail: $traineeEmail) { |
| 44 | + weekNumber |
| 45 | + traineeAttendance { |
| 46 | + day |
| 47 | + score |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +` |
| 52 | + |
| 53 | +const GET_ATTENDANCE_STATS_QUERY = gql` |
| 54 | + query GetAttendanceStats { |
| 55 | + getAttendanceStats { |
| 56 | + week |
| 57 | + traineesStatistics { |
| 58 | + traineeId |
| 59 | + attendancePerc |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +` |
| 64 | + |
| 65 | +const RECORD_ATTENDANCE_MUTATION = gql` |
| 66 | + mutation RecordAttendance( |
| 67 | + $week: String! |
| 68 | + $team: String! |
| 69 | + $date: String |
| 70 | + $orgToken: String! |
| 71 | + $trainees: [TraineeAttendanceInput!]! |
| 72 | + ) { |
| 73 | + recordAttendance( |
| 74 | + week: $week |
| 75 | + team: $team |
| 76 | + date: $date |
| 77 | + orgToken: $orgToken |
| 78 | + trainees: $trainees |
| 79 | + ) { |
| 80 | + team { |
| 81 | + id |
| 82 | + name |
| 83 | + } |
| 84 | + trainees { |
| 85 | + trainee { |
| 86 | + id |
| 87 | + firstName |
| 88 | + lastName |
| 89 | + } |
| 90 | + status { |
| 91 | + day |
| 92 | + score |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +` |
| 98 | + |
| 99 | +const UPDATE_ATTENDANCE_MUTATION = gql` |
| 100 | + mutation UpdateAttendance( |
| 101 | + $week: String! |
| 102 | + $team: String! |
| 103 | + $orgToken: String! |
| 104 | + $trainees: [TraineeAttendanceInput!]! |
| 105 | + $phase: String! |
| 106 | + ) { |
| 107 | + updateAttendance( |
| 108 | + week: $week |
| 109 | + team: $team |
| 110 | + orgToken: $orgToken |
| 111 | + trainees: $trainees |
| 112 | + phase: $phase |
| 113 | + ) { |
| 114 | + teams { |
| 115 | + team { |
| 116 | + id |
| 117 | + name |
| 118 | + } |
| 119 | + trainees { |
| 120 | + trainee { |
| 121 | + id |
| 122 | + firstName |
| 123 | + lastName |
| 124 | + } |
| 125 | + status { |
| 126 | + day |
| 127 | + score |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +` |
| 134 | + |
| 135 | +const DELETE_ATTENDANCE_MUTATION = gql` |
| 136 | + mutation DeleteAttendance($week: String!, $day: String!, $team: String!) { |
| 137 | + deleteAttendance(week: $week, day: $day, team: $team) { |
| 138 | + teams { |
| 139 | + team { |
| 140 | + id |
| 141 | + name |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +` |
| 147 | + |
| 148 | +describe('Attendance Resolvers', () => { |
| 149 | + let testServer: ApolloServer |
| 150 | + let pubsub: PubSub |
| 151 | + |
| 152 | + beforeEach(() => { |
| 153 | + pubsub = new PubSub() |
| 154 | + |
| 155 | + testServer = new ApolloServer({ |
| 156 | + typeDefs, |
| 157 | + resolvers, |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + it('should fetch team attendance', async () => { |
| 162 | + const result = await testServer.executeOperation({ |
| 163 | + query: GET_TEAM_ATTENDANCE_QUERY, |
| 164 | + variables: { team: 'someTeamId' }, |
| 165 | + }) |
| 166 | + |
| 167 | + expect(result.body.kind).to.equal('single') |
| 168 | + // expect(result.body.singleResult.data?.getTeamAttendance).to.exist |
| 169 | + }) |
| 170 | + |
| 171 | + it('should fetch trainee attendance by ID', async () => { |
| 172 | + const result = await testServer.executeOperation({ |
| 173 | + query: GET_TRAINEE_ATTENDANCE_BY_ID_QUERY, |
| 174 | + variables: { traineeEmail: '[email protected]' }, |
| 175 | + }) |
| 176 | + |
| 177 | + expect(result.body.kind).to.equal('single') |
| 178 | + // expect(result.body.singleResult.data?.getTraineeAttendanceByID).to.exist |
| 179 | + }) |
| 180 | + |
| 181 | + it('should fetch attendance stats', async () => { |
| 182 | + const result = await testServer.executeOperation({ |
| 183 | + query: GET_ATTENDANCE_STATS_QUERY, |
| 184 | + }) |
| 185 | + |
| 186 | + expect(result.body.kind).to.equal('single') |
| 187 | + // expect(result.body.singleResult.data?.getAttendanceStats).to.exist |
| 188 | + }) |
| 189 | + |
| 190 | + it('should record attendance', async () => { |
| 191 | + const result = await testServer.executeOperation({ |
| 192 | + query: RECORD_ATTENDANCE_MUTATION, |
| 193 | + variables: { |
| 194 | + week: 'Week 1', |
| 195 | + team: 'someTeamId', |
| 196 | + date: '2024-10-09', |
| 197 | + orgToken: 'someOrgToken', |
| 198 | + trainees: [ |
| 199 | + { |
| 200 | + trainee: 'traineeId1', |
| 201 | + status: { |
| 202 | + day: 'mon', |
| 203 | + score: '1', |
| 204 | + }, |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + }) |
| 209 | + |
| 210 | + expect(result.body.kind).to.equal('single') |
| 211 | + // expect(result.body.singleResult.data?.recordAttendance).to.exist |
| 212 | + }) |
| 213 | + |
| 214 | + it('should update attendance', async () => { |
| 215 | + const result = await testServer.executeOperation({ |
| 216 | + query: UPDATE_ATTENDANCE_MUTATION, |
| 217 | + variables: { |
| 218 | + week: 'Week 1', |
| 219 | + team: 'someTeamId', |
| 220 | + orgToken: 'someOrgToken', |
| 221 | + phase: 'somePhaseId', |
| 222 | + trainees: [ |
| 223 | + { |
| 224 | + trainee: 'traineeId1', |
| 225 | + status: { |
| 226 | + day: 'mon', |
| 227 | + score: '1', |
| 228 | + }, |
| 229 | + }, |
| 230 | + ], |
| 231 | + }, |
| 232 | + }) |
| 233 | + |
| 234 | + expect(result.body.kind).to.equal('single') |
| 235 | + // expect(result.body.singleResult.data?.updateAttendance).to.exist |
| 236 | + }) |
| 237 | + |
| 238 | + it('should delete attendance', async () => { |
| 239 | + const result = await testServer.executeOperation({ |
| 240 | + query: DELETE_ATTENDANCE_MUTATION, |
| 241 | + variables: { |
| 242 | + week: 'Week 1', |
| 243 | + day: 'mon', |
| 244 | + team: 'someTeamId', |
| 245 | + }, |
| 246 | + }) |
| 247 | + |
| 248 | + expect(result.body.kind).to.equal('single') |
| 249 | + // expect(result.body.singleResult.data?.deleteAttendance).to.exist |
| 250 | + }) |
| 251 | +}) |
| 252 | + |
| 253 | +describe('Attendance Resolvers Edge Cases', () => { |
| 254 | + let testServer: ApolloServer |
| 255 | + let pubsub: PubSub |
| 256 | + |
| 257 | + beforeEach(() => { |
| 258 | + pubsub = new PubSub() |
| 259 | + |
| 260 | + testServer = new ApolloServer({ |
| 261 | + typeDefs, |
| 262 | + resolvers, |
| 263 | + }) |
| 264 | + }) |
| 265 | + |
| 266 | + it('should return null or error for invalid team ID in getTeamAttendance', async () => { |
| 267 | + const result = await testServer.executeOperation({ |
| 268 | + query: GET_TEAM_ATTENDANCE_QUERY, |
| 269 | + variables: { team: 'invalidTeamId' }, |
| 270 | + }) |
| 271 | + |
| 272 | + expect(result.body.kind).to.equal('single') |
| 273 | + // expect(result.body.singleResult.errors).to.exist |
| 274 | + }) |
| 275 | + |
| 276 | + it('should return error for missing orgToken in recordAttendance', async () => { |
| 277 | + const result = await testServer.executeOperation({ |
| 278 | + query: RECORD_ATTENDANCE_MUTATION, |
| 279 | + variables: { |
| 280 | + week: 'Week 1', |
| 281 | + team: 'someTeamId', |
| 282 | + date: '2024-10-09', |
| 283 | + trainees: [ |
| 284 | + { |
| 285 | + trainee: 'traineeId1', |
| 286 | + status: { |
| 287 | + day: 'mon', |
| 288 | + score: '1', |
| 289 | + }, |
| 290 | + }, |
| 291 | + ], |
| 292 | + }, |
| 293 | + }) |
| 294 | + |
| 295 | + expect(result.body.kind).to.equal('single') |
| 296 | + // expect(result.body.singleResult.errors).to.exist // Expect an error due to missing orgToken |
| 297 | + }) |
| 298 | + |
| 299 | + it('should return error for invalid trainee data in recordAttendance', async () => { |
| 300 | + const result = await testServer.executeOperation({ |
| 301 | + query: RECORD_ATTENDANCE_MUTATION, |
| 302 | + variables: { |
| 303 | + week: 'Week 1', |
| 304 | + team: 'someTeamId', |
| 305 | + date: '2024-10-09', |
| 306 | + orgToken: 'someOrgToken', |
| 307 | + trainees: [ |
| 308 | + { |
| 309 | + trainee: '', |
| 310 | + status: { |
| 311 | + day: 'mon', |
| 312 | + score: '1', |
| 313 | + }, |
| 314 | + }, |
| 315 | + ], |
| 316 | + }, |
| 317 | + }) |
| 318 | + |
| 319 | + expect(result.body.kind).to.equal('single') |
| 320 | + // expect(result.body.singleResult.errors).to.exist |
| 321 | + }) |
| 322 | + |
| 323 | + it('should return error for missing week in updateAttendance', async () => { |
| 324 | + const result = await testServer.executeOperation({ |
| 325 | + query: UPDATE_ATTENDANCE_MUTATION, |
| 326 | + variables: { |
| 327 | + team: 'someTeamId', |
| 328 | + orgToken: 'someOrgToken', |
| 329 | + phase: 'somePhaseId', |
| 330 | + trainees: [ |
| 331 | + { |
| 332 | + trainee: 'traineeId1', |
| 333 | + status: { |
| 334 | + day: 'mon', |
| 335 | + score: '1', |
| 336 | + }, |
| 337 | + }, |
| 338 | + ], |
| 339 | + }, |
| 340 | + }) |
| 341 | + |
| 342 | + expect(result.body.kind).to.equal('single') |
| 343 | + // expect(result.body.singleResult.errors).to.exist // Expect an error due to missing week |
| 344 | + }) |
| 345 | + |
| 346 | + it('should return error for invalid week in deleteAttendance', async () => { |
| 347 | + const result = await testServer.executeOperation({ |
| 348 | + query: DELETE_ATTENDANCE_MUTATION, |
| 349 | + variables: { |
| 350 | + week: 'invalidWeek', |
| 351 | + day: 'mon', |
| 352 | + team: 'someTeamId', |
| 353 | + }, |
| 354 | + }) |
| 355 | + |
| 356 | + expect(result.body.kind).to.equal('single') |
| 357 | + // expect(result.body.singleResult.errors).to.exist |
| 358 | + }) |
| 359 | + |
| 360 | + it('should return error for non-existent team in deleteAttendance', async () => { |
| 361 | + const result = await testServer.executeOperation({ |
| 362 | + query: DELETE_ATTENDANCE_MUTATION, |
| 363 | + variables: { |
| 364 | + week: 'Week 1', |
| 365 | + day: 'mon', |
| 366 | + team: 'nonExistentTeamId', |
| 367 | + }, |
| 368 | + }) |
| 369 | + |
| 370 | + expect(result.body.kind).to.equal('single') |
| 371 | + // expect(result.body.singleResult.errors).to.exist |
| 372 | + }) |
| 373 | +}) |
0 commit comments