| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import Immutable, {List} from 'immutable'; |
| b69ab31 | | | 9 | import {SelfUpdate} from '../immutableExt'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | describe('SelfUpdate', () => { |
| b69ab31 | | | 12 | it('is needed because of immutable.js deepEquals', () => { |
| b69ab31 | | | 13 | const list1 = nestedList(10); |
| b69ab31 | | | 14 | const list2 = nestedList(10); |
| b69ab31 | | | 15 | // Immutable.is performs deepEqual repetitively. |
| b69ab31 | | | 16 | expect(immutableIsCallCounts(list1, list2)).toMatchObject([11, 11, 11]); |
| b69ab31 | | | 17 | }); |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | it('avoids repetitive deepEquals', () => { |
| b69ab31 | | | 20 | const list1 = new SelfUpdate(nestedList(10)); |
| b69ab31 | | | 21 | const list2 = new SelfUpdate(nestedList(10)); |
| b69ab31 | | | 22 | expect(immutableIsCallCounts(list1, list2)).toMatchObject([11, 1, 1]); |
| b69ab31 | | | 23 | }); |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | it('does not equal to a different type', () => { |
| b69ab31 | | | 26 | const list1 = new SelfUpdate(nestedList(10)); |
| b69ab31 | | | 27 | const list2 = nestedList(10); |
| b69ab31 | | | 28 | expect(Immutable.is(list1, list2)).toBeFalsy(); |
| b69ab31 | | | 29 | expect(Immutable.is(list2, list1)).toBeFalsy(); |
| b69ab31 | | | 30 | expect(list2.equals(list1)).toBeFalsy(); |
| b69ab31 | | | 31 | expect(list1.equals(list2)).toBeFalsy(); |
| b69ab31 | | | 32 | }); |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | it('helps when used as a nested item', () => { |
| b69ab31 | | | 35 | const list1 = List([List([new SelfUpdate(nestedList(8))])]); |
| b69ab31 | | | 36 | const list2 = List([List([new SelfUpdate(nestedList(8))])]); |
| b69ab31 | | | 37 | expect(immutableIsCallCounts(list1, list2)).toMatchObject([11, 3, 3]); |
| b69ab31 | | | 38 | }); |
| b69ab31 | | | 39 | }); |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | type NestedList = List<number | NestedList>; |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | /** Construct a nested List of a given depth. */ |
| b69ab31 | | | 44 | function nestedList(depth: number): NestedList { |
| b69ab31 | | | 45 | return depth <= 0 ? List([10]) : List([nestedList(depth - 1)]); |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | /** Call Immutable.is n times, return call counts. */ |
| b69ab31 | | | 49 | function immutableIsCallCounts(a: unknown, b: unknown, n = 3): Array<number> { |
| b69ab31 | | | 50 | const ListEqualsMock = jest.spyOn(List.prototype, 'equals'); |
| b69ab31 | | | 51 | const counts = Array.from({length: n}, () => { |
| b69ab31 | | | 52 | if (!Immutable.is(a, b)) { |
| b69ab31 | | | 53 | return -1; |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | const count = ListEqualsMock.mock.calls.length; |
| b69ab31 | | | 56 | ListEqualsMock.mockClear(); |
| b69ab31 | | | 57 | return count; |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | ListEqualsMock.mockRestore(); |
| b69ab31 | | | 60 | return counts; |
| b69ab31 | | | 61 | } |