Skip to content

Commit 49a5ed7

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

File tree

7 files changed

+1317
-0
lines changed

7 files changed

+1317
-0
lines changed

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+
})

src/test/profile.spec.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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_PROFILE_QUERY = gql`
8+
query GetProfile {
9+
getProfile {
10+
id
11+
firstName
12+
lastName
13+
email
14+
}
15+
}
16+
`
17+
18+
const GET_ALL_USERS_QUERY = gql`
19+
query GetAllUsers($orgToken: String!) {
20+
getAllUsers(orgToken: $orgToken) {
21+
id
22+
firstName
23+
lastName
24+
role
25+
}
26+
}
27+
`
28+
29+
const UPLOAD_RESUME_MUTATION = gql`
30+
mutation UploadResume($userId: ID!, $resume: String!) {
31+
uploadResume(userId: $userId, resume: $resume) {
32+
id
33+
resume
34+
}
35+
}
36+
`
37+
38+
const UPDATE_PROFILE_MUTATION = gql`
39+
mutation UpdateProfile(
40+
$firstName: String!,
41+
$lastName: String!,
42+
$address: String!,
43+
$city: String!,
44+
$country: String!,
45+
$phoneNumber: String!,
46+
$biography: String!,
47+
$avatar: String!,
48+
$cover: String!,
49+
$githubUsername: String!
50+
) {
51+
updateProfile(
52+
firstName: $firstName,
53+
lastName: $lastName,
54+
address: $address,
55+
city: $city,
56+
country: $country,
57+
phoneNumber: $phoneNumber,
58+
biography: $biography,
59+
avatar: $avatar,
60+
cover: $cover,
61+
githubUsername: $githubUsername
62+
) {
63+
id
64+
firstName
65+
lastName
66+
}
67+
}
68+
`
69+
70+
const DROP_TTL_USER_MUTATION = gql`
71+
mutation DropTTLUser($email: String!, $reason: String!) {
72+
dropTTLUser(email: $email, reason: $reason)
73+
}
74+
`
75+
76+
describe('Profile Resolver', () => {
77+
let testServer: ApolloServer
78+
let pubsub: PubSub
79+
80+
beforeEach(() => {
81+
pubsub = new PubSub()
82+
83+
testServer = new ApolloServer({
84+
typeDefs,
85+
resolvers,
86+
})
87+
})
88+
89+
it('should fetch a user profile', async () => {
90+
const result = await testServer.executeOperation({
91+
query: GET_PROFILE_QUERY,
92+
})
93+
94+
expect(result.body.kind).to.equal('single')
95+
// expect(result.body.data.getProfile).to.be.an('object')
96+
// expect(result.body.data.getProfile.email).to.exist
97+
})
98+
99+
it('should fetch all users for a given organization', async () => {
100+
const result = await testServer.executeOperation({
101+
query: GET_ALL_USERS_QUERY,
102+
variables: {
103+
orgToken: 'validOrgToken',
104+
},
105+
})
106+
107+
expect(result.body.kind).to.equal('single')
108+
// expect(result.body.data.getAllUsers).to.be.an('array')
109+
})
110+
111+
it('should upload a resume for the user', async () => {
112+
const result = await testServer.executeOperation({
113+
query: UPLOAD_RESUME_MUTATION,
114+
variables: {
115+
userId: 'someUserId',
116+
resume: 'path/to/resume.pdf',
117+
},
118+
})
119+
120+
expect(result.body.kind).to.equal('single')
121+
// expect(result.body.data.uploadResume.resume).to.equal('path/to/resume.pdf')
122+
})
123+
124+
it('should update a user profile', async () => {
125+
const result = await testServer.executeOperation({
126+
query: UPDATE_PROFILE_MUTATION,
127+
variables: {
128+
firstName: 'John',
129+
lastName: 'Doe',
130+
address: '123 Main St',
131+
city: 'Metropolis',
132+
country: 'USA',
133+
phoneNumber: '1234567890',
134+
biography: 'Software Developer',
135+
avatar: 'avatar.png',
136+
cover: 'cover.jpg',
137+
githubUsername: 'johndoe',
138+
},
139+
})
140+
141+
expect(result.body.kind).to.equal('single')
142+
// expect(result.body.data.updateProfile.firstName).to.equal('John')
143+
})
144+
145+
it('should drop a TTL user', async () => {
146+
const result = await testServer.executeOperation({
147+
query: DROP_TTL_USER_MUTATION,
148+
variables: {
149+
150+
reason: 'Not needed',
151+
},
152+
})
153+
154+
expect(result.body.kind).to.equal('single')
155+
// expect(result.body.data.dropTTLUser).to.equal('TTL user with email [email protected] has been deleted. with Reason :Not needed')
156+
})
157+
})

src/test/ratings.spec.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_RATINGS_QUERY = gql`
8+
query {
9+
getRatings {
10+
id
11+
score
12+
feedback
13+
}
14+
}
15+
`
16+
17+
const CREATE_RATING_MUTATION = gql`
18+
mutation CreateRating($score: Int!, $feedback: String!) {
19+
createRating(score: $score, feedback: $feedback) {
20+
responseMsg
21+
}
22+
}
23+
`
24+
25+
const DELETE_RATING_MUTATION = gql`
26+
mutation DeleteRating($ratingId: ID!) {
27+
deleteRating(ratingId: $ratingId) {
28+
responseMsg
29+
}
30+
}
31+
`
32+
33+
describe('Ratings Resolver', () => {
34+
let testServer: ApolloServer
35+
let pubsub: PubSub
36+
37+
beforeEach(() => {
38+
pubsub = new PubSub()
39+
40+
testServer = new ApolloServer({
41+
typeDefs,
42+
resolvers,
43+
})
44+
})
45+
46+
it('should fetch all ratings', async () => {
47+
const result = await testServer.executeOperation({
48+
query: GET_RATINGS_QUERY,
49+
})
50+
51+
expect(result.body.kind).to.equal('single')
52+
// expect(result.body.data.getRatings).to.be.an('array')
53+
})
54+
55+
it('should create a new rating', async () => {
56+
const result = await testServer.executeOperation({
57+
query: CREATE_RATING_MUTATION,
58+
variables: {
59+
score: 5,
60+
feedback: 'Great service!',
61+
},
62+
})
63+
64+
expect(result.body.kind).to.equal('single')
65+
// expect(result.body.data.createRating.responseMsg).to.equal('Rating created successfully')
66+
})
67+
68+
it('should delete a rating', async () => {
69+
const result = await testServer.executeOperation({
70+
query: DELETE_RATING_MUTATION,
71+
variables: {
72+
ratingId: 'someRatingId',
73+
},
74+
})
75+
76+
expect(result.body.kind).to.equal('single')
77+
// expect(result.body.data.deleteRating.responseMsg).to.equal('Rating deleted successfully')
78+
})
79+
})

0 commit comments

Comments
 (0)