Skip to content

Commit 8c380c2

Browse files
author
Christophe Prakash
committed
Add test
1 parent 06b1866 commit 8c380c2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,47 @@ describe('fetchBaseQuery', () => {
431431

432432
expect(request.headers['content-type']).toBe('application/vnd.api+json')
433433
})
434+
435+
it('supports a custom jsonReplacer', async () => {
436+
const body = {
437+
items: new Set(["A", "B", "C"])
438+
}
439+
440+
let request: any
441+
;({ data: request } = await baseQuery(
442+
{
443+
url: '/echo',
444+
body,
445+
method: 'POST',
446+
},
447+
commonBaseQueryApi,
448+
{}
449+
))
450+
451+
expect(request.headers['content-type']).toBe('application/json')
452+
expect(request.body).toEqual({ items: {} }) // Set is not properly marshalled by default
453+
454+
// Use jsonReplacer
455+
const baseQueryWithReplacer = fetchBaseQuery({
456+
baseUrl,
457+
fetchFn: fetchFn as any,
458+
jsonReplacer: (key, value) => value instanceof Set ? [...value] : value
459+
})
460+
461+
;({ data: request } = await baseQueryWithReplacer(
462+
{
463+
url: '/echo',
464+
body,
465+
method: 'POST',
466+
},
467+
commonBaseQueryApi,
468+
{}
469+
))
470+
471+
expect(request.headers['content-type']).toBe('application/json')
472+
expect(request.body).toEqual({ items: ["A", "B", "C"] }) // Set is marshalled correctly by jsonReplacer
473+
474+
})
434475
})
435476

436477
describe('arg.params', () => {

0 commit comments

Comments
 (0)