Skip to content

Commit

Permalink
test: add unit testing for skipBlur
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpmohiburrahman authored and mdjastrzebski committed Nov 27, 2024
1 parent c8ab9f9 commit ecb1014
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/user-event/type/__tests__/type-managed.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ManagedTextInput logEvent={logEvent} />);

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',
]);
});
});
30 changes: 30 additions & 0 deletions src/user-event/type/__tests__/type.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
});
});
});

0 comments on commit ecb1014

Please # to comment.