Files
NapCatQQ/.pnpm-store/v3/files/0b/7ef18d6d72a664b4fcbc91ef150a8a770e84adf7852da1ed57e970d41f4ac066288dd0044f114bfc5ca2c8056a51cde1d59ea98a751d76a75d0835b272cd11
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

89 lines
2.4 KiB
Plaintext

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])
})