-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrequest-spec.js
145 lines (136 loc) · 3.83 KB
/
request-spec.js
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
141
142
143
144
145
// @ts-check
import { gql } from '@apollo/client'
import { client } from '../../src/graphql-client'
describe('Make GraphQL requests', () => {
it('fetches all items', () => {
cy.request({
method: 'POST',
url: 'http://localhost:3000/',
body: {
operationName: 'allTodos',
query: `
query allTodos {
allTodos {
id,
title,
completed
}
}
`,
},
})
.its('body.data.allTodos')
// depends on the order of specs
// so let's be generous and not assume strict number
.should('have.length.gte', 0)
})
it('fetches all items using application client', () => {
// make a GraphQL query using the app's client
// https://www.apollographql.com/docs/react/data/queries/
const query = gql`
query allTodos {
allTodos {
id
title
completed
}
}
`
// use https://on.cypress.io/wrap to let the Cypress test
// wait for the promise returned by the "client.query" to resolve
cy.wrap(
client.query({
query,
// it is important to AVOID any caching here
// and fetch the current server data
fetchPolicy: 'no-cache',
}),
)
.its('data.allTodos')
.should('have.length.gte', 0)
})
it('creates one item', () => {
const random = Cypress._.random(1e5)
const title = `test ${random}`
cy.log(`Adding item ${title}`)
.then(() => {
const query = gql`
mutation AddTodo($title: String!) {
createTodo(title: $title, completed: false) {
id
}
}
`
// by returning the promise returned by the "client.query"
// call from the .then callback, we force the test to wait
// and yield the result to the next Cypress command or assertion
return client.query({
query,
variables: {
title,
},
// it is important to AVOID any caching here
// and fetch the current server data
fetchPolicy: 'no-cache',
})
})
// use zero timeout to avoid "cy.its" retrying
// since the response object is NOT going to change
.its('data.createTodo', { timeout: 0 })
.should('have.property', 'id')
// the item we have created should be shown in the list
cy.visit('/')
cy.contains('.todo', title)
})
it('creates an item and fetches it', () => {
cy.visit('/').wait(1000)
const random = Cypress._.random(1e5)
const title = `test ${random}`
cy.log(`Adding item ${title}`)
cy.request({
method: 'POST',
url: 'http://localhost:3000/',
body: {
operationName: 'AddTodo',
query: `
mutation AddTodo($title: String!) {
createTodo(title: $title, completed: false) {
id
}
}
`,
variables: {
title,
},
},
})
.its('body.data.createTodo')
.should('have.property', 'id')
.then((id) => {
// make a GraphQL query using the app's client
// https://www.apollographql.com/docs/react/data/queries/
const query = gql`
query getTodo($id: ID!) {
Todo(id: $id) {
id
title
completed
}
}
`
// use cy.wrap command to tell Cypress to wait for
// the query to resolve before proceeding to the next command
cy.wrap(client.query({ query, variables: { id } }))
// the query yields a Todo object
// that we can validate
.its('data.Todo')
.should('deep.include', {
id,
completed: false,
title,
})
})
cy.reload()
cy.contains('.todo', title)
})
})