Skip to content

Commit

Permalink
fix(type): catch internal promise rejections (#635)
Browse files Browse the repository at this point in the history
* fix(type): catch internal promise rejections

* fix: improve error message for missing character

* test: fix tests in CI
  • Loading branch information
ph-fritsche authored Mar 30, 2021
1 parent 54a10f5 commit 6ae18e3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/keyboard/getNextKeyDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,24 @@ cases(
},
},
)

cases(
'errors',
({text, expectedError}) => {
expect(() => getNextKeyDef(`${text}`, options)).toThrow(expectedError)
},
{
'invalid descriptor': {
text: '{!}',
expectedError: 'Expected key descriptor but found "!" in "{!}"',
},
'missing descriptor': {
text: '',
expectedError: 'Expected key descriptor but found "" in ""',
},
'missing closing bracket': {
text: '{a)',
expectedError: 'Expected closing bracket but found ")" in "{a)"',
},
},
)
29 changes: 29 additions & 0 deletions src/__tests__/type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import userEvent from '../'
import {wait} from '../utils'
import {setup, addListeners} from './helpers/utils'
import './helpers/custom-element'

Expand Down Expand Up @@ -1408,3 +1409,31 @@ test('overwrite selection with same value', () => {

expect(element).toHaveValue('11123')
})

describe('promise rejections', () => {
afterEach(() => {
console.error.mockReset()
})

test.each([
['foo', '[{', 'Unable to find the "window"'],
[document.body, '[{', 'Expected key descriptor but found "{"'],
])(
'catch promise rejections and report to the console on synchronous calls',
async (element, text, errorMessage) => {
const errLog = jest
.spyOn(console, 'error')
.mockImplementationOnce(() => {})

userEvent.type(element, text)

await wait(1)

expect(errLog).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(errorMessage),
}),
)
},
)
})
20 changes: 14 additions & 6 deletions src/keyboard/getNextKeyDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export function getNextKeyDef(
: text.slice(descriptorStart).match(/^\w+/)?.[0]
: text[descriptorStart]

// istanbul ignore if
if (!descriptor) {
throw new Error(
`Expected key descriptor but found "${text[descriptorStart]}" in "${text}"`,
getErrorMessage('key descriptor', text[descriptorStart], text),
)
}

Expand All @@ -50,12 +49,13 @@ export function getNextKeyDef(
? '}'
: ']'

// istanbul ignore if
if (endBracket && text[descriptorEnd + endModifier.length] !== endBracket) {
throw new Error(
`Expected closing bracket but found "${
text[descriptorEnd + endModifier.length]
}" in "${text}"`,
getErrorMessage(
'closing bracket',
text[descriptorEnd + endModifier.length],
text,
),
)
}

Expand Down Expand Up @@ -135,3 +135,11 @@ function isPrintableCharacter(startBracket: string, descriptor: string) {
(startBracket === '{' && descriptor.length === 1)
)
}

function getErrorMessage(
expected: string,
found: string | undefined,
text: string,
) {
return `Expected ${expected} but found "${found ?? ''}" in "${text}"`
}
10 changes: 7 additions & 3 deletions src/type/typeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export async function typeImplementation(

const {selectionStart, selectionEnd} = getSelectionRange(element)

if (value != null &&
(selectionStart === null || selectionStart === 0) &&
(selectionEnd === null || selectionEnd === 0)
if (
value != null &&
(selectionStart === null || selectionStart === 0) &&
(selectionEnd === null || selectionEnd === 0)
) {
setSelectionRange(
currentElement() as Element,
Expand All @@ -70,4 +71,7 @@ export async function typeImplementation(
if (!skipAutoClose) {
releaseAllKeys()
}

// eslint-disable-next-line consistent-return -- we need to return the internal Promise so that it is catchable if we don't await
return promise
}

0 comments on commit 6ae18e3

Please sign in to comment.