Spaces:
Running
Running
File size: 2,385 Bytes
c2b7eb3 | 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 | import { copyWithStructuralSharing } from '@reduxjs/toolkit/query'
test('equal object from JSON Object', () => {
const json = JSON.stringify({
a: { b: { c: { d: 1, e: '2', f: true }, g: false }, h: null },
i: null,
})
const objA = JSON.parse(json)
const objB = JSON.parse(json)
expect(objA).toStrictEqual(objB)
expect(objA).not.toBe(objB)
const newCopy = copyWithStructuralSharing(objA, objB)
expect(newCopy).toBe(objA)
expect(newCopy).not.toBe(objB)
expect(newCopy).toStrictEqual(objB)
})
test('equal object from JSON Object', () => {
const json = JSON.stringify({
a: { b: { c: { d: 1, e: '2', f: true }, g: false }, h: null },
i: null,
})
const objA = JSON.parse(json)
const objB = JSON.parse(json)
objB.a.h = 4
expect(objA).not.toStrictEqual(objB)
expect(objA).not.toBe(objB)
expect(objA.a.b).toStrictEqual(objB.a.b)
expect(objA.a.b).not.toBe(objB.a.b)
const newCopy = copyWithStructuralSharing(objA, objB)
expect(newCopy).not.toBe(objA)
expect(newCopy).not.toStrictEqual(objA)
expect(newCopy).toStrictEqual(objB)
expect(newCopy.a.b).toBe(objA.a.b)
expect(newCopy.a.b).not.toBe(objB.a.b)
expect(newCopy.a.b).toStrictEqual(objB.a.b)
})
test('equal object from JSON Array', () => {
const json = JSON.stringify([
1,
'a',
{ 2: 'b' },
{ 3: { 4: 'c' }, d: null },
null,
5,
])
const objA = JSON.parse(json)
const objB = JSON.parse(json)
expect(objA).toStrictEqual(objB)
expect(objA).not.toBe(objB)
const newCopy = copyWithStructuralSharing(objA, objB)
expect(newCopy).toBe(objA)
expect(newCopy).not.toBe(objB)
expect(newCopy).toStrictEqual(objB)
})
test('equal object from JSON Array', () => {
const json = JSON.stringify([
1,
'a',
{ 2: 'b' },
{ 3: { 4: 'c' }, d: null },
null,
5,
])
const objA = JSON.parse(json)
const objB = JSON.parse(json)
objB[2][2] = 'x'
expect(objA).not.toStrictEqual(objB)
expect(objA).not.toBe(objB)
const newCopy = copyWithStructuralSharing(objA, objB)
expect(newCopy).not.toBe(objA)
expect(newCopy).not.toBe(objB)
expect(newCopy).toStrictEqual(objB)
expect(newCopy[3]).toBe(objA[3])
expect(newCopy[3]).not.toBe(objB[3])
expect(newCopy[3]).toStrictEqual(objB[3])
expect(newCopy[2]).not.toBe(objA[2])
expect(newCopy[2]).not.toBe(objB[2])
expect(newCopy[2]).toStrictEqual(objB[2])
})
|