-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathquery.test.ts
More file actions
89 lines (80 loc) · 2.93 KB
/
query.test.ts
File metadata and controls
89 lines (80 loc) · 2.93 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
79
80
81
82
83
84
85
86
87
88
89
/*
* 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 configurationsEndpoint = '/organizations/'
// Not all endpoints use all parameters, but unused parameters are safely discarded
const OPTIONS: Argument<Queries[keyof Queries]> = {
parameters: {organizationId: 'f_ecom_zzrmy_orgf_001'}
}
// Mock data for configurations
const mockConfigurationsData = {
configurations: [
{
id: 'gcp',
value: 'test-gcp-api-key'
},
{
id: 'einstein',
value: 'test-einstein-api-key'
}
]
}
describe('Shopper Configurations query hooks', () => {
beforeEach(() => nock.cleanAll())
afterEach(() => {
expect(nock.pendingMocks()).toHaveLength(0)
})
test('`useConfigurations` has meta.displayName defined', async () => {
mockQueryEndpoint(configurationsEndpoint, mockConfigurationsData)
const queryClient = createQueryClient()
const {result} = renderHookWithProviders(
() => {
return queries.useConfigurations(OPTIONS)
},
{queryClient}
)
await waitAndExpectSuccess(() => result.current)
expect(queryClient.getQueryCache().getAll()[0].meta?.displayName).toBe('useConfigurations')
})
test('`useConfigurations` returns data on success', async () => {
mockQueryEndpoint(configurationsEndpoint, mockConfigurationsData)
const {result} = renderHookWithProviders(() => {
return queries.useConfigurations(OPTIONS)
})
await waitAndExpectSuccess(() => result.current)
expect(result.current.data).toEqual(mockConfigurationsData)
})
test('`useConfigurations` returns error on error', async () => {
mockQueryEndpoint(configurationsEndpoint, {}, 400)
const {result} = renderHookWithProviders(() => {
return queries.useConfigurations(OPTIONS)
})
await waitAndExpectError(() => result.current)
})
test('`useConfigurations` handles 500 server error', async () => {
mockQueryEndpoint(configurationsEndpoint, {}, 500)
const {result} = renderHookWithProviders(() => {
return queries.useConfigurations(OPTIONS)
})
await waitAndExpectError(() => result.current)
})
})