From 3b58867915e999b7827ce0c350eb86dba31e72b6 Mon Sep 17 00:00:00 2001 From: Joerg Bayreuther Date: Fri, 24 Jul 2020 10:25:32 +0200 Subject: [PATCH 1/5] fix: remove error for typing on non value elements --- src/__tests__/type.js | 24 ++++++++++-------------- src/type.js | 22 +++++----------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/__tests__/type.js b/src/__tests__/type.js index 04d40acf..73ab1892 100644 --- a/src/__tests__/type.js +++ b/src/__tests__/type.js @@ -707,13 +707,12 @@ test('typing an invalid input value', () => { expect(element.validity.badInput).toBe(false) }) -test('should give error if we are trying to call type on an invalid element', async () => { - const {element} = setup('
') - await expect(() => - userEvent.type(element, "I'm only a div :("), - ).rejects.toThrowErrorMatchingInlineSnapshot( - `"the current element is of type BODY and doesn't have a valid value"`, - ) +test('should not throw error if we are trying to call type on an element without a value', () => { + const {element} = setup('
') + expect.assertions(0) + return userEvent + .type(element, "I'm only a div :(") + .catch(e => expect(e).toBeUndefined()) }) test('typing on button should not alter its value', () => { @@ -752,11 +751,8 @@ test('typing on input type submit should not alter its value', () => { expect(element).toHaveValue('foo') }) -test('typing on input type file should not trigger input event', () => { - const inputHandler = jest.fn() - const {element} = setup('', { - eventHandlers: {input: inputHandler}, - }) - userEvent.type(element, 'bar') - expect(inputHandler).toHaveBeenCalledTimes(0) +test('typing on input type file should not result in an error', () => { + const {element} = setup('') + expect.assertions(0) + return userEvent.type(element, 'bar').catch(e => expect(e).toBeUndefined()) }) diff --git a/src/type.js b/src/type.js index 3c62b81a..52668fe6 100644 --- a/src/type.js +++ b/src/type.js @@ -88,16 +88,7 @@ async function typeImpl( // The focused element could change between each event, so get the currently active element each time const currentElement = () => getActiveElement(element.ownerDocument) - const currentValue = () => { - const activeElement = currentElement() - const value = activeElement.value - if (typeof value === 'undefined') { - throw new TypeError( - `the current element is of type ${activeElement.tagName} and doesn't have a valid value`, - ) - } - return value - } + const currentValue = () => currentElement().value // by default, a new element has it's selection start and end at 0 // but most of the time when people call "type", they expect it to type @@ -109,7 +100,8 @@ async function typeImpl( // explicitely start typing with the cursor at 0. Not super common. if ( currentElement().selectionStart === 0 && - currentElement().selectionEnd === 0 + currentElement().selectionEnd === 0 && + currentValue() != null ) { setSelectionRangeIfNecessary( currentElement(), @@ -215,7 +207,7 @@ function getSpecialCharCallback(remainingString) { function getTypeCallback(remainingString) { const character = remainingString[0] - const callback = createTypeCharacter(character) + const callback = context => typeCharacter(character, context) return { callback, remainingString: remainingString.slice(1), @@ -279,10 +271,6 @@ function fireInputEventIfNeeded({ return {prevValue} } -function createTypeCharacter(character) { - return context => typeCharacter(character, context) -} - function typeCharacter( char, { @@ -313,7 +301,7 @@ function typeCharacter( ...eventOverrides, }) - if (keyPressDefaultNotPrevented) { + if (keyPressDefaultNotPrevented && currentValue() != null) { let newEntry = char if (prevWasMinus) { newEntry = `-${char}` From 64f33675c8e44330765e3862e55c94a714307a39 Mon Sep 17 00:00:00 2001 From: Joerg Bayreuther Date: Fri, 24 Jul 2020 10:41:06 +0200 Subject: [PATCH 2/5] test: better names for input color tests --- src/__tests__/type-modifiers.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/__tests__/type-modifiers.js b/src/__tests__/type-modifiers.js index cf79b20e..b7b668d6 100644 --- a/src/__tests__/type-modifiers.js +++ b/src/__tests__/type-modifiers.js @@ -471,14 +471,10 @@ test('{space} on a button', () => { `) }) -test('{space} with preventDefault keydown on button', () => { - const {element, getEventSnapshot} = setup('