-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.test.js
417 lines (367 loc) · 11.4 KB
/
index.test.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const { AEMHeadless, ErrorCodes } = require('../../src')
// /////////////////////////////////////////////
const queryString = `
{
adventureList {
items {
_path
}
}
}
`
const persistedName = 'wknd/persist-query-name'
let sdk = {}
beforeEach(() => {
sdk = new AEMHeadless({
endpoint: 'endpoint/path.gql',
serviceURL: 'http://localhost',
auth: ['user', 'pass']
})
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: 'test'
})
})
})
test('API: persistQuery API Success', () => {
// check success response
const promise = sdk.persistQuery(queryString, `${persistedName}-${Date.now()}`)
return expect(promise).resolves.toBeTruthy()
})
test('API: invalid serviceURL', () => {
const serviceURL = 'invalid url path'
const config = {
serviceURL
}
sdk = new AEMHeadless(config)
const promise = sdk.runQuery(queryString)
return expect(promise).rejects.toThrow(`Invalid URL/path: ${serviceURL}`)
})
test('API: persistQuery API Error', () => {
fetch.mockRejectedValue({
ok: false
})
// check error response
const promise = sdk.persistQuery(queryString, persistedName)
return expect(promise).rejects.toThrow()
})
test('API: listPersistedQueries API Success', () => {
const promise = sdk.listPersistedQueries()
return expect(promise).resolves.toBeTruthy()
})
test('API: runQuery API Success', () => {
// check success response
const promise = sdk.runQuery(queryString)
return expect(promise).resolves.toBeTruthy()
})
test('API: runQuery with variables API Success', () => {
// check success response
const promise = sdk.runQuery({ query: queryString, variables: { name: 'Ado' } })
return expect(promise).resolves.toBeTruthy()
})
test('API: runQuery API Error', () => {
fetch.mockRejectedValue({
ok: false
})
// check error response
const promise = sdk.runQuery()
return expect(promise).rejects.toThrow()
})
test('API: runPersistedQuery API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName)
return expect(promise).resolves.toBeTruthy()
})
test('API: runPersistedQuery with variables API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName, { name: 'test' }, { method: 'POST' })
return expect(promise).resolves.toBeTruthy()
})
test('API: runPersistedQuery GET with variables API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName, { name: 'test' })
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}%3Bname%3Dtest`, expect.anything(), expect.anything())
return expect(promise).resolves.toBeTruthy()
})
test('API: runPersistedQuery API Error', () => {
fetch.mockRejectedValue({
ok: false
})
// check error response
const promise = sdk.runPersistedQuery('/test')
return expect(promise).rejects.toThrow()
})
// #181 2.2 Response error: JSON parsed - valid error defined in API response
test('API: custom error message', () => {
fetch.mockResolvedValue({
ok: false,
json: async () => ({
error: {
message: 'API custom error'
}
})
})
// check error response
const promise = sdk.runPersistedQuery('/test')
return expect(promise).rejects.toThrow(ErrorCodes.API_ERROR)
})
// #175 2.3 Response error: Couldn't parse JSON - no error defined in API response
test('API: Failed response JSON parsing error', () => {
const JsonError = Error('JSON parse error')
fetch.mockResolvedValue({
ok: false,
json: async () => {
throw JsonError
}
})
const promise = sdk.runPersistedQuery('/test')
return expect(promise).rejects.toThrow(ErrorCodes.RESPONSE_ERROR)
})
// #191 3.2. Response ok: Data error - Couldn't parse the JSON from OK response
test('API: Successful response JSON parsing error', () => {
const JsonError = Error('JSON parse error')
fetch.mockResolvedValue({
ok: true,
json: async () => {
throw JsonError
}
})
const promise = sdk.runPersistedQuery('/test')
return expect(promise).rejects.toThrow(ErrorCodes.RESPONSE_ERROR)
})
test('ERROR: Error helper', () => {
fetch.mockResolvedValue({
ok: false
})
const promise = sdk.runPersistedQuery('/test')
return promise.catch(e => {
// eslint-disable-next-line
return expect(e.toJSON()).toHaveProperty('message', e.message)
})
})
test('API: auth as a token', () => {
const config = {
serviceURL: 'http://localhost',
auth: 'token'
}
sdk = new AEMHeadless(config)
const promise = sdk.runQuery(queryString)
return expect(promise).resolves.toBeTruthy()
})
test('API: additional request options', () => {
const config = {}
sdk = new AEMHeadless(config)
const promise = sdk.runQuery(queryString, { method: 'GET' })
return expect(promise).resolves.toStrictEqual({ data: 'test' })
})
test('API: runQuery with custom headers API Success', () => {
const config = { headers: { 'global-header': 'global-value' } }
sdk = new AEMHeadless(config)
const promise = sdk.runQuery(queryString, { headers: { 'custom-header': 'custom-value' } })
return expect(promise).resolves.toBeTruthy()
})
test('API: params validation', () => {
fetch.mockResolvedValue({
ok: false,
json: async () => ({
errors: [{
message: 'API custom error'
}]
})
})
// check error response
const promise = sdk.runQuery('/test')
return expect(promise).rejects.toThrow(ErrorCodes.API_ERROR)
})
test('API: custom API error missing', () => {
fetch.mockResolvedValue({
ok: false,
json: async () => ({})
})
const promise = sdk.runPersistedQuery('/test')
return promise.catch(e => {
// eslint-disable-next-line
return expect(e.toJSON()).toHaveProperty('message', e.message)
})
})
test('API: multiple API custom errors', () => {
sdk = new AEMHeadless({
endpoint: {},
serviceURL: {}
})
// check error response
const promise = sdk.runPersistedQuery('/test')
return expect(promise).rejects.toThrow('Invalid URL/path:')
})
describe('runPaginatedQuery', () => {
const mockModel = 'mockModel'
const mockFields = 'mockFields'
const mockConfig = { pageSize: 10 }
const mockArgs = {}
const mockOptions = {}
const mockRetryOptions = {}
it('should throw an error if model is missing', async () => {
const gen = await sdk.runPaginatedQuery(null, mockFields, mockConfig, mockArgs, mockOptions, mockRetryOptions)
await expect(gen.next()).rejects.toThrow(ErrorCodes.INVALID_PARAM)
})
it('should throw an error if fields are missing', async () => {
const gen = await sdk.runPaginatedQuery(mockModel, null, mockConfig, mockArgs, mockOptions, mockRetryOptions)
await expect(gen.next()).rejects.toThrow(ErrorCodes.INVALID_PARAM)
})
it('should yield the filtered data', async () => {
const mockData = { id: '1', name: 'foo' }
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModelPaginated: {
edges: [
{ node: mockData }
],
pageInfo: {}
}
}
})
})
const gen = await sdk.runPaginatedQuery(mockModel, mockFields, mockConfig, mockArgs, mockOptions, mockRetryOptions)
const { value } = await gen.next()
expect(value.data).toEqual([mockData])
})
it('should yield done true and value false, at the last iteration', async () => {
const mockData = { id: '1', name: 'foo' }
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModelPaginated: {
edges: [
{ node: mockData }
],
pageInfo: {
hasNextPage: false
}
}
}
})
})
const gen = await sdk.runPaginatedQuery(mockModel, mockFields, mockConfig, mockArgs, mockOptions, mockRetryOptions)
const result = await gen.next()
expect(result.done).toBeFalsy()
expect(result.value.data).toEqual([mockData])
const { done, value } = await gen.next()
expect(done).toBeTruthy()
expect(value).toBeFalsy()
})
it('should yield only first item - cursor query', async () => {
const mockData = [
{ id: '1', name: 'foo' },
{ id: '2', name: 'bar' }
]
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModelPaginated: {
edges: [
{ node: mockData[0] }
],
pageInfo: {
hasNextPage: true
}
}
}
})
})
const gen = await sdk.runPaginatedQuery(mockModel, mockFields, mockConfig, { first: 1 }, mockOptions, mockRetryOptions)
const result = await gen.next()
expect(result).toEqual({ done: false, value: { data: [mockData[0]], hasNext: true } })
})
it('should yield only first item - offset query', async () => {
const mockData = [
{ id: '1', name: 'foo' },
{ id: '2', name: 'bar' }
]
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModelList: {
items: [
mockData[0]
]
}
}
})
})
const gen = await sdk.runPaginatedQuery('mockModel', mockFields, { useLimitOffset: true }, { limit: 1 }, mockOptions, mockRetryOptions)
const { value } = await gen.next()
expect(value.data).toEqual([mockData[0]])
})
it('should yield item - path query', async () => {
const mockData = { id: '1', name: 'foo' }
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModelByPath: {
item: mockData
}
}
})
})
const gen = await sdk.runPaginatedQuery(mockModel, mockFields, {}, { _path: 'path' }, mockOptions, mockRetryOptions)
const { value } = await gen.next()
expect(value.data).toEqual(mockData)
})
it('should update pagingArgs', async () => {
const mockData = { id: '1', name: 'foo' }
fetch.resetMocks()
fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
mockModel2Paginated: {
edges: [
{ node: mockData },
{ node: mockData }
],
pageInfo: {
hasNextPage: true
}
}
}
})
})
const gen = await sdk.runPaginatedQuery('mockModel2', mockFields, { pageSize: 1 }, { limit: 1 }, mockOptions, mockRetryOptions)
const result = await gen.next()
expect(result.done).toBeFalsy()
expect(result.value.data).toEqual([mockData, mockData])
const { done, value } = await gen.next()
expect(done).toBeFalsy()
expect(value.data).toEqual([mockData, mockData])
})
})