-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
test(useKeyModifier): add test to cover all features #5051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5c1634
test(useKeyModifier): add test to cover all features
NoiseFan 7223c79
test(useKeyModifier): Use it.for syntax to simplify test case execution
NoiseFan aa03377
Merge branch 'main' into test/useKeyModifier
ilyaliao 7615d81
test(useKeyModifier): should be work with non-default event
NoiseFan a513ef3
Merge branch 'main' into test/useKeyModifier
NoiseFan 8094383
test(useKeyModifier): modify case comment
NoiseFan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
interface dispatchKeyboardEventOptions extends Partial<KeyboardEvent> { | ||
key: string | ||
target?: HTMLElement | ||
eventType?: 'keydown' | 'keyup' | ||
} | ||
|
||
/** | ||
* Dispatch keyboard event | ||
*/ | ||
export function dispatchKeyboardEvent(options: dispatchKeyboardEventOptions): void { | ||
const { eventType = 'keydown', target = document, key, ...args } = options | ||
target.dispatchEvent(new KeyboardEvent(eventType, { key, ...args })) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { KeyModifier } from './index' | ||
import { describe, expect, it } from 'vitest' | ||
import { dispatchKeyboardEvent } from '../../.test' | ||
import { useKeyModifier } from './index' | ||
|
||
/** | ||
* Assert useKeyModifier state | ||
*/ | ||
function assertModifierState(key: KeyModifier, options: KeyboardEventInit) { | ||
const state = useKeyModifier(key) | ||
expect(state.value).toBe(null) | ||
|
||
dispatchKeyboardEvent({ key, ...options }) | ||
expect(state.value).toBeTruthy() | ||
|
||
dispatchKeyboardEvent({ key, type: 'keyup' }) | ||
expect(state.value).toBeFalsy() | ||
} | ||
|
||
describe('useKeyModifier', () => { | ||
it('should be defined', () => { | ||
expect(useKeyModifier).toBeDefined() | ||
}) | ||
|
||
describe('all events', () => { | ||
const cases: Array<{ key: KeyModifier, options: KeyboardEventInit }> = [ | ||
{ key: 'Alt', options: { altKey: true } }, | ||
{ key: 'AltGraph', options: { modifierAltGraph: true } }, | ||
{ key: 'CapsLock', options: { modifierCapsLock: true } }, | ||
{ key: 'Control', options: { ctrlKey: true } }, | ||
{ key: 'Fn', options: { modifierFn: true } }, | ||
{ key: 'FnLock', options: { modifierFnLock: true } }, | ||
{ key: 'Meta', options: { metaKey: true } }, | ||
{ key: 'NumLock', options: { modifierNumLock: true } }, | ||
{ key: 'ScrollLock', options: { modifierScrollLock: true } }, | ||
{ key: 'Shift', options: { shiftKey: true } }, | ||
{ key: 'Symbol', options: { modifierSymbol: true } }, | ||
{ key: 'SymbolLock', options: { modifierSymbolLock: true } }, | ||
] | ||
|
||
it.for(cases)('should track state for %s', ({ key, options }) => { | ||
assertModifierState(key, options) | ||
}) | ||
}) | ||
|
||
describe('params', () => { | ||
describe('events', () => { | ||
it('should allow event to be specified', () => { | ||
const state = useKeyModifier('Alt', { events: ['mousedown'] }) | ||
expect(state.value).toBe(null) | ||
|
||
document.dispatchEvent(new MouseEvent('mousedown', { altKey: true })) | ||
expect(state.value).toBeTruthy() | ||
|
||
document.dispatchEvent(new MouseEvent('mousedown', { altKey: false })) | ||
expect(state.value).toBeFalsy() | ||
}) | ||
|
||
it('should be work with custom event', () => { | ||
const state = useKeyModifier('Alt', { events: ['click'] }) | ||
expect(state.value).toBe(null) | ||
|
||
document.dispatchEvent(new MouseEvent('click', { altKey: true })) | ||
43081j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(state.value).toBeTruthy() | ||
|
||
document.dispatchEvent(new MouseEvent('click', { altKey: false })) | ||
expect(state.value).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('initial', () => { | ||
it('should be null when initial value is default', () => { | ||
const state = useKeyModifier('Alt') | ||
expect(state.value).toBeNull() | ||
}) | ||
|
||
it('should be true when initial value is true', () => { | ||
const state = useKeyModifier('Alt', { initial: true }) | ||
expect(state.value).toBeTruthy() | ||
}) | ||
|
||
it('should be false when initial value is false', () => { | ||
const state = useKeyModifier('Alt', { initial: false }) | ||
expect(state.value).toBeFalsy() | ||
}) | ||
|
||
it('should be null when initial value is null', () => { | ||
const state = useKeyModifier('Alt', { initial: null }) | ||
expect(state.value).toBeFalsy() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.