|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import Replaceable from '../Replaceable'; |
| 9 | + |
| 10 | +describe('Replaceable', () => { |
| 11 | + describe('constructor', () => { |
| 12 | + test('init with object', () => { |
| 13 | + const replaceable = new Replaceable({a: 1, b: 2}); |
| 14 | + expect(replaceable.object).toEqual({a: 1, b: 2}); |
| 15 | + expect(replaceable.type).toBe('object'); |
| 16 | + }); |
| 17 | + |
| 18 | + test('init with array', () => { |
| 19 | + const replaceable = new Replaceable([1, 2, 3]); |
| 20 | + expect(replaceable.object).toEqual([1, 2, 3]); |
| 21 | + expect(replaceable.type).toBe('array'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('init with Map', () => { |
| 25 | + const replaceable = new Replaceable( |
| 26 | + new Map([ |
| 27 | + ['a', 1], |
| 28 | + ['b', 2], |
| 29 | + ]), |
| 30 | + ); |
| 31 | + expect(replaceable.object).toEqual( |
| 32 | + new Map([ |
| 33 | + ['a', 1], |
| 34 | + ['b', 2], |
| 35 | + ]), |
| 36 | + ); |
| 37 | + expect(replaceable.type).toBe('map'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('init with other type should throw error', () => { |
| 41 | + expect(() => { |
| 42 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 43 | + const replaceable = new Replaceable(new Date()); |
| 44 | + }).toThrow('Type date is not support in Replaceable!'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('get', () => { |
| 49 | + test('get object item', () => { |
| 50 | + const replaceable = new Replaceable({a: 1, b: 2}); |
| 51 | + expect(replaceable.get('b')).toBe(2); |
| 52 | + }); |
| 53 | + |
| 54 | + test('get array item', () => { |
| 55 | + const replaceable = new Replaceable([1, 2, 3]); |
| 56 | + expect(replaceable.get(1)).toBe(2); |
| 57 | + }); |
| 58 | + |
| 59 | + test('get Map item', () => { |
| 60 | + const replaceable = new Replaceable( |
| 61 | + new Map([ |
| 62 | + ['a', 1], |
| 63 | + ['b', 2], |
| 64 | + ]), |
| 65 | + ); |
| 66 | + expect(replaceable.get('b')).toBe(2); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('set', () => { |
| 71 | + test('set object item', () => { |
| 72 | + const replaceable = new Replaceable({a: 1, b: 2}); |
| 73 | + replaceable.set('b', 3); |
| 74 | + expect(replaceable.object).toEqual({a: 1, b: 3}); |
| 75 | + }); |
| 76 | + |
| 77 | + test('set array item', () => { |
| 78 | + const replaceable = new Replaceable([1, 2, 3]); |
| 79 | + replaceable.set(1, 3); |
| 80 | + expect(replaceable.object).toEqual([1, 3, 3]); |
| 81 | + }); |
| 82 | + |
| 83 | + test('set Map item', () => { |
| 84 | + const replaceable = new Replaceable( |
| 85 | + new Map([ |
| 86 | + ['a', 1], |
| 87 | + ['b', 2], |
| 88 | + ]), |
| 89 | + ); |
| 90 | + replaceable.set('b', 3); |
| 91 | + expect(replaceable.object).toEqual( |
| 92 | + new Map([ |
| 93 | + ['a', 1], |
| 94 | + ['b', 3], |
| 95 | + ]), |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('forEach', () => { |
| 101 | + test('object forEach', () => { |
| 102 | + const replaceable = new Replaceable({a: 1, b: 2}); |
| 103 | + const cb = jest.fn(); |
| 104 | + replaceable.forEach(cb); |
| 105 | + expect(cb.mock.calls[0]).toEqual([1, 'a', {a: 1, b: 2}]); |
| 106 | + expect(cb.mock.calls[1]).toEqual([2, 'b', {a: 1, b: 2}]); |
| 107 | + }); |
| 108 | + |
| 109 | + test('array forEach', () => { |
| 110 | + const replaceable = new Replaceable([1, 2, 3]); |
| 111 | + const cb = jest.fn(); |
| 112 | + replaceable.forEach(cb); |
| 113 | + expect(cb.mock.calls[0]).toEqual([1, 0, [1, 2, 3]]); |
| 114 | + expect(cb.mock.calls[1]).toEqual([2, 1, [1, 2, 3]]); |
| 115 | + expect(cb.mock.calls[2]).toEqual([3, 2, [1, 2, 3]]); |
| 116 | + }); |
| 117 | + |
| 118 | + test('map forEach', () => { |
| 119 | + const map = new Map([ |
| 120 | + ['a', 1], |
| 121 | + ['b', 2], |
| 122 | + ]); |
| 123 | + const replaceable = new Replaceable(map); |
| 124 | + const cb = jest.fn(); |
| 125 | + replaceable.forEach(cb); |
| 126 | + expect(cb.mock.calls[0]).toEqual([1, 'a', map]); |
| 127 | + expect(cb.mock.calls[1]).toEqual([2, 'b', map]); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('isReplaceable', () => { |
| 132 | + test('should return true if two object types equal and support', () => { |
| 133 | + expect(Replaceable.isReplaceable({a: 1}, {b: 2})).toBe(true); |
| 134 | + expect(Replaceable.isReplaceable([], [1, 2, 3])).toBe(true); |
| 135 | + expect( |
| 136 | + Replaceable.isReplaceable( |
| 137 | + new Map(), |
| 138 | + new Map([ |
| 139 | + ['a', 1], |
| 140 | + ['b', 2], |
| 141 | + ]), |
| 142 | + ), |
| 143 | + ).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + test('should return false if two object types not equal', () => { |
| 147 | + expect(Replaceable.isReplaceable({a: 1}, [1, 2, 3])).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + test('should return false if object types not support', () => { |
| 151 | + expect(Replaceable.isReplaceable('foo', 'bar')).toBe(false); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments