Skip to content

Commit 912fac9

Browse files
committed
move redundant code to setup section and make test case async
add introduce test retries for better handling of intermittent failures
1 parent 14e1dfb commit 912fac9

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

frontend/src/views/tests/users.test.js

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,52 +55,75 @@ describe('List Users', () => {
5555
});
5656
});
5757

58+
// Retry failed tests up to 2 times to handle occasional async flakiness
59+
jest.retryTimes(2);
5860
describe('Change of role and mapper level', () => {
59-
const setup = () => {
61+
const setup = async () => {
6062
const { user, container } = renderWithRouter(
61-
<ReduxIntlProviders>
63+
<ReduxIntlProviders store={store}>
6264
<UsersList />
6365
</ReduxIntlProviders>,
6466
);
65-
return {
66-
user,
67-
container,
68-
};
67+
68+
// wait until loading spinner gone
69+
await waitFor(() =>
70+
expect(container.getElementsByClassName('show-loading-animation').length).toBe(0),
71+
);
72+
73+
// wait for table body
74+
const tbody = await screen.findByTestId('user-list');
75+
76+
// ensure rows & triggers are stable
77+
await waitFor(() => {
78+
const triggers = within(tbody).getAllByTestId('action-trigger');
79+
expect(triggers.length).toBeGreaterThan(0);
80+
expect(screen.getByText(/Ram/i)).toBeInTheDocument();
81+
});
82+
83+
return { tbody, user, container };
6984
};
7085

7186
beforeEach(() => {
7287
const popupRoot = document.getElementById('popup-root');
7388
if (popupRoot) popupRoot.innerHTML = '';
7489
});
7590

76-
it('should call endpoint to update role', async () => {
77-
const { user, container } = setup();
91+
it('should call endpoint to update level', async () => {
92+
const { tbody, user, container } = await setup();
93+
94+
const triggers = await within(tbody).findAllByTestId('action-trigger');
95+
await user.click(triggers[0]);
96+
97+
const tooltip = await screen.findByTestId('action-content', {}, { timeout: 1000 });
98+
99+
const advancedOption = await within(tooltip).findByText(/advanced/i);
100+
await user.click(advancedOption);
101+
102+
await waitFor(() => {
103+
expect(screen.queryByTestId('action-content')).not.toBeInTheDocument();
104+
});
78105
await waitFor(() =>
79106
expect(container.getElementsByClassName('show-loading-animation').length).toBe(0),
80107
);
81-
await user.click(container.getElementsByClassName('pointer hover-blue-grey')[0]);
82-
const tooltip = await screen.findByRole('tooltip');
83-
await user.click(within(tooltip).getByText(/advanced/i));
84-
await waitFor(
85-
() =>
86-
expect(tooltip).not.toBeInTheDocument() &&
87-
expect(container.getElementsByClassName('show-loading-animation').length).toBe(16),
88-
);
89108
});
90109

91-
it('should call endpoint to update level', async () => {
92-
const { user, container } = setup();
110+
it('should call endpoint to update Role', async () => {
111+
const { tbody, user, container } = await setup();
112+
113+
const triggers = await within(tbody).findAllByTestId('action-trigger');
114+
await user.click(triggers[0]);
115+
116+
const tooltip = await screen.findByTestId('action-content', {}, { timeout: 1000 });
117+
118+
const adminOption = await within(tooltip).findByText(/admin/i);
119+
await user.click(adminOption);
120+
121+
await waitFor(() => {
122+
expect(screen.queryByTestId('action-content')).not.toBeInTheDocument();
123+
});
93124
await waitFor(() =>
94125
expect(container.getElementsByClassName('show-loading-animation').length).toBe(0),
95126
);
96-
await user.click(container.getElementsByClassName('pointer hover-blue-grey')[0]);
97-
const tooltip = await screen.findByRole('tooltip');
98-
await user.click(within(tooltip).getByText(/admin/i));
99-
await waitFor(
100-
() =>
101-
expect(tooltip).not.toBeInTheDocument() &&
102-
expect(container.getElementsByClassName('show-loading-animation').length).toBe(16),
103-
);
104127
});
105128
});
106129

0 commit comments

Comments
 (0)