-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-setup.js
195 lines (181 loc) · 6.39 KB
/
jest-setup.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
/*
* Copyright (c) 2021, salesforce.com, 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
*/
/* eslint-env jest */
/* eslint-disable @typescript-eslint/no-var-requires */
// fetch polyfill can be removed when node 16 is no longer supported
require('cross-fetch/polyfill')
require('raf/polyfill') // fix requestAnimationFrame issue with polyfill
require('@testing-library/jest-dom/extend-expect')
const mockConfig = require('@salesforce/retail-react-app/config/mocks/default')
const {configure: configureTestingLibrary} = require('@testing-library/react')
const {Crypto} = require('@peculiar/webcrypto')
const {setupServer} = require('msw/node')
const {rest} = require('msw')
const {
mockCategory,
mockedRegisteredCustomer,
exampleTokenReponse,
mockCustomerBaskets
} = require('./app/mocks/mock-data')
// set jsdom in https context to allow read/write secure cookies
global.jsdom.reconfigure({url: 'https://www.domain.com'})
configureTestingLibrary({
// Increase to: 6 x default timeout of 1 second
...(process.env.CI ? {asyncUtilTimeout: 6000} : {})
})
/**
* Set up an API mocking server for testing purposes.
* This mock server includes the basic oauth flow endpoints.
*/
export const setupMockServer = () => {
return setupServer(
rest.post('*/oauth2/authorize', (req, res, ctx) => res(ctx.delay(0), ctx.status(200))),
rest.get('*/oauth2/authorize', (req, res, ctx) => res(ctx.delay(0), ctx.status(200))),
rest.post('*/oauth2/login', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
),
rest.get('*/oauth2/logout', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(exampleTokenReponse))
),
rest.get('*/customers/:customerId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
),
rest.get('*/customers/:customerId/baskets', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockCustomerBaskets))
),
rest.post('*/sessions', (req, res, ctx) => res(ctx.delay(0), ctx.status(200))),
rest.post('*/oauth2/token', (req, res, ctx) =>
res(
ctx.delay(0),
ctx.json({
customer_id: 'customerid',
// Is this token for guest or registered user?
access_token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoyNjczOTExMjYxLCJpYXQiOjI2NzM5MDk0NjF9.BDAp9G8nmArdBqAbsE5GUWZ3fiv2LwQKClEFDCGIyy8',
refresh_token: 'testrefeshtoken',
usid: 'testusid',
enc_user_id: 'testEncUserId',
id_token: 'testIdToken'
})
)
),
rest.get('*/categories/:categoryId', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(200), ctx.json(mockCategory))
),
rest.post('*/baskets/actions/merge', (req, res, ctx) => res(ctx.delay(0), ctx.status(200))),
rest.post('*/v3/activities/EinsteinTestSite/*', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
}),
rest.post('*/activities/EinsteinTestSite/*', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
}),
rest.post('*/v3/personalization/recs/EinsteinTestSite/*', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
})
)
}
beforeAll(() => {
global.server = setupMockServer()
global.server.listen({
onUnhandledRequest(req) {
console.error('Found an unhandled %s request to %s', req.method, req.url.href)
}
})
})
afterEach(() => {
global.server.resetHandlers()
})
afterAll(() => {
global.server.close()
})
// Mock the application configuration to be used in all tests.
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => {
return {
getConfig: () => mockConfig
}
})
// TextEncoder is a web API, need to import it
// from nodejs util in testing environment.
global.TextEncoder = require('util').TextEncoder
global.TextDecoder = require('util').TextDecoder
// This file consists of global mocks for jsdom.
class StorageMock {
constructor() {
this.store = {}
}
clear() {
this.store = {}
}
getItem(key) {
return this.store[key] || null
}
setItem(key, value) {
this.store[key] = value?.toString()
}
removeItem(key) {
delete this.store[key]
}
}
Object.defineProperty(window, 'crypto', {
value: new Crypto()
})
Object.defineProperty(window, 'localStorage', {
value: new StorageMock()
})
Object.defineProperty(window, 'sessionStorage', {
value: new StorageMock()
})
Object.defineProperty(window, 'scrollTo', {
value: () => null
})
if (typeof window.matchMedia !== 'function') {
Object.defineProperty(window, 'matchMedia', {
enumerable: true,
configurable: true,
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
})
}
const prepareHandlers = (handlerConfig = []) => {
return handlerConfig.map((config) => {
return rest[config.method?.toLowerCase() || 'get'](config.path, (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.status(config.status || 200),
config.res && ctx.json(config.res(req, res, ctx))
)
})
})
}
/**
* This util function allows developer to prepend handlers to the mock server by passing a config array of objects
*
* @param handlerConfig
* @example
* const handlers = [
* {
* path: "*\/products/"
* method: 'post',
* res: (req, res, ctx) => {
* return mockData
* }
* }
* ]
*/
export const prependHandlersToServer = (handlerConfig) => {
const handlers = prepareHandlers(handlerConfig)
global.server.use(...handlers)
}