|
| 1 | +import useSet from '..' |
| 2 | + |
| 3 | +describe('useSet', () => { |
| 4 | + it('should init set and utils', () => { |
| 5 | + const [set, utils] = useSet([1, 2]) |
| 6 | + |
| 7 | + expect(set.value).toEqual(new Set([1, 2])) |
| 8 | + expect(utils).toStrictEqual({ |
| 9 | + add: expect.any(Function), |
| 10 | + remove: expect.any(Function), |
| 11 | + reset: expect.any(Function), |
| 12 | + clear: expect.any(Function), |
| 13 | + has: expect.any(Function), |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should init empty set if no initial set provided', () => { |
| 18 | + const [set] = useSet() |
| 19 | + |
| 20 | + expect(set.value).toEqual(new Set()) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should have an initially provided key', () => { |
| 24 | + const [, utils] = useSet([1, 2]) |
| 25 | + expect(utils.has(1)).toBeTruthy() |
| 26 | + expect(utils.has(3)).toBeFalsy() |
| 27 | + }) |
| 28 | + |
| 29 | + it('should have an added key', () => { |
| 30 | + const [, utils] = useSet([1, 2]) |
| 31 | + utils.add(3) |
| 32 | + |
| 33 | + expect(utils.has(3)).toBeTruthy() |
| 34 | + }) |
| 35 | + |
| 36 | + it('should get false for non-existing key', () => { |
| 37 | + const [, utils] = useSet([1, 2]) |
| 38 | + |
| 39 | + expect(utils.has(3)).toBeFalsy() |
| 40 | + }) |
| 41 | + |
| 42 | + it('should add a new key', () => { |
| 43 | + const [set, utils] = useSet([1, 2]) |
| 44 | + utils.add(3) |
| 45 | + |
| 46 | + expect(set.value).toEqual(new Set([1, 2, 3])) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should work if setting existing key', () => { |
| 50 | + const [set, utils] = useSet(['oldKey']) |
| 51 | + utils.add('oldKey') |
| 52 | + |
| 53 | + expect(set.value).toEqual(new Set(['oldKey'])) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should remove existing key', () => { |
| 57 | + const [set, utils] = useSet([1, 2]) |
| 58 | + utils.remove(2) |
| 59 | + |
| 60 | + expect(set.value).toEqual(new Set([1])) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should do nothing if removing non-existing key', () => { |
| 64 | + const [set, utils] = useSet(['a', 'b']) |
| 65 | + utils.remove('nonExisting') |
| 66 | + |
| 67 | + expect(set.value).toEqual(new Set(['a', 'b'])) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should reset to initial set provided', () => { |
| 71 | + const [set, utils] = useSet([1]) |
| 72 | + utils.add(2) |
| 73 | + expect(set.value).toEqual(new Set([1, 2])) |
| 74 | + utils.reset() |
| 75 | + |
| 76 | + expect(set.value).toEqual(new Set([1])) |
| 77 | + }) |
| 78 | +}) |
0 commit comments