Skip to content

Commit

Permalink
test(navigation-key): in case an invalid key is passed to the factory…
Browse files Browse the repository at this point in the history
…, throw an error
  • Loading branch information
Jesu Castillo committed Nov 1, 2020
1 parent 357be70 commit 45445c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/type.js
@@ -1,4 +1,5 @@
import userEvent from '../'
import {navigationKey} from '../keys/navigation-key'
import {setup, addListeners} from './helpers/utils'
import './helpers/custom-element'

Expand Down Expand Up @@ -1096,3 +1097,9 @@ test('navigation key: {arrowleft} and {arrowright} moves the cursor', () => {
input[value="Brekafast"] - keyup: t (116)
`)
})

test('navigationKey throws if an invalid key is passed', () => {
expect(() => {
navigationKey('SOME_INVALID_KEY')
}).toThrowError(TypeError)
})
7 changes: 7 additions & 0 deletions src/keys/navigation-key.js
Expand Up @@ -21,6 +21,13 @@ function getSelectionRange(currentElement, key) {
}

function navigationKey(key) {
if (!keys[key]) {
throw new TypeError(
`Invalid parameter key, navigationKey expects one of: "${Object.keys(
keys,
).join('", "')}" but received "${key}" instead`,
)
}
const event = {
key,
keyCode: keys[key].keyCode,
Expand Down

0 comments on commit 45445c3

Please sign in to comment.