|
| 1 | +import useActions from '../useActions'; |
| 2 | +import renderHook from '../util/renderHook'; |
| 3 | + |
| 4 | +interface InjectActions { |
| 5 | + incrementAsync: (delay?: number) => void; |
| 6 | + decrementAsync: (delay?: number) => void; |
| 7 | +} |
| 8 | + |
| 9 | +describe('useActions', () => { |
| 10 | + it('should be defined', () => { |
| 11 | + expect(useActions).toBeDefined(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should be defined actions', () => { |
| 15 | + const { vm } = renderHook<InjectActions>(() => ({ |
| 16 | + ...useActions(['incrementAsync']), |
| 17 | + ...useActions('test', ['decrementAsync']), |
| 18 | + })); |
| 19 | + |
| 20 | + expect(vm.incrementAsync).toBeDefined(); |
| 21 | + expect(vm.decrementAsync).toBeDefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should async update count state', () => { |
| 25 | + const { vm } = renderHook<InjectActions>(() => ({ |
| 26 | + ...useActions(['incrementAsync']), |
| 27 | + ...useActions('test', ['decrementAsync']), |
| 28 | + })); |
| 29 | + |
| 30 | + expect(vm.$store.state.count).toBe(0); |
| 31 | + expect(vm.$store.state.test.count).toBe(0); |
| 32 | + |
| 33 | + vm.incrementAsync(0); |
| 34 | + vm.decrementAsync(0); |
| 35 | + |
| 36 | + setTimeout(() => { |
| 37 | + expect(vm.$store.state.count).toBe(1); |
| 38 | + expect(vm.$store.state.test.count).toBe(-1); |
| 39 | + }); |
| 40 | + }); |
| 41 | +}); |
0 commit comments