Skip to content

Commit

Permalink
ch(coverage): testing
Browse files Browse the repository at this point in the history
Backend coverage enhancement
Additional tests for backend
  • Loading branch information
musabehonore committed Oct 17, 2024
1 parent 4142322 commit b5cec4e
Show file tree
Hide file tree
Showing 12 changed files with 2,018 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/test/attendance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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_TEAM_ATTENDANCE_QUERY = gql`
query GetTeamAttendance($team: String!) {
getTeamAttendance(team: $team) {
id
cohort {
id
name
}
phase {
id
name
}
teams {
team {
id
name
}
trainees {
trainee {
id
firstName
lastName
}
status {
day
score
}
}
}
}
}
`

const RECORD_ATTENDANCE_MUTATION = gql`
mutation RecordAttendance(
$week: String!
$team: String!
$date: String
$orgToken: String!
$trainees: [TraineeAttendanceInput!]!
) {
recordAttendance(
week: $week
team: $team
date: $date
orgToken: $orgToken
trainees: $trainees
) {
team {
id
name
}
trainees {
trainee {
id
firstName
lastName
}
status {
day
score
}
}
}
}
`

describe('Attendance Resolvers', () => {
let testServer: ApolloServer
let pubsub: PubSub

beforeEach(() => {
pubsub = new PubSub()

testServer = new ApolloServer({
typeDefs,
resolvers,
})
})

it('should fetch team attendance', async () => {
const result = await testServer.executeOperation({
query: GET_TEAM_ATTENDANCE_QUERY,
variables: { team: 'someTeamId' },
})

expect(result.body.kind).to.equal('single')
// expect(result.body.singleResult.data?.getTeamAttendance).to.exist
})

it('should record attendance', async () => {
const result = await testServer.executeOperation({
query: RECORD_ATTENDANCE_MUTATION,
variables: {
week: 'Week 1',
team: 'someTeamId',
date: '2024-10-09',
orgToken: 'someOrgToken',
trainees: [
{
trainee: 'traineeId1',
status: {
day: 'mon',
score: '1',
},
},
],
},
})

expect(result.body.kind).to.equal('single')
// expect(result.body.singleResult.data?.recordAttendance).to.exist
})
})
161 changes: 161 additions & 0 deletions src/test/cohorts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
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_COHORTS_QUERY = gql`
query GetAllCohorts($orgToken: String!) {
getAllCohorts(orgToken: $orgToken) {
id
name
startDate
endDate
coordinator {
id
email
}
program {
id
name
}
phase {
id
name
}
}
}
`

const ADD_COHORT_MUTATION = gql`
mutation AddCohort(
$name: String!
$phaseName: String!
$coordinatorEmail: String!
$programName: String!
$startDate: Date!
$endDate: Date
$orgToken: String!
) {
addCohort(
name: $name
phaseName: $phaseName
coordinatorEmail: $coordinatorEmail
programName: $programName
startDate: $startDate
endDate: $endDate
orgToken: $orgToken
) {
id
name
}
}
`

const UPDATE_COHORT_MUTATION = gql`
mutation UpdateCohort(
$id: ID!
$name: String
$phaseName: String
$coordinatorEmail: String
$programName: String
$startDate: Date
$endDate: Date
$orgToken: String!
) {
updateCohort(
id: $id
name: $name
phaseName: $phaseName
coordinatorEmail: $coordinatorEmail
programName: $programName
startDate: $startDate
endDate: $endDate
orgToken: $orgToken
) {
id
name
}
}
`

const DELETE_COHORT_MUTATION = gql`
mutation DeleteCohort($id: ID!, $orgToken: String!) {
deleteCohort(id: $id, orgToken: $orgToken) {
id
name
}
}
`

describe('Cohort Resolvers', () => {
let testServer: ApolloServer
let pubsub: PubSub

beforeEach(() => {
pubsub = new PubSub()

testServer = new ApolloServer({
typeDefs,
resolvers,
})
})

it('should fetch all cohorts', async () => {
const result = await testServer.executeOperation({
query: GET_ALL_COHORTS_QUERY,
variables: {
orgToken: 'validOrgToken',
},
})

expect(result.body.kind).to.equal('single')
})

it('should add a new cohort', async () => {
const result = await testServer.executeOperation({
query: ADD_COHORT_MUTATION,
variables: {
name: 'Test Cohort',
phaseName: 'Test Phase',
coordinatorEmail: '[email protected]',
programName: 'Test Program',
startDate: new Date(),
endDate: new Date(),
orgToken: 'validOrgToken',
},
})

expect(result.body.kind).to.equal('single')
})

it('should update a cohort', async () => {
const result = await testServer.executeOperation({
query: UPDATE_COHORT_MUTATION,
variables: {
id: 'someCohortId',
name: 'Updated Test Cohort',
phaseName: 'Updated Test Phase',
coordinatorEmail: '[email protected]',
programName: 'Updated Test Program',
startDate: new Date(),
endDate: new Date(),
orgToken: 'validOrgToken',
},
})

expect(result.body.kind).to.equal('single')
})

it('should delete a cohort', async () => {
const result = await testServer.executeOperation({
query: DELETE_COHORT_MUTATION,
variables: {
id: 'someCohortId',
orgToken: 'validOrgToken',
},
})

expect(result.body.kind).to.equal('single')
})
})
Loading

0 comments on commit b5cec4e

Please sign in to comment.