Skip to content

Commit

Permalink
fix(expect-utils): Treat ImmutableJS Lists as Sets (#12763)
Browse files Browse the repository at this point in the history
  • Loading branch information
romellem authored May 2, 2022
1 parent e9cc8a8 commit dd690ca
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Fixes

- `[@jest/expect-utils]` Fix deep equality of ImmutableJS Lists ([#12763](https://github.com/facebook/jest/pull/12763))

### Chore & Maintenance

- `[@jest-reporters]` Move helper functions from `utils.ts` into separate files ([#12782](https://github.com/facebook/jest/pull/12782))
Expand Down
1 change: 1 addition & 0 deletions packages/expect-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest-get-type": "^28.0.2"
},
"devDependencies": {
"immutable": "^4.0.0",
"jest-matcher-utils": "^28.0.2"
},
"engines": {
Expand Down
8 changes: 8 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {List} from 'immutable';
import {stringify} from 'jest-matcher-utils';
import {
arrayBufferEquality,
Expand Down Expand Up @@ -517,6 +518,13 @@ describe('iterableEquality', () => {

expect(iterableEquality(a, b)).toBe(true);
});

test('returns true when given Immutable Lists without an OwnerID', () => {
const a = List([1, 2, 3]);
const b = a.filter(v => v > 0);

expect(iterableEquality(a, b)).toBe(true);
});
});

describe('arrayBufferEquality', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/expect-utils/src/jasmineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ function isDomNode(obj: any): boolean {
);
}

// SENTINEL constants are from https://github.com/facebook/immutable-js
// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';

export function isImmutableUnorderedKeyed(maybeKeyed: any) {
Expand All @@ -258,3 +259,10 @@ export function isImmutableUnorderedSet(maybeSet: any) {
!maybeSet[IS_ORDERED_SENTINEL]
);
}

export function isImmutableList(maybeList: any) {
return !!(
maybeList &&
maybeList[IS_LIST_SENTINEL]
);
}
11 changes: 7 additions & 4 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {isPrimitive} from 'jest-get-type';
import {
equals,
isA,
isImmutableList,
isImmutableUnorderedKeyed,
isImmutableUnorderedSet,
} from './jasmineUtils';
Expand Down Expand Up @@ -254,10 +255,12 @@ export const iterableEquality = (
return false;
}

const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (!equals(aEntries, bEntries)) {
return false;
if (!isImmutableList(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.
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@jest/expect-utils@workspace:packages/expect-utils"
dependencies:
immutable: ^4.0.0
jest-get-type: ^28.0.2
jest-matcher-utils: ^28.0.2
languageName: unknown
Expand Down

0 comments on commit dd690ca

Please sign in to comment.