Skip to content

Commit b662f70

Browse files
spaenlehswouf
authored andcommitted
fix: add missing accountId in mocks
1 parent 8ecc668 commit b662f70

File tree

3 files changed

+95
-101
lines changed

3 files changed

+95
-101
lines changed

cypress/support/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Cypress.Commands.add('setUpApi', (database, appContext) => {
2727
// eslint-disable-next-line no-param-reassign
2828
win.appContext = {
2929
memberId: CURRENT_MEMBER.id,
30+
accountId: CURRENT_MEMBER.id,
3031
itemId: MOCK_SERVER_ITEM.id,
3132
apiHost: Cypress.env('VITE_API_HOST'),
3233
...appContext,

public/mockServiceWorker.js

Lines changed: 93 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
/* tslint:disable */
33

44
/**
5-
* Mock Service Worker (1.3.3).
5+
* Mock Service Worker.
66
* @see https://github.com/mswjs/msw
77
* - Please do NOT modify this file.
88
* - Please do NOT serve this file on production.
99
*/
1010

11-
const INTEGRITY_CHECKSUM = '3d6b9f06410d179a7f7404d4bf4c3c70'
11+
const PACKAGE_VERSION = '2.6.5'
12+
const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074'
13+
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
1214
const activeClientIds = new Set()
1315

1416
self.addEventListener('install', function () {
@@ -47,7 +49,10 @@ self.addEventListener('message', async function (event) {
4749
case 'INTEGRITY_CHECK_REQUEST': {
4850
sendToClient(client, {
4951
type: 'INTEGRITY_CHECK_RESPONSE',
50-
payload: INTEGRITY_CHECKSUM,
52+
payload: {
53+
packageVersion: PACKAGE_VERSION,
54+
checksum: INTEGRITY_CHECKSUM,
55+
},
5156
})
5257
break
5358
}
@@ -57,7 +62,12 @@ self.addEventListener('message', async function (event) {
5762

5863
sendToClient(client, {
5964
type: 'MOCKING_ENABLED',
60-
payload: true,
65+
payload: {
66+
client: {
67+
id: client.id,
68+
frameType: client.frameType,
69+
},
70+
},
6171
})
6272
break
6373
}
@@ -86,12 +96,6 @@ self.addEventListener('message', async function (event) {
8696

8797
self.addEventListener('fetch', function (event) {
8898
const { request } = event
89-
const accept = request.headers.get('accept') || ''
90-
91-
// Bypass server-sent events.
92-
if (accept.includes('text/event-stream')) {
93-
return
94-
}
9599

96100
// Bypass navigation requests.
97101
if (request.mode === 'navigate') {
@@ -112,29 +116,8 @@ self.addEventListener('fetch', function (event) {
112116
}
113117

114118
// Generate unique request ID.
115-
const requestId = Math.random().toString(16).slice(2)
116-
117-
event.respondWith(
118-
handleRequest(event, requestId).catch((error) => {
119-
if (error.name === 'NetworkError') {
120-
console.warn(
121-
'[MSW] Successfully emulated a network error for the "%s %s" request.',
122-
request.method,
123-
request.url,
124-
)
125-
return
126-
}
127-
128-
// At this point, any exception indicates an issue with the original request/response.
129-
console.error(
130-
`\
131-
[MSW] Caught an exception from the "%s %s" request (%s). This is probably not a problem with Mock Service Worker. There is likely an additional logging output above.`,
132-
request.method,
133-
request.url,
134-
`${error.name}: ${error.message}`,
135-
)
136-
}),
137-
)
119+
const requestId = crypto.randomUUID()
120+
event.respondWith(handleRequest(event, requestId))
138121
})
139122

140123
async function handleRequest(event, requestId) {
@@ -146,21 +129,24 @@ async function handleRequest(event, requestId) {
146129
// this message will pend indefinitely.
147130
if (client && activeClientIds.has(client.id)) {
148131
;(async function () {
149-
const clonedResponse = response.clone()
150-
sendToClient(client, {
151-
type: 'RESPONSE',
152-
payload: {
153-
requestId,
154-
type: clonedResponse.type,
155-
ok: clonedResponse.ok,
156-
status: clonedResponse.status,
157-
statusText: clonedResponse.statusText,
158-
body:
159-
clonedResponse.body === null ? null : await clonedResponse.text(),
160-
headers: Object.fromEntries(clonedResponse.headers.entries()),
161-
redirected: clonedResponse.redirected,
132+
const responseClone = response.clone()
133+
134+
sendToClient(
135+
client,
136+
{
137+
type: 'RESPONSE',
138+
payload: {
139+
requestId,
140+
isMockedResponse: IS_MOCKED_RESPONSE in response,
141+
type: responseClone.type,
142+
status: responseClone.status,
143+
statusText: responseClone.statusText,
144+
body: responseClone.body,
145+
headers: Object.fromEntries(responseClone.headers.entries()),
146+
},
162147
},
163-
})
148+
[responseClone.body],
149+
)
164150
})()
165151
}
166152

@@ -174,6 +160,10 @@ async function handleRequest(event, requestId) {
174160
async function resolveMainClient(event) {
175161
const client = await self.clients.get(event.clientId)
176162

163+
if (activeClientIds.has(event.clientId)) {
164+
return client
165+
}
166+
177167
if (client?.frameType === 'top-level') {
178168
return client
179169
}
@@ -196,20 +186,22 @@ async function resolveMainClient(event) {
196186

197187
async function getResponse(event, client, requestId) {
198188
const { request } = event
199-
const clonedRequest = request.clone()
189+
190+
// Clone the request because it might've been already used
191+
// (i.e. its body has been read and sent to the client).
192+
const requestClone = request.clone()
200193

201194
function passthrough() {
202-
// Clone the request because it might've been already used
203-
// (i.e. its body has been read and sent to the client).
204-
const headers = Object.fromEntries(clonedRequest.headers.entries())
195+
// Cast the request headers to a new Headers instance
196+
// so the headers can be manipulated with.
197+
const headers = new Headers(requestClone.headers)
205198

206-
// Remove MSW-specific request headers so the bypassed requests
207-
// comply with the server's CORS preflight check.
208-
// Operate with the headers as an object because request "Headers"
209-
// are immutable.
210-
delete headers['x-msw-bypass']
199+
// Remove the "accept" header value that marked this request as passthrough.
200+
// This prevents request alteration and also keeps it compliant with the
201+
// user-defined CORS policies.
202+
headers.delete('accept', 'msw/passthrough')
211203

212-
return fetch(clonedRequest, { headers })
204+
return fetch(requestClone, { headers })
213205
}
214206

215207
// Bypass mocking when the client is not active.
@@ -225,57 +217,46 @@ async function getResponse(event, client, requestId) {
225217
return passthrough()
226218
}
227219

228-
// Bypass requests with the explicit bypass header.
229-
// Such requests can be issued by "ctx.fetch()".
230-
if (request.headers.get('x-msw-bypass') === 'true') {
231-
return passthrough()
232-
}
233-
234220
// Notify the client that a request has been intercepted.
235-
const clientMessage = await sendToClient(client, {
236-
type: 'REQUEST',
237-
payload: {
238-
id: requestId,
239-
url: request.url,
240-
method: request.method,
241-
headers: Object.fromEntries(request.headers.entries()),
242-
cache: request.cache,
243-
mode: request.mode,
244-
credentials: request.credentials,
245-
destination: request.destination,
246-
integrity: request.integrity,
247-
redirect: request.redirect,
248-
referrer: request.referrer,
249-
referrerPolicy: request.referrerPolicy,
250-
body: await request.text(),
251-
bodyUsed: request.bodyUsed,
252-
keepalive: request.keepalive,
221+
const requestBuffer = await request.arrayBuffer()
222+
const clientMessage = await sendToClient(
223+
client,
224+
{
225+
type: 'REQUEST',
226+
payload: {
227+
id: requestId,
228+
url: request.url,
229+
mode: request.mode,
230+
method: request.method,
231+
headers: Object.fromEntries(request.headers.entries()),
232+
cache: request.cache,
233+
credentials: request.credentials,
234+
destination: request.destination,
235+
integrity: request.integrity,
236+
redirect: request.redirect,
237+
referrer: request.referrer,
238+
referrerPolicy: request.referrerPolicy,
239+
body: requestBuffer,
240+
keepalive: request.keepalive,
241+
},
253242
},
254-
})
243+
[requestBuffer],
244+
)
255245

256246
switch (clientMessage.type) {
257247
case 'MOCK_RESPONSE': {
258248
return respondWithMock(clientMessage.data)
259249
}
260250

261-
case 'MOCK_NOT_FOUND': {
251+
case 'PASSTHROUGH': {
262252
return passthrough()
263253
}
264-
265-
case 'NETWORK_ERROR': {
266-
const { name, message } = clientMessage.data
267-
const networkError = new Error(message)
268-
networkError.name = name
269-
270-
// Rejecting a "respondWith" promise emulates a network error.
271-
throw networkError
272-
}
273254
}
274255

275256
return passthrough()
276257
}
277258

278-
function sendToClient(client, message) {
259+
function sendToClient(client, message, transferrables = []) {
279260
return new Promise((resolve, reject) => {
280261
const channel = new MessageChannel()
281262

@@ -287,17 +268,28 @@ function sendToClient(client, message) {
287268
resolve(event.data)
288269
}
289270

290-
client.postMessage(message, [channel.port2])
271+
client.postMessage(
272+
message,
273+
[channel.port2].concat(transferrables.filter(Boolean)),
274+
)
291275
})
292276
}
293277

294-
function sleep(timeMs) {
295-
return new Promise((resolve) => {
296-
setTimeout(resolve, timeMs)
278+
async function respondWithMock(response) {
279+
// Setting response status code to 0 is a no-op.
280+
// However, when responding with a "Response.error()", the produced Response
281+
// instance will have status code set to 0. Since it's not possible to create
282+
// a Response instance with status code 0, handle that use-case separately.
283+
if (response.status === 0) {
284+
return Response.error()
285+
}
286+
287+
const mockedResponse = new Response(response.body, response)
288+
289+
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
290+
value: true,
291+
enumerable: true,
297292
})
298-
}
299293

300-
async function respondWithMock(response) {
301-
await sleep(response.delay)
302-
return new Response(response.body, response)
294+
return mockedResponse
303295
}

src/mocks/db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const defaultMockContext: LocalContext = {
4343
context: Context.Builder,
4444
itemId: mockItem.id,
4545
memberId: mockMembers[0].id,
46+
accountId: mockMembers[0].id,
4647
};
4748

4849
const buildDatabase = (members?: CompleteMember[]): Database => ({

0 commit comments

Comments
 (0)