Skip to content

Commit f929b4e

Browse files
committed
refactor: Prefer Vitest utilities over mock call extraction
Improve assertion explicitness.
1 parent 365291a commit f929b4e

File tree

1 file changed

+96
-59
lines changed

1 file changed

+96
-59
lines changed

src/utils/fetch-interceptor.test.js

Lines changed: 96 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,23 @@ describe( 'initializeFetchInterceptor', () => {
9595

9696
await waitForAsyncLogging();
9797

98-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
99-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
100-
101-
expect( loggedRequest.requestBody ).toContain( '[FormData with' );
102-
expect( loggedRequest.requestBody ).toContain(
103-
'file=<File: test.jpg'
98+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
99+
expect.objectContaining( {
100+
requestBody: expect.stringContaining( '[FormData with' ),
101+
} )
102+
);
103+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
104+
expect.objectContaining( {
105+
requestBody: expect.stringContaining(
106+
'file=<File: test.jpg'
107+
),
108+
} )
109+
);
110+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
111+
expect.objectContaining( {
112+
requestBody: expect.stringContaining( 'post=123' ),
113+
} )
104114
);
105-
expect( loggedRequest.requestBody ).toContain( 'post=123' );
106115
} );
107116

108117
it( 'should serialize Blob bodies correctly', async () => {
@@ -119,11 +128,12 @@ describe( 'initializeFetchInterceptor', () => {
119128

120129
await waitForAsyncLogging();
121130

122-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
123-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
124-
125-
expect( loggedRequest.requestBody ).toMatch(
126-
/\[Blob: \d+ bytes, type: image\/png\]/
131+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
132+
expect.objectContaining( {
133+
requestBody: expect.stringMatching(
134+
/\[Blob: \d+ bytes, type: image\/png\]/
135+
),
136+
} )
127137
);
128138
} );
129139

@@ -141,11 +151,12 @@ describe( 'initializeFetchInterceptor', () => {
141151

142152
await waitForAsyncLogging();
143153

144-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
145-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
146-
147-
expect( loggedRequest.requestBody ).toMatch(
148-
/\[File: document\.pdf, \d+ bytes, type: application\/pdf\]/
154+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
155+
expect.objectContaining( {
156+
requestBody: expect.stringMatching(
157+
/\[File: document\.pdf, \d+ bytes, type: application\/pdf\]/
158+
),
159+
} )
149160
);
150161
} );
151162

@@ -161,11 +172,10 @@ describe( 'initializeFetchInterceptor', () => {
161172

162173
await waitForAsyncLogging();
163174

164-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
165-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
166-
167-
expect( loggedRequest.requestBody ).toBe(
168-
'[ArrayBuffer: 1024 bytes]'
175+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
176+
expect.objectContaining( {
177+
requestBody: '[ArrayBuffer: 1024 bytes]',
178+
} )
169179
);
170180
} );
171181

@@ -183,11 +193,10 @@ describe( 'initializeFetchInterceptor', () => {
183193

184194
await waitForAsyncLogging();
185195

186-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
187-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
188-
189-
expect( loggedRequest.requestBody ).toBe(
190-
'key1=value1&key2=value2'
196+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
197+
expect.objectContaining( {
198+
requestBody: 'key1=value1&key2=value2',
199+
} )
191200
);
192201
} );
193202

@@ -203,10 +212,11 @@ describe( 'initializeFetchInterceptor', () => {
203212

204213
await waitForAsyncLogging();
205214

206-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
207-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
208-
209-
expect( loggedRequest.requestBody ).toBe( jsonString );
215+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
216+
expect.objectContaining( {
217+
requestBody: jsonString,
218+
} )
219+
);
210220
} );
211221

212222
it( 'should handle FormData with mixed content types', async () => {
@@ -238,23 +248,40 @@ describe( 'initializeFetchInterceptor', () => {
238248

239249
await waitForAsyncLogging();
240250

241-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
242-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
243-
244-
expect( loggedRequest.requestBody ).toContain(
245-
'[FormData with 4 field(s):'
251+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
252+
expect.objectContaining( {
253+
requestBody: expect.stringContaining(
254+
'[FormData with 4 field(s):'
255+
),
256+
} )
246257
);
247-
expect( loggedRequest.requestBody ).toContain(
248-
'text=simple text value'
258+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
259+
expect.objectContaining( {
260+
requestBody: expect.stringContaining(
261+
'text=simple text value'
262+
),
263+
} )
249264
);
250-
expect( loggedRequest.requestBody ).toContain(
251-
'file1=<File: image.png'
265+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
266+
expect.objectContaining( {
267+
requestBody: expect.stringContaining(
268+
'file1=<File: image.png'
269+
),
270+
} )
252271
);
253-
expect( loggedRequest.requestBody ).toContain(
254-
'file2=<File: doc.pdf'
272+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
273+
expect.objectContaining( {
274+
requestBody: expect.stringContaining(
275+
'file2=<File: doc.pdf'
276+
),
277+
} )
255278
);
256279
// Note: Blob is converted to File by FormData with name "blob"
257-
expect( loggedRequest.requestBody ).toContain( 'blob=<File: blob' );
280+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
281+
expect.objectContaining( {
282+
requestBody: expect.stringContaining( 'blob=<File: blob' ),
283+
} )
284+
);
258285
} );
259286

260287
it( 'should truncate long string values in FormData', async () => {
@@ -271,13 +298,22 @@ describe( 'initializeFetchInterceptor', () => {
271298

272299
await waitForAsyncLogging();
273300

274-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
275-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
276-
277-
expect( loggedRequest.requestBody ).toContain( 'longField=' );
278-
expect( loggedRequest.requestBody ).toContain( '...' );
279-
expect( loggedRequest.requestBody.length ).toBeLessThan(
280-
longString.length + 50
301+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
302+
expect.objectContaining( {
303+
requestBody: expect.stringContaining( 'longField=' ),
304+
} )
305+
);
306+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
307+
expect.objectContaining( {
308+
requestBody: expect.stringContaining( '...' ),
309+
} )
310+
);
311+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
312+
expect.objectContaining( {
313+
requestBody: expect.stringMatching(
314+
new RegExp( `.{1,${ longString.length + 50 }}` )
315+
),
316+
} )
281317
);
282318
} );
283319

@@ -298,11 +334,11 @@ describe( 'initializeFetchInterceptor', () => {
298334

299335
await waitForAsyncLogging();
300336

301-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
302-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
303-
304-
expect( loggedRequest.requestBody ).toBe(
305-
'[ReadableStream - cannot serialize without consuming]'
337+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
338+
expect.objectContaining( {
339+
requestBody:
340+
'[ReadableStream - cannot serialize without consuming]',
341+
} )
306342
);
307343
} );
308344

@@ -313,10 +349,11 @@ describe( 'initializeFetchInterceptor', () => {
313349

314350
await waitForAsyncLogging();
315351

316-
expect( bridge.onNetworkRequest ).toHaveBeenCalled();
317-
const loggedRequest = bridge.onNetworkRequest.mock.calls[ 0 ][ 0 ];
318-
319-
expect( loggedRequest.requestBody ).toBeNull();
352+
expect( bridge.onNetworkRequest ).toHaveBeenCalledWith(
353+
expect.objectContaining( {
354+
requestBody: null,
355+
} )
356+
);
320357
} );
321358
} );
322359
} );

0 commit comments

Comments
 (0)