Skip to content

Commit 1532c19

Browse files
authored
fix(expect): fix immutable.js iterable equality (#5692)
1 parent 73646b6 commit 1532c19

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

packages/expect/src/jest-utils.ts

+43-4
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ export function hasProperty(obj: object | null, property: string): boolean {
268268
// SENTINEL constants are from https://github.com/facebook/immutable-js
269269
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@'
270270
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@'
271+
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@'
271272
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@'
273+
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@'
272274

273275
export function isImmutableUnorderedKeyed(maybeKeyed: any) {
274276
return !!(
@@ -286,6 +288,36 @@ export function isImmutableUnorderedSet(maybeSet: any) {
286288
)
287289
}
288290

291+
function isObjectLiteral(source: unknown): source is Record<string, unknown> {
292+
return source != null && typeof source === 'object' && !Array.isArray(source)
293+
}
294+
295+
function isImmutableList(source: unknown): boolean {
296+
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL])
297+
}
298+
299+
function isImmutableOrderedKeyed(source: unknown): boolean {
300+
return Boolean(
301+
source
302+
&& isObjectLiteral(source)
303+
&& source[IS_KEYED_SENTINEL]
304+
&& source[IS_ORDERED_SENTINEL],
305+
)
306+
}
307+
308+
function isImmutableOrderedSet(source: unknown): boolean {
309+
return Boolean(
310+
source
311+
&& isObjectLiteral(source)
312+
&& source[IS_SET_SENTINEL]
313+
&& source[IS_ORDERED_SENTINEL],
314+
)
315+
}
316+
317+
function isImmutableRecord(source: unknown): boolean {
318+
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL])
319+
}
320+
289321
/**
290322
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
291323
*
@@ -411,10 +443,17 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> =
411443
if (!bIterator.next().done)
412444
return false
413445

414-
const aEntries = Object.entries(a)
415-
const bEntries = Object.entries(b)
416-
if (!equals(aEntries, bEntries))
417-
return false
446+
if (
447+
!isImmutableList(a)
448+
&& !isImmutableOrderedKeyed(a)
449+
&& !isImmutableOrderedSet(a)
450+
&& !isImmutableRecord(a)
451+
) {
452+
const aEntries = Object.entries(a)
453+
const bEntries = Object.entries(b)
454+
if (!equals(aEntries, bEntries))
455+
return false
456+
}
418457

419458
// Remove the first value from the stack of traversed values.
420459
aStack.pop()

pnpm-lock.yaml

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@vueuse/integrations": "^10.9.0",
2222
"axios": "^0.26.1",
2323
"debug": "^4.3.4",
24+
"immutable": "5.0.0-beta.5",
2425
"strip-ansi": "^7.1.0",
2526
"sweetalert2": "^11.6.16",
2627
"tinyspy": "^1.0.2",

test/core/test/immutable.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from 'vitest'
2+
import im from 'immutable'
3+
4+
test('basic', () => {
5+
expect(im.List([{ x: 1 }])).toEqual(im.List([{ x: 1 }]))
6+
expect(im.List([{ x: 1 }])).toEqual(im.List([1]).map(i => ({ x: i })))
7+
expect(im.List([{ x: 1 }])).not.toEqual(im.List([{ x: 2 }]))
8+
expect(im.List([{ x: 1 }])).not.toEqual(im.List([]))
9+
})

0 commit comments

Comments
 (0)