Skip to content

Commit

Permalink
fix(expect): fix immutable.js iterable equality (#5692)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored May 9, 2024
1 parent 73646b6 commit 1532c19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
47 changes: 43 additions & 4 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ export function hasProperty(obj: object | null, property: string): boolean {
// SENTINEL constants are from https://github.com/facebook/immutable-js
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@'
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@'
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@'
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@'
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@'

export function isImmutableUnorderedKeyed(maybeKeyed: any) {
return !!(
Expand All @@ -286,6 +288,36 @@ export function isImmutableUnorderedSet(maybeSet: any) {
)
}

function isObjectLiteral(source: unknown): source is Record<string, unknown> {
return source != null && typeof source === 'object' && !Array.isArray(source)
}

function isImmutableList(source: unknown): boolean {
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL])
}

function isImmutableOrderedKeyed(source: unknown): boolean {
return Boolean(
source
&& isObjectLiteral(source)
&& source[IS_KEYED_SENTINEL]
&& source[IS_ORDERED_SENTINEL],
)
}

function isImmutableOrderedSet(source: unknown): boolean {
return Boolean(
source
&& isObjectLiteral(source)
&& source[IS_SET_SENTINEL]
&& source[IS_ORDERED_SENTINEL],
)
}

function isImmutableRecord(source: unknown): boolean {
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL])
}

/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
Expand Down Expand Up @@ -411,10 +443,17 @@ export function iterableEquality(a: any, b: any, customTesters: Array<Tester> =
if (!bIterator.next().done)
return false

const aEntries = Object.entries(a)
const bEntries = Object.entries(b)
if (!equals(aEntries, bEntries))
return false
if (
!isImmutableList(a)
&& !isImmutableOrderedKeyed(a)
&& !isImmutableOrderedSet(a)
&& !isImmutableRecord(a)
) {
const aEntries = Object.entries(a)
const bEntries = Object.entries(b)
if (!equals(aEntries, bEntries))
return false
}

// Remove the first value from the stack of traversed values.
aStack.pop()
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@vueuse/integrations": "^10.9.0",
"axios": "^0.26.1",
"debug": "^4.3.4",
"immutable": "5.0.0-beta.5",
"strip-ansi": "^7.1.0",
"sweetalert2": "^11.6.16",
"tinyspy": "^1.0.2",
Expand Down
9 changes: 9 additions & 0 deletions test/core/test/immutable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import im from 'immutable'

test('basic', () => {
expect(im.List([{ x: 1 }])).toEqual(im.List([{ x: 1 }]))
expect(im.List([{ x: 1 }])).toEqual(im.List([1]).map(i => ({ x: i })))
expect(im.List([{ x: 1 }])).not.toEqual(im.List([{ x: 2 }]))
expect(im.List([{ x: 1 }])).not.toEqual(im.List([]))
})

0 comments on commit 1532c19

Please sign in to comment.