From 17c091d2cb3854b2b230c35d331e7af37671c6eb Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Sun, 29 May 2022 12:31:15 -0700 Subject: [PATCH] Add two new tests --- .../src/input-control/test/index.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/components/src/input-control/test/index.js b/packages/components/src/input-control/test/index.js index fe3668db2bd052..402fffc64b75c7 100644 --- a/packages/components/src/input-control/test/index.js +++ b/packages/components/src/input-control/test/index.js @@ -123,5 +123,53 @@ describe( 'InputControl', () => { expect( input ).toHaveValue( 'Original' ); expect( spy ).toHaveBeenCalledTimes( 0 ); } ); + + it( 'should not commit value until blurred when isPressEnterToChange is true', async () => { + const user = setupUser(); + const spy = jest.fn(); + render( + spy( v ) } + isPressEnterToChange + /> + ); + const input = getInput(); + + await user.type( input, 'that was then' ); + await user.click( document.body ); + + expect( spy ).toHaveBeenCalledTimes( 1 ); + expect( spy ).toHaveBeenCalledWith( 'that was then' ); + } ); + + it( 'should commit value when blurred if value is invalid', async () => { + const user = setupUser(); + const spyChange = jest.fn(); + render( + spyChange( v ) } + // If the value contains 'now' it is not valid. + pattern="(?!.*now)^.*$" + __unstableStateReducer={ ( state, action ) => { + let { value } = state; + if ( + action.type === 'COMMIT' && + action.payload.event.type === 'blur' + ) + value = value.replace( /\bnow\b/, 'meow' ); + + return { ...state, value }; + } } + /> + ); + const input = getInput(); + + await user.type( input, ' now' ); + await user.click( document.body ); + + expect( spyChange ).toHaveBeenLastCalledWith( 'this is meow' ); + } ); } ); } );