From 29f12a38f36fc3918aba7daa684b6e418bdf8a11 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Sat, 4 May 2024 12:03:27 +0200 Subject: [PATCH] fix(query-core): replaceEqualDeep correctly handles arrays that contain undefined --- packages/query-core/src/__tests__/utils.test.tsx | 14 ++++++++++++++ packages/query-core/src/utils.ts | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/query-core/src/__tests__/utils.test.tsx b/packages/query-core/src/__tests__/utils.test.tsx index fdecf0d992..e5ebffe3d2 100644 --- a/packages/query-core/src/__tests__/utils.test.tsx +++ b/packages/query-core/src/__tests__/utils.test.tsx @@ -376,6 +376,20 @@ describe('core/utils', () => { expect(current).toBe(next) }) + + it('should return the previous value when both values are an array of undefined', () => { + const current = [undefined] + const next = replaceEqualDeep(current, [undefined]) + + expect(next).toBe(current) + }) + + it('should return the previous value when both values are an array that contains undefined', () => { + const current = [{ foo: 1 }, undefined] + const next = replaceEqualDeep(current, [{ foo: 1 }, undefined]) + + expect(next).toBe(current) + }) }) describe('matchMutation', () => { diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index 251b55cba1..3fcf9d47a6 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -232,10 +232,9 @@ export function replaceEqualDeep(a: any, b: any): any { for (let i = 0; i < bSize; i++) { const key = array ? i : bItems[i] if ( - !array && + ((!array && aItems.includes(key)) || array) && a[key] === undefined && - b[key] === undefined && - aItems.includes(key) + b[key] === undefined ) { copy[key] = undefined equalItems++