8000 Replace custom equality tester for custom matcher · githubsheng/immutable-js@107af84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 107af84

Browse files
committed
Replace custom equality tester for custom matcher
Cursor test was having issues with using a custom equality tester that were sporadically failing to apply. This replaces with a slightly more explicit custom matcher, which is probably preferable anyhow as it more explicitly declares what is being tested. Finally, this replaces the `lastCalledWith` matcher with more explicit matchers for each argument. This lets us apply different assertions to each argument.
1 parent 95335c3 commit 107af84

File tree

3 files changed

+94
-91
lines changed
  • contrib/cursor
  • resources
  • 3 files changed

    +94
    -91
    lines changed

    contrib/cursor/__tests__/Cursor.ts

    Lines changed: 92 additions & 90 deletions
    Original file line numberDiff line numberDiff line change
    @@ -7,14 +7,21 @@ jest.autoMockOff();
    77
    import Immutable = require('immutable');
    88
    import Cursor = require('immutable/contrib/cursor');
    99

    10-
    jasmine.getEnv().addEqualityTester((a, b) =>
    11-
    a instanceof Immutable.Iterable && b instanceof Immutable.Iterable ?
    12-
    Immutable.is(a, b) :
    13-
    jasmine.undefined
    14-
    );
    15-
    1610
    describe('Cursor', () => {
    1711

    12+
    beforeEach(function () {
    13+
    this.addMatchers({
    14+
    toValueEqual: function (expected) {
    15+
    var actual = this.actual;
    16+
    if (!Immutable.is(expected, this.actual)) {
    17+
    this.message = 'Expected\n' + this.actual + '\nto equal\n' + expected;
    18+
    return false;
    19+
    }
    20+
    return true;
    21+
    }
    22+
    });
    23+
    });
    24+
    1825
    var json = { a: { b: { c: 1 } } };
    1926

    2027
    it('gets from its path', () => {
    @@ -60,7 +67,7 @@ describe('Cursor', () => {
    6067
    var data = Immutable.fromJS(json);
    6168
    var cursor = Cursor.from(data, ['a', 'b']);
    6269
    expect(cursor.toJS()).toEqual(json.a.b);
    63-
    expect(cursor).toEqual(data.getIn(['a', 'b']));
    70+
    expect(cursor).toValueEqual(data.getIn(['a', 'b']));
    6471
    expect(cursor.size).toBe(1);
    6572
    expect(cursor.get('c')).toBe(1);
    6673
    });
    @@ -85,19 +92,17 @@ describe('Cursor', () => {
    8592
    // cursor edits return new cursors:
    8693
    var newDeepCursor = deepCursor.update(x => x + 1);
    8794
    expect(newDeepCursor.deref()).toBe(2);
    88-
    expect(onChange).lastCalledWith(
    89-
    Immutable.fromJS({a:{b:{c:2}}}),
    90-
    data,
    91-
    ['a', 'b', 'c']
    92-
    );
    95+
    var call1 = onChange.mock.calls[0];
    96+
    expect(call1[0]).toValueEqual(Immutable.fromJS({a:{b:{c:2}}}));
    97+
    expect(call1[1]).toBe(data);
    98+
    expect(call1[2]).toEqual(['a', 'b', 'c']);
    9399

    94100
    var newestDeepCursor = newDeepCursor.update(x => x + 1);
    95101
    expect(newestDeepCursor.deref())< EF56 /span>.toBe(3);
    96-
    expect(onChange).lastCalledWith(
    97-
    Immutable.fromJS({a:{b:{c:3}}}),
    98-
    Immutable.fromJS({a:{b:{c:2}}}),
    99-
    ['a', 'b', 'c']
    100-
    );
    102+
    var call2 = onChange.mock.calls[1];
    103+
    expect(call2[0]).toValueEqual(Immutable.fromJS({a:{b:{c:3}}}));
    104+
    expect(call2[1]).toValueEqual(Immutable.fromJS({a:{b:{c:2}}}));
    105+
    expect(call2[2]).toEqual(['a', 'b', 'c']);
    101106

    102107
    // meanwhile, data is still immutable:
    103108
    expect(data.toJS()).toEqual(json);
    @@ -106,11 +111,10 @@ describe('Cursor', () => {
    106111
    expect(deepCursor.deref()).toBe(1);
    107112
    var otherNewDeepCursor = deepCursor.update(x => x + 10);
    108113
    expect(otherNewDeepCursor.deref()).toBe(11);
    109-
    expect(onChange).lastCalledWith(
    110-
    Immutable.fromJS({a:{b:{c:11}}}),
    111-
    data,
    112-
    ['a', 'b', 'c']
    113-
    );
    114+
    var call3 = onChange.mock.calls[2];
    115+
    expect(call3[0]).toValueEqual(Immutable.fromJS({a:{b:{c:11}}}));
    116+
    expect(call3[1]).toBe(data);
    117+
    expect(call3[2]).toEqual(['a', 'b', 'c']);
    114118

    115119
    // and update has been called exactly thrice.
    116120
    expect(onChange.mock.calls.length).toBe(3);
    @@ -126,21 +130,19 @@ describe('Cursor', () => {
    126130
    // onChange returning undefined has no effect
    127131
    var newCursor = deepCursor.update(x => x + 1);
    128132
    expect(newCursor.deref()).toBe(2);
    129-
    expect(onChange).lastCalledWith(
    130-
    Immutable.fromJS({a:{b:{c:2}}}),
    131-
    data,
    132-
    ['a', 'b', 'c']
    133-
    );
    133+
    var call1 = onChange.mock.calls[0];
    134+
    expect(call1[0]).toValueEqual(Immutable.fromJS({a:{b:{c:2}}}));
    135+
    expect(call1[1]).toBe(data);
    136+
    expect(call1[2]).toEqual(['a', 'b', 'c']);
    134137

    135138
    onChange.mockReturnValueOnce(Immutable.fromJS({a:{b:{c:11}}}));
    136139
    // onChange returning something else has an effect
    137140
    newCursor = newCursor.update(x => 999);
    138141
    expect(newCursor.deref()).toBe(11);
    139-
    expect(onChange).lastCalledWith(
    140-
    Immutable.fromJS({a:{b:{c:999}}}),
    141-
    Immutable.fromJS({a:{b:{c:2}}}),
    142-
    ['a', 'b', 'c']
    143-
    );
    142+
    var call2 = onChange.mock.calls[1];
    143+
    expect(call2[0]).toValueEqual(Immutable.fromJS({a:{b:{c:999}}}));
    144+
    expect(call2[1]).toValueEqual(Immutable.fromJS({a:{b:{c:2}}}));
    145+
    expect(call2[2]).toEqual(['a', 'b', 'c']);
    144146

    145147
    // and update has been called exactly twice
    146148
    expect(onChange.mock.calls.length).toBe(2);
    @@ -154,22 +156,22 @@ describe('Cursor', () => {
    154156
    var bCursor = aCursor.cursor('b');
    155157
    var cCursor = bCursor.cursor('c');
    156158

    157-
    expect(bCursor.set('c', 10).deref()).toEqual(
    159+
    expect(bCursor.set('c', 10).deref()).toValueEqual(
    158160
    Immutable.fromJS({ c: 10 })
    159161
    );
    160-
    expect(onChange).lastCalledWith(
    161-
    Immutable.fromJS({ a: { b: { c: 10 } } }),
    162-
    data,
    163-
    ['a', 'b', 'c']
    164-
    );
    162+
    163+
    var call1 = onChange.mock.calls[0];
    164+
    expect(call1[0]).toValueEqual(Immutable.fromJS({a:{b:{c:10}}}));
    165+
    expect(call1[1]).toBe(data);
    166+
    expect(call1[2]).toEqual(['a', 'b', 'c']);
    165167
    });
    166168

    167169
    it('creates maps as necessary', () => {
    168170
    var data = Immutable.Map();
    169171
    var cursor = Cursor.from(data, ['a', 'b', 'c']);
    170172
    expect(cursor.deref()).toBe(undefined);
    171173
    cursor = cursor.set('d', 3);
    172-
    expect(cursor.deref()).toEqual(Immutable.Map({d: 3}));
    174+
    expect(cursor.deref()).toValueEqual(Immutable.Map({d: 3}));
    173175
    });
    174176

    175177
    it('can set undefined', () => {
    @@ -183,59 +185,59 @@ describe('Cursor', () => {
    183185
    it('has the sequence API', () => {
    184186
    var data = Immutable.Map({a: 1, b: 2, c: 3});
    185187
    var cursor = Cursor.from(data);
    186-
    expect(cursor.map((x: number) => x * x)).toEqual(Immutable.Map({a: 1, b: 4, c: 9}));
    188+
    expect(cursor.map((x: number) => x * x)).toValueEqual(Immutable.Map({a: 1, b: 4, c: 9}));
    187189
    });
    188190

    189191
    it('can push values on a List', () => {
    190192
    var onChange = jest.genMockFunction();
    191193
    var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
    192194
    var cursor = Cursor.from(data, ['a', 'b'], onChange);
    193195

    194-
    expect(cursor.push(3,4)).toEqual(Immutable.List([0, 1, 2, 3, 4]));
    195-
    expect(onChange).lastCalledWith(
    196-
    Immutable.fromJS({a: {b: [0, 1, 2, 3, 4]}}),
    197-
    data,
    198-
    ['a', 'b']
    199-
    );
    196+
    expect(cursor.push(3,4)).toValueEqual(Immutable.List([0, 1, 2, 3, 4]));
    197+
    198+
    var call = onChange.mock.calls[0];
    199+
    expect(call[0]).toValueEqual(Immutable.fromJS({a: {b: [0, 1, 2, 3, 4]}}));
    200+
    expect(call[1]).toBe(data);
    201+
    expect(call[2]).toEqual(['a', 'b']);
    200202
    });
    201203

    202204
    it('can pop values of a List', () => {
    203205
    var onChange = jest.genMockFunction();
    204206
    var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
    205207
    var cursor = Cursor.from(data, ['a', 'b'], onChange);
    206208

    207-
    expect(cursor.pop()).toEqual(Immutable.List([0, 1]));
    208-
    expect(onChange).lastCalledWith(
    209-
    Immutable.fromJS({a: {b: [0, 1]}}),
    210-
    data,
    211-
    ['a', 'b']
    212-
    );
    209+
    expect(cursor.pop()).toValueEqual(Immutable.List([0, 1]));
    210+
    211+
    var call = onChange.mock.calls[0];
    212+
    expect(call[0]).toValueEqual(Immutable.fromJS({a: {b: [0, 1]}}));
    213+
    expect(call[1]).toBe(data);
    214+
    expect(call[2]).toEqual(['a', 'b']);
    213215
    });
    214216

    215217
    it('can unshift values on a List', () => {
    216218
    var onChange = jest.genMockFunction();
    217219
    var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
    218220
    var cursor = Cursor.from(data, ['a', 'b'], onChange);
    219221

    220-
    expect(cursor.unshift(-2, -1)).toEqual(Immutable.List([-2, -1, 0, 1, 2]));
    221-
    expect(onChange).lastCalledWith(
    222-
    Immutable.fromJS({a: {b: [-2, -1, 0, 1, 2]}}),
    223-
    data,
    224-
    ['a', 'b']
    225-
    );
    222+
    expect(cursor.unshift(-2, -1)).toValueEqual(Immutable.List([-2, -1, 0, 1, 2]));
    223+
    224+
    var call = onChange.mock.calls[0];
    225+
    expect(call[0]).toValueEqual(Immutable.fromJS({a: {b: [-2, -1, 0, 1, 2]}}));
    226+
    expect(call[1]).toBe(data);
    227+
    expect(call[2]).toEqual(['a', 'b']);
    226228
    });
    227229

    228230
    it('can shift values of a List', () => {
    229231
    var onChange = jest.genMockFunction();
    230232
    var data = Immutable.fromJS({a: {b: [0, 1, 2]}});
    231233
    var cursor = Cursor.from(data, ['a', 'b'], onChange);
    232234

    233-
    expect(cursor.shift()).toEqual(Immutable.List([1, 2]));
    234-
    expect(onChange).lastCalledWith(
    235-
    Immutable.fromJS({a: {b: [1, 2]}}),
    236-
    data,
    237-
    ['a', 'b']
    238-
    );
    235+
    expect(cursor.shift()).toValueEqual(Immutable.List([1, 2]));
    236+
    237+
    var call = onChange.mock.calls[0];
    238+
    expect(call[0]).toValueEqual(Immutable.fromJS({a: {b: [1, 2]}}));
    239+
    expect(call[1]).toBe(data);
    240+
    expect(call[2]).toEqual(['a', 'b']);
    239241
    });
    240242

    241243

    @@ -247,11 +249,11 @@ describe('Cursor', () => {
    247249
    var found = cursor.find(map => map.get('v') === 2);
    248250
    expect(typeof found.deref).toBe('function'); // is a cursor!
    249251
    found = found.set('v', 20);
    250-
    expect(onChange).lastCalledWith(
    251-
    Immutable.fromJS({a: {v: 1}, b: {v: 20}, c: {v: 3}}),
    252-
    data,
    253-
    ['b', 'v']
    254-
    );
    252+
    253+
    var call = onChange.mock.calls[0];
    254+
    expect(call[0]).toValueEqual(Immutable.fromJS({a: {v: 1}, b: {v: 20}, c: {v: 3}}));
    255+
    expect(call[1]).toBe(data);
    256+
    expect(call[2]).toEqual(['b', 'v']);
    255257
    });
    256258

    257259
    it('returns wrapped values for iteration API', () => {
    @@ -309,7 +311,7 @@ describe('Cursor', () => {
    309311
    var c2 = c1.withMutations(m => m.set('x', 1).set('y', 2).set('z', 3));
    310312

    311313
    expect(c1.deref()).toEqual(undefined);
    312-
    expect(c2.deref()).toEqual(Immutable.fromJS(
    314+
    expect(c2.deref()).toValueEqual(Immutable.fromJS(
    313315
    { x: 1, y: 2, z: 3 }
    314316
    ));
    315317
    expect(onChange.mock.calls.length).toBe(1);
    @@ -334,11 +336,11 @@ describe('Cursor', () => {
    334336
    var c = Cursor.from(data, ['a'], onChange);
    335337
    var c1 = c.updateIn(['b', 'c'], x => x * 10);
    336338
    expect(c1.getIn(['b', 'c'])).toBe(10);
    337-
    expect(onChange).lastCalledWith(
    338-
    Immutable.fromJS({a:{b:{c:10}}}),
    339-
    data,
    340-
    ['a', 'b', 'c']
    341-
    );
    339+
    340+
    var call = onChange.mock.calls[0];
    341+
    expect(call[0]).toValueEqual(Immutable.fromJS({a:{b:{c:10}}}));
    342+
    expect(call[1]).toBe(data);
    343+
    expect(call[2]).toEqual(['a', 'b', 'c']);
    342344
    });
    343345

    344346
    it('can set deeply', () => {
    @@ -347,11 +349,11 @@ describe('Cursor', () => {
    347349
    var c = Cursor.from(data, ['a'], onChange);
    348350
    var c1 = c.setIn(['b', 'c'], 10);
    349351
    expect(c1.getIn(['b', 'c'])).toBe(10);
    350-
    expect(onChange).lastCalledWith(
    351-
    Immutable.fromJS({a:{b:{c:10}}}),
    352-
    data,
    353-
    ['a', 'b', 'c']
    354-
    );
    352+
    353+
    var call = onChange.mock.calls[0];
    354+
    expect(call[0]).toValueEqual(Immutable.fromJS({a:{b:{c:10}}}));
    355+
    expect(call[1]).toBe(data);
    356+
    expect(call[2]).toEqual(['a', 'b', 'c']);
    355357
    });
    356358

    357359
    it('can get Record value as a property', () => {
    @@ -368,11 +370,11 @@ describe('Cursor', () => {
    368370
    var c = Cursor.from(data, ['a'], onChange);
    369371
    var c1 = c.set(2);
    370372
    expect(c1.deref()).toBe(2);
    371-
    expect(onChange).lastCalledWith(
    372-
    Immutable.fromJS({a:2}),
    373-
    data,
    374-
    ['a']
    375-
    );
    373+
    374+
    var call = onChange.mock.calls[0];
    375+
    expect(call[0]).toValueEqual(Immutable.fromJS({a:2}));
    376+
    expect(call[1]).toBe(data);
    377+
    expect(call[2]).toEqual(['a']);
    376378
    });
    377379

    378380
    it('can set value of a cursor to undefined directly', () => {
    @@ -381,11 +383,11 @@ describe('Cursor', () => {
    381383
    var c = Cursor.from(data, ['a'], onChange);
    382384
    var c1 = c.set(undefined);
    383385
    expect(c1.deref()).toBe(undefined);
    384-
    expect(onChange).lastCalledWith(
    385-
    Immutable.fromJS({a:undefined}),
    386-
    data,
    387-
    ['a']
    388-
    );
    386+
    387+
    var call = onChange.mock.calls[0];
    388+
    expect(call[0]).toValueEqual(Immutable.fromJS({a:undefined}));
    389+
    expect(call[1]).toBe(data);
    390+
    expect(call[2]).toEqual(['a']);
    389391
    });
    390392

    391393
    });

    contrib/cursor/index.js

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -15,7 +15,7 @@
    1515
    * If you wish to use it in the browser, please check out Browserify or WebPack!
    1616
    */
    1717

    18-
    var Immutable = require('immutable');
    18+
    var Immutable = require('../../');
    1919
    var Iterable = Immutable.Iterable;
    2020
    var Iterator = Iterable.Iterator;
    2121
    var Seq = Immutable.Seq;

    resources/jest.d.ts

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -18,6 +18,7 @@ interface Expect {
    1818
    toThrow(message?: string): void
    1919
    toBe(value: any): void
    2020
    toEqual(value: any): void
    21+
    toValueEqual(value: any): void
    2122
    toBeFalsy(): void
    2223
    toBeTruthy(): void
    2324
    toBeNull(): void

    0 commit comments

    Comments
     (0)
    0