|
1 | | -// import { mount } from '@src/helpers/test' |
2 | | -// import { useKey } from '@src/vue-use-kit' |
| 1 | +import { |
| 2 | + mount, |
| 3 | + checkOnMountAndUnmountEvents, |
| 4 | + checkOnStartEvents, |
| 5 | + checkOnStopEvents, |
| 6 | + checkElementExistenceOnMount |
| 7 | +} from '@src/helpers/test' |
| 8 | +import { useKey } from '@src/vue-use-kit' |
3 | 9 |
|
4 | 10 | afterEach(() => { |
5 | 11 | jest.clearAllMocks() |
6 | 12 | }) |
7 | 13 |
|
| 14 | +const testComponent = (filter = 'a', callback = () => ``, onMount = true) => ({ |
| 15 | + template: ` |
| 16 | + <div> |
| 17 | + <div id="isTracking" v-if="isTracking"></div> |
| 18 | + <div id="isPressed" v-if="isPressed"></div> |
| 19 | + <button id="start" @click="start"></button> |
| 20 | + <button id="stop" @click="stop"></button> |
| 21 | + </div> |
| 22 | + `, |
| 23 | + setup() { |
| 24 | + const { isPressed, isTracking, start, stop } = useKey( |
| 25 | + filter, |
| 26 | + callback, |
| 27 | + onMount |
| 28 | + ) |
| 29 | + return { isPressed, isTracking, start, stop } |
| 30 | + } |
| 31 | +}) |
| 32 | + |
8 | 33 | describe('useKey', () => { |
9 | | - it('should do something', () => { |
10 | | - // Add test here |
| 34 | + const noop = () => `` |
| 35 | + const events = ['keyup', 'keydown'] |
| 36 | + |
| 37 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 38 | + await checkOnMountAndUnmountEvents(document, events, testComponent) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should add events again when start is called', async () => { |
| 42 | + await checkOnStartEvents(document, events, testComponent) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should remove events when stop is called', async () => { |
| 46 | + await checkOnStopEvents(document, events, testComponent) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should show #isTracking when runOnMount is true', async () => { |
| 50 | + await checkElementExistenceOnMount(true, testComponent('a', noop, true)) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 54 | + await checkElementExistenceOnMount(false, testComponent('a', noop, false)) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should not show #isPressed when no key has been pressed', async () => { |
| 58 | + const wrapper = mount(testComponent()) |
| 59 | + await wrapper.vm.$nextTick() |
| 60 | + expect(wrapper.find('#isPressed').exists()).toBe(false) |
11 | 61 | }) |
12 | 62 | }) |
0 commit comments