Skip to content

Commit b2bd837

Browse files
committed
test: update to use useEvent intead of fireEvent
1 parent c238129 commit b2bd837

5 files changed

Lines changed: 98 additions & 90 deletions

File tree

src/authz-module/components/AuthZTitle.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReactNode } from 'react';
2-
import { render, screen, fireEvent } from '@testing-library/react';
2+
import { render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
34
import AuthZTitle, { AuthZTitleProps } from './AuthZTitle';
45

56
jest.mock('react-router-dom', () => ({
@@ -58,10 +59,11 @@ describe('AuthZTitle', () => {
5859

5960
render(<AuthZTitle {...defaultProps} actions={actions} />);
6061

61-
actions.forEach(({ label, onClick }) => {
62+
actions.forEach(async ({ label, onClick }) => {
63+
const user = userEvent.setup();
6264
const button = screen.getByRole('button', { name: label });
6365
expect(button).toBeInTheDocument();
64-
fireEvent.click(button);
66+
await user.click(button);
6567
expect(onClick).toHaveBeenCalled();
6668
});
6769
});

src/authz-module/libraries-manager/components/TeamTable/components/MultipleChoiceFilter.test.tsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import React from 'react';
2-
import {
3-
render, screen, fireEvent,
4-
} from '@testing-library/react';
1+
import { render, screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
53
import MultipleChoiceFilter from './MultipleChoiceFilter';
64

75
describe('MultipleChoiceFilter', () => {
@@ -37,52 +35,57 @@ describe('MultipleChoiceFilter', () => {
3735
expect(icon).toBeInTheDocument();
3836
});
3937

40-
it('should show all filter choices when dropdown is opened', () => {
38+
it('should show all filter choices when dropdown is opened', async () => {
39+
const user = userEvent.setup();
40+
4141
render(<MultipleChoiceFilter {...defaultProps} />);
4242

43-
fireEvent.click(screen.getByRole('button'));
43+
await user.click(screen.getByRole('button'));
4444

4545
expect(screen.getByText('Option 1 (5)')).toBeInTheDocument();
4646
expect(screen.getByText('Option 2 (3)')).toBeInTheDocument();
4747
expect(screen.getByText('Option 3 (0)')).toBeInTheDocument();
4848
});
4949

50-
it('should add value to filter when checkbox is checked', () => {
50+
it('should add value to filter when checkbox is checked', async () => {
51+
const user = userEvent.setup();
5152
render(<MultipleChoiceFilter {...defaultProps} />);
5253

53-
fireEvent.click(screen.getByRole('button'));
54+
await user.click(screen.getByRole('button'));
5455

5556
const checkbox1 = screen.getByLabelText('Option 1');
56-
fireEvent.click(checkbox1);
57+
await user.click(checkbox1);
5758

5859
expect(mockSetFilter).toHaveBeenCalledWith(['option1']);
5960
});
6061

61-
it('should remove value from filter when checkbox is unchecked', () => {
62+
it('should remove value from filter when checkbox is unchecked', async () => {
63+
const user = userEvent.setup();
6264
const propsWithSelectedValue = {
6365
...defaultProps,
6466
filterValue: ['option1', 'option2'],
6567
};
6668

6769
render(<MultipleChoiceFilter {...propsWithSelectedValue} />);
6870

69-
fireEvent.click(screen.getByRole('button'));
71+
await user.click(screen.getByRole('button'));
7072

7173
const checkbox1 = screen.getByLabelText('Option 1');
72-
fireEvent.click(checkbox1);
74+
await user.click(checkbox1);
7375

7476
expect(mockSetFilter).toHaveBeenCalledWith(['option2']);
7577
});
7678

77-
it('should show checked checkboxes for pre-selected values', () => {
79+
it('should show checked checkboxes for pre-selected values', async () => {
80+
const user = userEvent.setup();
7881
const propsWithSelectedValues = {
7982
...defaultProps,
8083
filterValue: ['option1', 'option3'],
8184
};
8285

8386
render(<MultipleChoiceFilter {...propsWithSelectedValues} />);
8487

85-
fireEvent.click(screen.getByRole('button'));
88+
await user.click(screen.getByRole('button'));
8689

8790
const checkbox1 = screen.getByLabelText('Option 1');
8891
const checkbox2 = screen.getByLabelText('Option 2');
@@ -93,18 +96,19 @@ describe('MultipleChoiceFilter', () => {
9396
expect(checkbox3).toBeChecked();
9497
});
9598

96-
it('should call setFilter with correct array when adding to existing selections', () => {
99+
it('should call setFilter with correct array when adding to existing selections', async () => {
100+
const user = userEvent.setup();
97101
const propsWithExistingSelection = {
98102
...defaultProps,
99103
filterValue: ['option2'],
100104
};
101105

102106
render(<MultipleChoiceFilter {...propsWithExistingSelection} />);
103107

104-
fireEvent.click(screen.getByRole('button'));
108+
await user.click(screen.getByRole('button'));
105109

106110
const checkbox1 = screen.getByLabelText('Option 1');
107-
fireEvent.click(checkbox1);
111+
await user.click(checkbox1);
108112

109113
expect(mockSetFilter).toHaveBeenCalledWith(['option2', 'option1']);
110114
});

src/authz-module/libraries-manager/components/TeamTable/components/SearchFilter.test.tsx

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,91 @@
1-
import React from 'react';
2-
import {
3-
render, screen, fireEvent,
4-
} from '@testing-library/react';
1+
import { useState } from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
54
import SearchFilter from './SearchFilter';
65

76
describe('SearchFilter', () => {
8-
const mockSetFilter = jest.fn();
9-
10-
const defaultProps = {
11-
filterValue: '',
12-
setFilter: mockSetFilter,
13-
placeholder: 'Search placeholder',
14-
};
15-
167
beforeEach(() => {
178
jest.clearAllMocks();
189
});
1910

11+
const SearchFilterWrapper = ({
12+
initFilterValue = '', customPlaceholder = 'Search placeholder',
13+
}:{ initFilterValue?: string; customPlaceholder?:string }) => {
14+
const [filter, setFilter] = useState(initFilterValue);
15+
return (
16+
<SearchFilter
17+
filterValue={filter}
18+
setFilter={setFilter}
19+
placeholder={customPlaceholder}
20+
/>
21+
);
22+
};
23+
2024
it('should render search input with correct placeholder', () => {
21-
render(<SearchFilter {...defaultProps} />);
25+
render(<SearchFilterWrapper />);
2226

2327
const input = screen.getByPlaceholderText('Search placeholder');
2428
expect(input).toBeInTheDocument();
2529
expect(input).toHaveAttribute('type', 'text');
2630
});
2731

2832
it('should display empty value when filterValue is undefined', () => {
29-
const propsWithUndefined = { ...defaultProps, filterValue: undefined as any };
30-
render(<SearchFilter {...propsWithUndefined} />);
33+
render(<SearchFilterWrapper initFilterValue={undefined} />);
3134

3235
const input = screen.getByPlaceholderText('Search placeholder');
3336
expect(input).toHaveValue('');
3437
});
3538

3639
it('should display filterValue if provided', () => {
37-
const propsWithString = { ...defaultProps, filterValue: 'test search' };
38-
render(<SearchFilter {...propsWithString} />);
40+
render(<SearchFilterWrapper initFilterValue="test search" />);
3941

4042
const input = screen.getByPlaceholderText('Search placeholder');
4143
expect(input).toHaveValue('test search');
4244
});
4345

44-
it('should call setFilter with input value when typing', () => {
45-
render(<SearchFilter {...defaultProps} />);
46+
it('should call setFilter with input value when typing', async () => {
47+
const user = userEvent.setup();
48+
render(<SearchFilterWrapper />);
4649

4750
const input = screen.getByPlaceholderText('Search placeholder');
48-
fireEvent.change(input, { target: { value: 'new search term' } });
51+
await user.click(input);
52+
await user.type(input, 'new search term');
4953

50-
expect(mockSetFilter).toHaveBeenCalledWith('new search term');
54+
expect(input).toHaveValue('new search term');
5155
});
5256

53-
it('should call setFilter with undefined when input is cleared', () => {
54-
const propsWithValue = { ...defaultProps, filterValue: 'existing value' as any };
55-
render(<SearchFilter {...propsWithValue} />);
57+
it('should clear the input correctly', async () => {
58+
const user = userEvent.setup();
59+
render(<SearchFilterWrapper initFilterValue="existing value" />);
5660

5761
const input = screen.getByPlaceholderText('Search placeholder');
58-
fireEvent.change(input, { target: { value: '' } });
59-
60-
expect(mockSetFilter).toHaveBeenCalledWith(undefined);
62+
await user.click(input);
63+
await user.clear(input);
64+
expect(input).toHaveValue('');
6165
});
6266

63-
it('should handle multiple character input correctly', () => {
64-
render(<SearchFilter {...defaultProps} />);
67+
it('should handle multiple character input correctly', async () => {
68+
const user = userEvent.setup();
69+
70+
render(<SearchFilterWrapper />);
6571

6672
const input = screen.getByPlaceholderText('Search placeholder');
73+
await user.click(input);
6774

6875
// Type multiple characters
69-
fireEvent.change(input, { target: { value: 'a' } });
70-
expect(mockSetFilter).toHaveBeenCalledWith('a');
76+
await user.type(input, 'a');
77+
expect(input).toHaveValue('a');
7178

72-
fireEvent.change(input, { target: { value: 'ab' } });
73-
expect(mockSetFilter).toHaveBeenCalledWith('ab');
79+
await user.type(input, 'b');
80+
expect(input).toHaveValue('ab');
7481

75-
fireEvent.change(input, { target: { value: 'abc' } });
76-
expect(mockSetFilter).toHaveBeenCalledWith('abc');
82+
await user.type(input, 'c');
83+
expect(input).toHaveValue('abc');
7784
});
7885

7986
it('should handle different placeholder text', () => {
8087
const customPlaceholder = 'Enter search term here...';
81-
render(<SearchFilter {...defaultProps} placeholder={customPlaceholder} />);
88+
render(<SearchFilterWrapper customPlaceholder={customPlaceholder} />);
8289

8390
const input = screen.getByPlaceholderText(customPlaceholder);
8491
expect(input).toBeInTheDocument();

src/authz-module/libraries-manager/components/TeamTable/components/SortDropdown.test.tsx

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {
2-
render, screen, fireEvent, act,
3-
} from '@testing-library/react';
4-
import { IntlProvider } from '@edx/frontend-platform/i18n';
1+
import { screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
53
import { DataTableContext } from '@openedx/paragon';
4+
import { renderWrapper } from '@src/setupTest';
65
import SortDropdown from './SortDropdown';
76

87
jest.mock('@edx/frontend-platform/i18n', () => jest.requireActual('@edx/frontend-platform/i18n'));
@@ -28,12 +27,10 @@ describe('SortDropdown', () => {
2827
...contextOverrides,
2928
};
3029

31-
return render(
32-
<IntlProvider locale="en">
33-
<DataTableContext.Provider value={contextValue}>
34-
<SortDropdown />
35-
</DataTableContext.Provider>
36-
</IntlProvider>,
30+
return renderWrapper(
31+
<DataTableContext.Provider value={contextValue}>
32+
<SortDropdown />
33+
</DataTableContext.Provider>,
3734
);
3835
};
3936

@@ -48,11 +45,12 @@ describe('SortDropdown', () => {
4845
expect(screen.getByText('Sort')).toBeInTheDocument();
4946
});
5047

51-
it('should render all sort options when dropdown is opened', () => {
48+
it('should render all sort options when dropdown is opened', async () => {
49+
const user = userEvent.setup();
5250
renderSortDropdown();
5351

5452
const toggleButton = screen.getByRole('button');
55-
fireEvent.click(toggleButton);
53+
await user.click(toggleButton);
5654

5755
expect(screen.getByText('Name A-Z')).toBeInTheDocument();
5856
expect(screen.getByText('Name Z-A')).toBeInTheDocument();
@@ -84,30 +82,29 @@ describe('SortDropdown', () => {
8482
expect(screen.getByText('Name Z-A')).toBeInTheDocument();
8583
});
8684

87-
it('should handle sort selection and call toggleSortBy', () => {
85+
it('should handle sort selection and call toggleSortBy', async () => {
86+
const user = userEvent.setup();
8887
renderSortDropdown();
8988

9089
const toggleButton = screen.getByRole('button');
91-
fireEvent.click(toggleButton);
90+
await user.click(toggleButton);
9291

9392
const nameAZOption = screen.getByText('Name A-Z');
9493

95-
act(() => {
96-
fireEvent.click(nameAZOption);
97-
});
94+
await user.click(nameAZOption);
9895

9996
expect(mockToggleSortBy).toHaveBeenCalledWith('username', false);
10097

10198
const nameZAOption = screen.getByText('Name Z-A');
10299

103-
act(() => {
104-
fireEvent.click(nameZAOption);
105-
});
100+
await user.click(toggleButton);
101+
await user.click(nameZAOption);
106102

107103
expect(mockToggleSortBy).toHaveBeenCalledWith('username', true);
108104
});
109105

110-
it('should mark the active sort option as active', () => {
106+
it('should mark the active sort option as active', async () => {
107+
const user = userEvent.setup();
111108
const contextWithSort = {
112109
state: {
113110
...defaultDataTableState,
@@ -118,7 +115,7 @@ describe('SortDropdown', () => {
118115
renderSortDropdown(contextWithSort);
119116

120117
const toggleButton = screen.getByRole('button');
121-
fireEvent.click(toggleButton);
118+
await user.click(toggleButton);
122119

123120
// Get all elements with "Name A-Z" text and find the dropdown item
124121
const nameAZOptions = screen.getAllByText('Name A-Z');

src/authz-module/libraries-manager/components/TeamTable/components/TableControlBar.test.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {
2-
render, screen, fireEvent,
3-
} from '@testing-library/react';
4-
import { IntlProvider } from '@edx/frontend-platform/i18n';
1+
import { screen } from '@testing-library/react';
52
import {
63
DataTableContext, CheckboxFilter, TextFilter,
74
} from '@openedx/paragon';
5+
import { renderWrapper } from '@src/setupTest';
6+
import userEvent from '@testing-library/user-event';
87
import TableControlBar from './TableControlBar';
98

109
jest.mock('./MultipleChoiceFilter', () => {
@@ -61,12 +60,10 @@ describe('TableControlBar', () => {
6160
};
6261

6362
const renderWithContext = (contextValue = defaultContextValue) => (
64-
render(
65-
<IntlProvider locale="en">
66-
<DataTableContext.Provider value={contextValue}>
67-
<TableControlBar />
68-
</DataTableContext.Provider>
69-
</IntlProvider>,
63+
renderWrapper(
64+
<DataTableContext.Provider value={contextValue}>
65+
<TableControlBar />
66+
</DataTableContext.Provider>,
7067
)
7168
);
7269

@@ -101,7 +98,8 @@ describe('TableControlBar', () => {
10198
expect(screen.getByText('Clear filters')).toBeInTheDocument();
10299
});
103100

104-
it('should call setAllFilters with empty array when Clear filters is clicked', () => {
101+
it('should call setAllFilters with empty array when Clear filters is clicked', async () => {
102+
const user = userEvent.setup();
105103
const contextWithFilters = {
106104
...defaultContextValue,
107105
state: {
@@ -112,7 +110,7 @@ describe('TableControlBar', () => {
112110
renderWithContext(contextWithFilters);
113111

114112
const clearButton = screen.getByText('Clear filters');
115-
fireEvent.click(clearButton);
113+
await user.click(clearButton);
116114

117115
expect(mockSetAllFilters).toHaveBeenCalledWith([]);
118116
});

0 commit comments

Comments
 (0)