-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtester.js
370 lines (324 loc) · 7.94 KB
/
tester.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */
'use strict'
const fs = require('fs')
const path = require('path')
const { expect } = require('chai')
const gql = require('graphql-tag')
const EasyGraphQLTester = require('../lib')
const userSchema = fs.readFileSync(
path.join(__dirname, 'schema', 'user.gql'),
'utf8'
)
const familySchema = fs.readFileSync(
path.join(__dirname, 'schema', 'family.gql'),
'utf8'
)
describe('Assert test', () => {
let tester
before(() => {
tester = new EasyGraphQLTester([userSchema, familySchema])
})
describe('Should pass if the query is invalid', () => {
it('Invalid query getUser', () => {
const invalidQuery = `
{
getUser {
id
invalidField
familyInfo {
father {
email
username
}
}
}
}
`
tester.test(false, invalidQuery)
})
it('Should pass if the query is valid', () => {
const validQuery = `
query GET_ME($result: Float!){
getMeByTestResult(result: $result) {
email
createdAt
}
}
`
tester.test(true, validQuery, {
result: 4.9,
})
})
it('Should not pass if the query arg is invalid', () => {
const validQuery = `
{
getMeByTestResult(invalidField: 4.9) {
email
}
}
`
tester.test(false, validQuery)
})
it('Should pass if the mutation is invalid', () => {
const mutation = `
mutation UpdateUserScores($scores: UpdateUserScoresInput!) {
updateUserScores(scores: $scores) {
email
scores
}
}
`
tester.test(false, mutation, {
scores: {
scores: ['1'],
},
})
})
it('Should pass if the mutation is valid', () => {
const mutation = `
mutation UpdateUserScores($scores: UpdateUserScoresInput!) {
updateUserScores(scores: $scores) {
email
scores
}
}
`
tester.test(true, mutation, {
scores: {
scores: [1],
},
})
})
it('Should not pass if one value on the mutation input is invalid', () => {
const mutation = `
mutation UpdateUserScores($scores: UpdateUserScoresInput!) {
updateUserScores(scores: $scores) {
email
scores
}
}
`
tester.test(false, mutation, {
scores: {
scores: [1],
invalidField: true,
},
})
})
it('Should pass if the input is invalid', () => {
const mutation = `
mutation CreateUser($input: UserInput!){
createUser(input: $input) {
email
}
}
`
tester.test(false, mutation, {
input: {
email: 'test@test.com',
fullName: 'test',
password: 'test',
},
})
})
it('Should return an error if the isValid field is different from boolean', () => {
let error
try {
const validQuery = `
{
getMeByTestResult(result: 4.9) {
email
}
}
`
tester.test('yes', validQuery)
} catch (err) {
error = err
}
if (!error) {
throw new Error('There should be an error')
}
expect(error).to.be.an.instanceOf(Error)
expect(error.message).to.be.eq('isValid argument must be a boolean')
})
it('Should return an error if the query is valid and the argument is false', () => {
let error
try {
const validQuery = `
{
getMeByTestResult(result: 4.9) {
email
}
}
`
tester.test(false, validQuery)
} catch (err) {
error = err
}
if (!error) {
throw new Error('There should be an error')
}
expect(error).to.be.an.instanceOf(Error)
expect(error.message).to.be.eq(
'Failed, there should be an error and the passed query/mutation is valid'
)
})
it('Should pass query with union', () => {
const query = `
{
search(id: "1") {
... on User {
id
}
... on FamilyInfo {
id
father {
username
}
brothers {
username
}
}
}
}
`
tester.test(true, query)
})
it('Should pass if it returns a string', () => {
const query = `
{
getString
}
`
tester.test(true, query)
})
it('Should pass if it returns a Int', () => {
const query = `
{
getInt
}
`
tester.test(true, query)
})
it('Should pass if it returns a Boolean', () => {
const mutation = `
mutation CreateBoolean($input: IsAdminInput!) {
createBoolean(input: $input)
}
`
tester.test(true, mutation, {
input: {
isAdmin: true,
},
})
})
it('Should receive scalar as argument', () => {
const mutation = `
mutation {
createTest(name: "Test", age: 20)
}
`
tester.test(true, mutation)
})
it('Should receive scalar arr as argument', () => {
const mutation = `
mutation {
createNames(names: ["Test", "Test 2"])
}
`
tester.test(true, mutation)
})
it('Should receive scalar arr', () => {
const mutation = gql`
mutation($username: String!) {
insert_user(objects: [{ username: $username }]) {
returning {
id
username
}
}
}
`
tester.test(true, mutation, {
username: 'Test',
})
})
it('Should fail if scalar expect scalar and get arr', () => {
let error
try {
const mutation = `
mutation {
createTest(name: ["Test"], age: 20)
}
`
tester.test(true, mutation)
} catch (err) {
error = err
}
expect(error).to.exist
expect(error.message).to.be.eq(
'Argument "name" has invalid value ["Test"].'
)
})
it('Should receive scalar boolean (false) argument', () => {
const mutation = `
mutation {
createTodo(completed: false)
}
`
tester.test(true, mutation)
})
it('Should fail if the mutation doesnt receive a boolean', () => {
const mutation = `
mutation {
createTodo(completed: "false")
}
`
tester.test(false, mutation)
})
it('Should receive mutation with graphql-tag', () => {
const mutation = gql`
mutation {
createTodo(completed: false)
}
`
tester.test(true, mutation)
})
it('Should test a subscription', () => {
const subscription = `
subscription {
newUsers(limit: 1) {
id
username
email
}
}
`
tester.test(true, subscription)
})
it('Should fail if a field on the variables is missing', () => {
let error
try {
const RESET_MUTATION = `
mutation RESET_MUTATION($demo: String!, $password: String!, $confirmPassword: String!) {
resetPassword(resetToken: $demo, password: $password, confirmPassword: $confirmPassword) {
id
email
}
}
`
tester.test(true, RESET_MUTATION, {
demo: 'jnzjkadnan',
confirmPassword: 'aa',
})
} catch (err) {
error = err
}
expect(error).to.exist
expect(error.message).to.be.eq(
'Variable "$password" of required type "String!" was not provided.'
)
})
})
})