10000 is(): test primitives when dereferencing · powercoder23/immutable-js@1dcf3e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dcf3e0

Browse files
committed
is(): test primitives when dereferencing
This prevents `is()` from throwing. Repeating the tests instead of having to recurse and handling a recursion guard. Also fixing the `expectIs()` and `expectIsNot()` helpers that were not testing.
1 parent 6b60746 commit 1dcf3e0

File tree

4 files changed

+91
-30
lines changed

4 files changed

+91
-30
lines changed

__tests__/Equality.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ describe('Equality', () => {
1212

1313
function expectIs(left, right) {
1414
var comparison = Immutable.is(left, right);
15+
expect(comparison).toBe(true);
1516
var commutative = Immutable.is(right, left);
16-
return comparison && commutative && comparison === commutative;
17+
expect(commutative).toBe(true);
18+
expect(comparison === commutative).toBe(true);
1719
}
1820

1921
function expectIsNot(left, right) {
2022
var comparison = Immutable.is(left, right);
23+
expect(comparison).toBe(false);
2124
var commutative = Immutable.is(right, left);
22-
return !comparison && !commutative && comparison === commutative;
25+
expect(commutative).toBe(false);
26+
expect(comparison === commutative).toBe(true);
2327
}
2428

2529
it('uses Object.is semantics', () => {
@@ -36,7 +40,9 @@ describe('Equality', () => {
3640
expectIs(NaN, NaN);
3741
expectIs(0, 0);
3842
expectIs(-0, -0);
39-
expectIsNot(0, -0);
43+
// Note: Unlike Object.is, Immutable.is assumes 0 and -0 are the same value,
44+
// matching the behavior of ES6 Map key equality.
45+
expectIs(0, -0);
4046
expectIs(NaN, 0/0);
4147

4248
var string = "hello";
@@ -54,6 +60,35 @@ describe('Equality', () => {
5460
expectIsNot(object, {key:'value'});
5561
});
5662

63+
it('dereferences things', () => {
64+
var ptrA = {foo: 1}, ptrB = {foo: 2};
65+
expectIsNot(ptrA, ptrB);
66+
ptrA.valueOf = ptrB.valueOf = function() {
67+
return 5;
68+
}
69+
expectIs(ptrA, ptrB);
70+
var object = {key:'value'};
71+
ptrA.valueOf = ptrB.valueOf = function() {
72+
return object;
73+
}
74+
expectIs(ptrA, ptrB);
75+
ptrA.valueOf = ptrB.valueOf = function() {
76+
return null;
77+
}
78+
expectIs(ptrA, ptrB);
79+
ptrA.valueOf = ptrB.valueOf = function() {
80+
return void 0;
81+
}
82+
expectIs(ptrA, ptrB);
83+
ptrA.valueOf = function() {
84+
return 4;
85+
}
86+
ptrB.valueOf = function() {
87+
return 5;
< 8000 /td>
88+
}
89+
expectIsNot(ptrA, ptrB);
90+
});
91+
5792
it('compares sequences', () => {
5893
var arraySeq = Immutable.Seq.of(1,2,3);
5994
var arraySeq2 = Immutable.Seq([1,2,3]);

dist/immutable.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@
690690
typeof valueB.valueOf === 'function') {
691691
valueA = valueA.valueOf();
692692
valueB = valueB.valueOf();
693+
if (valueA === valueB || (valueA !== valueA && valueB !== valueB)) {
694+
return true;
695+
}
696+
if (!valueA || !valueB) {
697+
return false;
698+
}
693699
}
694700
return typeof valueA.equals === 'function' &&
695701
typeof valueB.equals === 'function' ?

0 commit comments

Comments
 (0)
0