-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathquery.test.ts
More file actions
78 lines (72 loc) · 2.96 KB
/
query.test.ts
File metadata and controls
78 lines (72 loc) · 2.96 KB
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
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import nock from 'nock'
import {
mockQueryEndpoint,
renderHookWithProviders,
waitAndExpectError,
waitAndExpectSuccess,
createQueryClient
} from '../../test-utils'
import {Argument} from '../types'
import * as queries from './query'
jest.mock('../../auth/index.ts', () => {
const {default: mockAuth} = jest.requireActual('../../auth/index.ts')
mockAuth.prototype.ready = jest.fn().mockResolvedValue({access_token: 'access_token'})
return mockAuth
})
type Queries = typeof queries
const basketsEndpoint = '/checkout/shopper-baskets/'
// Not all endpoints use all parameters, but unused parameters are safely discarded
const OPTIONS: Argument<Queries[keyof Queries]> = {
parameters: {basketId: 'basketId', shipmentId: 'shipmentId'}
}
/** Map of query name to returned data type */
type TestMap = {[K in keyof Queries]: NonNullable<ReturnType<Queries[K]>['data']>}
// This is an object rather than an array to more easily ensure we cover all hooks
const testMap: TestMap = {
useBasket: {basketId: 'basketId'},
usePaymentMethodsForBasket: {applicablePaymentMethods: []},
usePriceBooksForBasket: ['priceBookId'],
useShippingMethodsForShipment: {defaultShippingMethodId: 'defaultShippingMethodId'},
useTaxesFromBasket: {taxes: {}}
}
// Type assertion is necessary because `Object.entries` is limited
const testCases = Object.entries(testMap) as Array<[keyof TestMap, TestMap[keyof TestMap]]>
describe('Shopper Baskets V2 query hooks', () => {
beforeEach(() => nock.cleanAll())
afterEach(() => {
expect(nock.pendingMocks()).toHaveLength(0)
})
test.each(testCases)('`%s` has meta.displayName defined', async (queryName, data) => {
mockQueryEndpoint(basketsEndpoint, data)
const queryClient = createQueryClient()
const {result} = renderHookWithProviders(
() => {
return queries[queryName](OPTIONS)
},
{queryClient}
)
await waitAndExpectSuccess(() => result.current)
expect(queryClient.getQueryCache().getAll()[0].meta?.displayName).toBe(queryName)
})
test.each(testCases)('`%s` returns data on success', async (queryName, data) => {
mockQueryEndpoint(basketsEndpoint, data)
const {result} = renderHookWithProviders(() => {
return queries[queryName](OPTIONS)
})
await waitAndExpectSuccess(() => result.current)
expect(result.current.data).toEqual(data)
})
test.each(testCases)('`%s` returns error on error', async (queryName) => {
mockQueryEndpoint(basketsEndpoint, {}, 400)
const {result} = renderHookWithProviders(() => {
return queries[queryName](OPTIONS)
})
await waitAndExpectError(() => result.current)
})
})