From ecb10149b1c82c677bec25a0e150aa847c57c2c1 Mon Sep 17 00:00:00 2001 From: "MD. MOHIBUR RAHMAN" <35300157+mrpmohiburrahman@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:15:01 +0600 Subject: [PATCH] test: add unit testing for skipBlur --- .../type/__tests__/type-managed.test.tsx | 27 +++++++++++++++++ src/user-event/type/__tests__/type.test.tsx | 30 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/user-event/type/__tests__/type-managed.test.tsx b/src/user-event/type/__tests__/type-managed.test.tsx index 37e9f03c7..6b1f45c9f 100644 --- a/src/user-event/type/__tests__/type-managed.test.tsx +++ b/src/user-event/type/__tests__/type-managed.test.tsx @@ -110,4 +110,31 @@ describe('type() for managed TextInput', () => { expect(events).toMatchSnapshot('input: "ABC", value: "XXX"'); }); + + it('skips blur and endEditing events when `skipBlur: true` in managed TextInput', async () => { + const { events, logEvent } = createEventLogger(); + render(); + + const user = userEvent.setup(); + await user.type(screen.getByTestId('input'), 'a', { + skipBlur: true, + }); + + const eventNames = getEventsNames(events); + + // Ensure 'endEditing' and 'blur' are not present + expect(eventNames).not.toContain('endEditing'); + expect(eventNames).not.toContain('blur'); + + // Verify the exact events that should be present + expect(eventNames).toEqual([ + 'pressIn', + 'focus', + 'pressOut', + 'keyPress', + 'change', + 'changeText', + 'selectionChange', + ]); + }); }); diff --git a/src/user-event/type/__tests__/type.test.tsx b/src/user-event/type/__tests__/type.test.tsx index 5753f4168..474216f58 100644 --- a/src/user-event/type/__tests__/type.test.tsx +++ b/src/user-event/type/__tests__/type.test.tsx @@ -386,4 +386,34 @@ describe('type()', () => { await user.type(input, ' World'); expect(input).toHaveDisplayValue('Hello World'); }); + + it('skips blur and endEditing events when `skipBlur: true`', async () => { + const { events } = renderTextInputWithToolkit(); + + const user = userEvent.setup(); + await user.type(screen.getByTestId('input'), 'a', { + skipBlur: true, + }); + + const eventNames = getEventsNames(events); + + // Ensure 'endEditing' and 'blur' are not present + expect(eventNames).not.toContain('endEditing'); + expect(eventNames).not.toContain('blur'); + + // Verify the exact events that should be present + expect(eventNames).toEqual([ + 'pressIn', + 'focus', + 'pressOut', + 'keyPress', + 'change', + 'changeText', + 'selectionChange', + ]); + + expect(lastEventPayload(events, 'selectionChange')).toMatchObject({ + nativeEvent: { selection: { start: 1, end: 1 } }, + }); + }); });