generated from ludorival/node-typescript-jest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.client.test.ts
105 lines (86 loc) · 3 KB
/
graphql.client.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { setupServer } from 'msw/node'
import { createTodo, fetchTodos, todoById } from '../../../test/graphql.client'
import { omitVersion } from '../../../test/utils'
import {
createTodoWillSucceed,
emptyTodos,
multipleTodos,
pact,
todoByIdFound,
todoByIdNotFound,
todosWillRaiseTechnicalFailure,
} from './handlers'
const server = setupServer()
beforeAll(() => {
pact.reset()
server.listen()
})
afterEach(() => {
server.resetHandlers()
})
afterAll(async () => {
server.close()
})
describe('To-Do list GraphQL API client', () => {
describe('fetchTodos', () => {
it('should fetch all To-Do items', async () => {
// use multipleTodos handlers from contracts
server.use(multipleTodos)
// call the fetchTodos function and get the actual data
const actualData = await fetchTodos()
// expect the actual data to match the expected data
expect(actualData).toMatchSnapshot()
})
it('should get a technical failure the first time and an empty todo list', async () => {
// use todosWillRaiseTechnicalFailure and emptyTodos handlers from contracts
server.use(todosWillRaiseTechnicalFailure)
// call first time fetchTodos should return an error
expect.assertions(2)
await fetchTodos().catch((e) =>
expect(e).toMatchObject({
message: 'Request failed with status code 500',
}),
)
server.resetHandlers(emptyTodos)
// call the fetchTodos function and get the actual data
const actualData = await fetchTodos()
// expect the actual data to match the expected data
expect(actualData).toEqual([])
})
})
describe('createTodo', () => {
it('should create a new To-Do item', async () => {
// use createTodoWillSucceed handlers from contracts
server.use(createTodoWillSucceed)
// call the createTodo function and get the actual data
const actualData = await createTodo('Buy groceries')
// expect the actual data to match the expected data
expect(actualData).toMatchSnapshot()
})
})
describe('todoById', () => {
it('should get a todo by its id', async () => {
// use todoByIdFound handlers from contracts
server.use(todoByIdFound)
// call the todoById function and get the actual data
const actualData = await todoById('1')
// expect the actual data to match the expected data
expect(actualData).toMatchSnapshot()
})
it('should get an error when getting a todo does not found it', async () => {
// use todoByIdFound handlers from contracts
server.use(todoByIdNotFound)
// call the todoById function and get the actual data
try {
await todoById('1')
fail('Should never reach')
} catch (e: unknown) {
expect(e).toMatchObject({ message: 'The todo item 1 is not found' })
}
})
})
})
it('the pact file can be generated and match with the snapshot', () => {
const pactFile = pact.generatePactFile()
expect(omitVersion(pactFile)).toMatchSnapshot()
})