Skip to content

Commit 78f9905

Browse files
committed
improvement(useKey): Adding tests and docs
1 parent 6364bea commit 78f9905

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

src/functions/useKey/stories/useKey.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,34 @@ Vue function that executes a handler when a keyboard key is pressed.
55
## Reference
66

77
```typescript
8-
// function useKey()
8+
type UseKeyFilter = string | ((event: KeyboardEvent) => boolean)
9+
```
10+
11+
```typescript
12+
function useKey(
13+
filter: UseKeyFilter,
14+
callback?: any,
15+
runOnMount?: boolean
16+
): {
17+
isPressed: Ref<boolean>
18+
isTracking: Ref<boolean>
19+
start: () => void
20+
stop: () => void
21+
}
922
```
1023

1124
### Parameters
1225

13-
- `value: string` lorem ipsa
26+
- `filter: UseKeyFilter` the filter string or function to use for triggering the key event
27+
- `callback: Function` the function called when the given key is pressed
28+
- `runOnMount: boolean` whether to track the given key on mount, `true` by default.
1429

1530
### Returns
1631

17-
- `value: Ref<string>` lorem ipsa
32+
- `isPressed: Ref<boolean>` whether the key is currently pressed or not
33+
- `isTracking: Ref<boolean>` whether this function events are running or not
34+
- `start: Function` the function used for start tracking the key event
35+
- `stop: Function` the function used for stop tracking the key event
1836

1937
## Usage
2038

src/functions/useKey/stories/useKey.story.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ const basicDemo = () => ({
1717
demo-name="UseKeyDemo.vue"
1818
>
1919
<template v-slot:title></template>
20-
<template v-slot:intro></template>
20+
<template v-slot:intro>
21+
<p>
22+
Press an arrow key (←, →, ↑, ↓) to move the ninja cat around
23+
the screen.
24+
</p>
25+
</template>
2126
</story-title>
2227
<demo />
2328
</div>`
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
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'
39

410
afterEach(() => {
511
jest.clearAllMocks()
612
})
713

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+
833
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)
1161
})
1262
})

0 commit comments

Comments
 (0)