source: node_modules/@reduxjs/toolkit/src/query/tests/defaultSerializeQueryArgs.test.ts@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import { defaultSerializeQueryArgs } from '@internal/query/defaultSerializeQueryArgs'
2
3const endpointDefinition: any = {}
4const endpointName = 'test'
5
6test('string arg', () => {
7 expect(
8 defaultSerializeQueryArgs({
9 endpointDefinition,
10 endpointName,
11 queryArgs: 'arg',
12 }),
13 ).toMatchInlineSnapshot(`"test("arg")"`)
14})
15
16test('number arg', () => {
17 expect(
18 defaultSerializeQueryArgs({
19 endpointDefinition,
20 endpointName,
21 queryArgs: 5,
22 }),
23 ).toMatchInlineSnapshot(`"test(5)"`)
24})
25
26test('bigint arg has non-default serialization (intead of throwing)', () => {
27 expect(
28 defaultSerializeQueryArgs({
29 endpointDefinition,
30 endpointName,
31 queryArgs: BigInt(10),
32 }),
33 ).toMatchInlineSnapshot(`"test({"$bigint":"10"})"`)
34})
35
36test('simple object arg is sorted', () => {
37 expect(
38 defaultSerializeQueryArgs({
39 endpointDefinition,
40 endpointName,
41 queryArgs: { name: 'arg', age: 5 },
42 }),
43 ).toMatchInlineSnapshot(`"test({"age":5,"name":"arg"})"`)
44})
45
46test('nested object arg is sorted recursively', () => {
47 expect(
48 defaultSerializeQueryArgs({
49 endpointDefinition,
50 endpointName,
51 queryArgs: { name: { last: 'Split', first: 'Banana' }, age: 5 },
52 }),
53 ).toMatchInlineSnapshot(
54 `"test({"age":5,"name":{"first":"Banana","last":"Split"}})"`,
55 )
56})
57
58test('Fully serializes a deeply nested object', () => {
59 const nestedObj = {
60 a: {
61 a1: {
62 a11: {
63 a111: 1,
64 },
65 },
66 },
67 b: {
68 b2: {
69 b21: 3,
70 },
71 b1: {
72 b11: 2,
73 },
74 },
75 }
76
77 const res = defaultSerializeQueryArgs({
78 endpointDefinition,
79 endpointName,
80 queryArgs: nestedObj,
81 })
82 expect(res).toMatchInlineSnapshot(
83 `"test({"a":{"a1":{"a11":{"a111":1}}},"b":{"b1":{"b11":2},"b2":{"b21":3}}})"`,
84 )
85})
86
87test('Caches results for plain objects', () => {
88 const testData = Array.from({ length: 10000 }).map((_, i) => {
89 return {
90 albumId: i,
91 id: i,
92 title: 'accusamus beatae ad facilis cum similique qui sunt',
93 url: 'https://via.placeholder.com/600/92c952',
94 thumbnailUrl: 'https://via.placeholder.com/150/92c952',
95 }
96 })
97
98 const data = {
99 testData,
100 }
101
102 const runWithTimer = (data: any) => {
103 const start = Date.now()
104 const res = defaultSerializeQueryArgs({
105 endpointDefinition,
106 endpointName,
107 queryArgs: data,
108 })
109 const end = Date.now()
110 const duration = end - start
111 return [res, duration] as const
112 }
113
114 const [res1, time1] = runWithTimer(data)
115 const [res2, time2] = runWithTimer(data)
116
117 expect(res1).toBe(res2)
118 expect(time2).toBeLessThanOrEqual(time1)
119 // Locally, stringifying 10K items takes 25-30ms.
120 // Assuming the WeakMap cache hit, this _should_ be 0
121 expect(time2).toBeLessThan(2)
122})
Note: See TracBrowser for help on using the repository browser.