|
| 1 | +import useMap from '..' |
| 2 | + |
| 3 | +describe('useMap', () => { |
| 4 | + it('should init map and utils', () => { |
| 5 | + const [map, utils] = useMap<string | number, any>([ |
| 6 | + ['foo', 'bar'], |
| 7 | + ['a', 1], |
| 8 | + ]) |
| 9 | + expect(Array.from(map.value)).toEqual([ |
| 10 | + ['foo', 'bar'], |
| 11 | + ['a', 1], |
| 12 | + ]) |
| 13 | + expect(utils).toStrictEqual({ |
| 14 | + get: expect.any(Function), |
| 15 | + set: expect.any(Function), |
| 16 | + setAll: expect.any(Function), |
| 17 | + remove: expect.any(Function), |
| 18 | + reset: expect.any(Function), |
| 19 | + clear: expect.any(Function), |
| 20 | + has: expect.any(Function), |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should init empty map if not initial object provided', () => { |
| 25 | + const [map] = useMap() |
| 26 | + expect([...map.value]).toEqual([]) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should get corresponding value for initial provided key', () => { |
| 30 | + const [, utils] = useMap<string | number, any>([ |
| 31 | + ['foo', 'bar'], |
| 32 | + ['a', 1], |
| 33 | + ]) |
| 34 | + |
| 35 | + expect(utils.get('a')).toBe(1) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should get corresponding value for existing provided key', () => { |
| 39 | + const [, utils] = useMap<string | number, any>([ |
| 40 | + ['foo', 'bar'], |
| 41 | + ['a', 1], |
| 42 | + ]) |
| 43 | + |
| 44 | + utils.set('a', 666) |
| 45 | + expect(utils.get('a')).toBe(666) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should get undefined for non-existing provided key', () => { |
| 49 | + const [, utils] = useMap<string | number, any>([ |
| 50 | + ['foo', 'bar'], |
| 51 | + ['a', 1], |
| 52 | + ]) |
| 53 | + |
| 54 | + expect(utils.get('nonExisting')).toBeUndefined() |
| 55 | + }) |
| 56 | + |
| 57 | + it('should set new key-value pair', () => { |
| 58 | + const [map, utils] = useMap<string | number, any>([ |
| 59 | + ['foo', 'bar'], |
| 60 | + ['a', 1], |
| 61 | + ]) |
| 62 | + utils.set('newKey', 99) |
| 63 | + |
| 64 | + expect([...map.value]).toEqual([ |
| 65 | + ['foo', 'bar'], |
| 66 | + ['a', 1], |
| 67 | + ['newKey', 99], |
| 68 | + ]) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should override current value if setting existing key', () => { |
| 72 | + const [map, utils] = useMap<string | number, any>([ |
| 73 | + ['foo', 'bar'], |
| 74 | + ['a', 1], |
| 75 | + ]) |
| 76 | + |
| 77 | + utils.set('foo', 'qux') |
| 78 | + |
| 79 | + expect([...map.value]).toEqual([ |
| 80 | + ['foo', 'qux'], |
| 81 | + ['a', 1], |
| 82 | + ]) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should set new map', () => { |
| 86 | + const [map, utils] = useMap<string | number, any>([ |
| 87 | + ['foo', 'bar'], |
| 88 | + ['a', 1], |
| 89 | + ]) |
| 90 | + |
| 91 | + utils.setAll([ |
| 92 | + ['foo', 'foo'], |
| 93 | + ['a', 2], |
| 94 | + ]) |
| 95 | + |
| 96 | + expect([...map.value]).toEqual([ |
| 97 | + ['foo', 'foo'], |
| 98 | + ['a', 2], |
| 99 | + ]) |
| 100 | + }) |
| 101 | + |
| 102 | + it('remove should be work', () => { |
| 103 | + const [map, utils] = useMap<string | number, any>([ |
| 104 | + ['foo', 'bar'], |
| 105 | + ['a', 1], |
| 106 | + ]) |
| 107 | + expect(map.value.size).toBe(2) |
| 108 | + utils.remove('foo') |
| 109 | + expect(map.value.size).toBe(1) |
| 110 | + }) |
| 111 | + |
| 112 | + it('reset should be work', () => { |
| 113 | + const [map, utils] = useMap<string | number, any>([ |
| 114 | + ['foo', 'bar'], |
| 115 | + ['a', 1], |
| 116 | + ]) |
| 117 | + utils.set('text', 'new map') |
| 118 | + expect([...map.value]).toEqual([ |
| 119 | + ['foo', 'bar'], |
| 120 | + ['a', 1], |
| 121 | + ['text', 'new map'], |
| 122 | + ]) |
| 123 | + utils.reset() |
| 124 | + expect([...map.value]).toEqual([ |
| 125 | + ['foo', 'bar'], |
| 126 | + ['a', 1], |
| 127 | + ]) |
| 128 | + }) |
| 129 | +}) |
0 commit comments