generated from ludorival/node-typescript-jest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.client.cy.tsx
140 lines (121 loc) · 4.21 KB
/
graphql.client.cy.tsx
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React from 'react'
import { mount } from '@cypress/react'
import * as props from '../../../test/graphql.client'
import CreateTodo from '../CreateTodo'
import TodoDetails from '../TodoDetails'
import TodoList from '../TodoList'
import {
createTodoWillSucceed,
emptyTodos,
multipleTodos,
pact,
todoByIdFound,
todoByIdNotFound,
todosWillRaiseTechnicalFailure,
} from './handlers'
import { omitVersion } from '../../../test/utils'
before(() => {
cy.reloadPact(pact)
})
beforeEach(() => {
pact.setCurrentSource(Cypress.currentTest.title)
})
after(() => {
cy.writePact(pact)
})
describe('To-Do list GraphQL API client', () => {
describe('fetchTodos', () => {
it('should fetch all To-Do items', () => {
// use multipleTodos handlers from contracts
cy.intercept('POST', `/graphql`, multipleTodos).as('multipleTodos')
// Mount the TodoList to fetchTodos function and get the actual data
mount(<TodoList {...props} />)
// expect the actual data to match the expected data
cy.wait('@multipleTodos')
.its('response')
.its('body')
.its('data.todos')
.should('have.length', 3)
})
it('should get a technical failure the first time and an empty todo list', () => {
Cypress.on('uncaught:exception', () => {
return false // ignore
})
// use todosWillRaiseTechnicalFailure and emptyTodos handlers from contracts
cy.intercept('POST', `/graphql`, todosWillRaiseTechnicalFailure).as(
'todosWillRaiseTechnicalFailure',
)
// Mount the TodoList to fetchTodos function and get the actual data
mount(<TodoList {...props} />)
cy.wait('@todosWillRaiseTechnicalFailure')
.its('response')
.its('statusCode')
.should('be.equal', 500)
cy.intercept('POST', `/graphql`, emptyTodos).as('emptyTodos')
// Reload the fetch
cy.get('#reload').click()
// expect the actual data to match the expected data
cy.wait('@emptyTodos')
.its('response.body.data.todos')
.should('have.length', 0)
})
})
describe('createTodo', () => {
it('should create a new To-Do item', () => {
// use createTodoWillSucceed handlers from contracts
cy.intercept('POST', `/graphql`, createTodoWillSucceed).as(
'createTodoWillSucceed',
)
// mount the CreateTodo and get the actual data
mount(<CreateTodo {...props} />)
cy.get('#title')
.type('Go to groceries')
.get('#description')
.type('- Banana, Apple')
.get('#submit')
.click()
// expect the actual data to match the status code
cy.wait('@createTodoWillSucceed')
.its('response')
.its('body.data')
.its('createTodo.id')
.should('be.equal', '1')
})
})
describe('todoById', () => {
it('should get a todo by its id', () => {
// use todoByIdFound handlers from contracts
cy.intercept('POST', `/graphql`, todoByIdFound).as('todoByIdFound')
// mount the TodoDetails and get the actual data
mount(<TodoDetails id="1" {...props} />)
// expect the actual data to match the expected status code
cy.wait('@todoByIdFound')
.its('response')
.its('body.data')
.its('todoById.id')
.should('be.equal', '1')
})
it('should get an error when getting a todo does not found it', () => {
Cypress.on('uncaught:exception', () => {
return false // ignore
})
// use todoByIdFound handlers from contracts
cy.intercept('POST', `/graphql`, todoByIdNotFound).as('todoByIdNotFound')
// mount the TodoDetails and get the actual data
mount(<TodoDetails id="1" {...props} />)
// expect the actual data to match the expected status code
cy.wait('@todoByIdNotFound')
.its('response')
.its('body.errors')
.should('deep.equal', [{ message: 'The todo item 1 is not found' }])
})
})
it('the generated pact file should match with the snapshot', () => {
const pactFile = pact.generatePactFile()
cy.fixture('test-consumer-graphql-provider.json').then((expectedPact) => {
expect(omitVersion(pactFile)).to.deep.equal(
omitVersion(expectedPact, false),
)
})
})
})