|
| 1 | +import { act, renderHook } from '@testing-library/react-hooks'; |
| 2 | +import useUpsert from '../useUpsert'; |
| 3 | + |
| 4 | +interface TestItem { |
| 5 | + id: string; |
| 6 | + text: string; |
| 7 | +} |
| 8 | + |
| 9 | +const testItems: TestItem[] = [{ id: '1', text: '1' }, { id: '2', text: '2' }]; |
| 10 | + |
| 11 | +const itemsAreEqual = (a: TestItem, b: TestItem) => { |
| 12 | + return a.id === b.id; |
| 13 | +}; |
| 14 | + |
| 15 | +const setUp = (initialList: TestItem[] = []) => renderHook(() => useUpsert<TestItem>(itemsAreEqual, initialList)); |
| 16 | + |
| 17 | +describe('useUpsert', () => { |
| 18 | + describe('initialization', () => { |
| 19 | + const { result } = setUp(testItems); |
| 20 | + const [list, utils] = result.current; |
| 21 | + |
| 22 | + it('properly initiates the list content', () => { |
| 23 | + expect(list).toEqual(testItems); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns an upsert function', () => { |
| 27 | + expect(utils.upsert).toBeInstanceOf(Function); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('upserting a new item', () => { |
| 32 | + const { result } = setUp(testItems); |
| 33 | + const [, utils] = result.current; |
| 34 | + |
| 35 | + const newItem: TestItem = { |
| 36 | + id: '3', |
| 37 | + text: '3', |
| 38 | + }; |
| 39 | + act(() => { |
| 40 | + utils.upsert(newItem); |
| 41 | + }); |
| 42 | + |
| 43 | + it('inserts a new item', () => { |
| 44 | + expect(result.current[0]).toContain(newItem); |
| 45 | + }); |
| 46 | + it('works immutably', () => { |
| 47 | + expect(result.current[0]).not.toBe(testItems); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('upserting an existing item', () => { |
| 52 | + const { result } = setUp(testItems); |
| 53 | + const [, utils] = result.current; |
| 54 | + |
| 55 | + const newItem: TestItem = { |
| 56 | + id: '2', |
| 57 | + text: '4', |
| 58 | + }; |
| 59 | + act(() => { |
| 60 | + utils.upsert(newItem); |
| 61 | + }); |
| 62 | + const updatedList = result.current[0]; |
| 63 | + |
| 64 | + it('has the same length', () => { |
| 65 | + expect(updatedList).toHaveLength(testItems.length); |
| 66 | + }); |
| 67 | + it('updates the item', () => { |
| 68 | + expect(updatedList).toContain(newItem); |
| 69 | + }); |
| 70 | + it('works immutably', () => { |
| 71 | + expect(updatedList).not.toBe(testItems); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments