Skip to content

Commit

Permalink
test: fix LoadingButton tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Jan 15, 2024
1 parent 971de66 commit 6082f9a
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions src/generic/loading-button/LoadingButton.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React from 'react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import {
act,
fireEvent,
render,
waitFor,

Check failure on line 6 in src/generic/loading-button/LoadingButton.test.jsx

View workflow job for this annotation

GitHub Actions / tests

'waitFor' is defined but never used
} from '@testing-library/react';

import LoadingButton from '.';

Expand Down Expand Up @@ -27,8 +32,9 @@ describe('<LoadingButton />', () => {
});

it('renders the spinner correctly', async () => {
let resolver;
const longFunction = () => new Promise((resolve) => {
setTimeout(resolve, 1000);
resolver = resolve;
});
const { container, getByRole, getByText } = render(RootWrapper(longFunction));
const buttonElement = getByRole('button');
Expand All @@ -38,31 +44,10 @@ describe('<LoadingButton />', () => {
// StatefulButton only sets aria-disabled (not disabled) when the state is pending
// expect(buttonElement).toBeDisabled();
expect(buttonElement).toHaveAttribute('aria-disabled', 'true');
await waitFor(() => {
// StatefulButton only sets aria-disabled (not disabled) when the state is pending
// expect(buttonElement).toBeEnabled();
expect(buttonElement).not.toHaveAttribute('aria-disabled', 'true');
expect(container.getElementsByClassName('icon-spin').length).toBe(0);
});
});

it('renders the spinner correctly even with error', async () => {
const longFunction = () => new Promise((_resolve, reject) => {
setTimeout(reject, 1000);
});
const { container, getByRole, getByText } = render(RootWrapper(longFunction));
const buttonElement = getByRole('button');
fireEvent.click(buttonElement);
expect(container.getElementsByClassName('icon-spin').length).toBe(1);
expect(getByText(buttonTitle)).toBeInTheDocument();
// StatefulButton only sets aria-disabled (not disabled) when the state is pending
// expect(buttonElement).toBeDisabled();
expect(buttonElement).toHaveAttribute('aria-disabled', 'true');
await waitFor(() => {
// StatefulButton only sets aria-disabled (not disabled) when the state is pending
// expect(buttonElement).toBeEnabled();
expect(buttonElement).not.toHaveAttribute('aria-disabled', 'true');
expect(container.getElementsByClassName('icon-spin').length).toBe(0);
});
await act(async () => { resolver(); });

expect(buttonElement).not.toHaveAttribute('aria-disabled', 'true');
expect(container.getElementsByClassName('icon-spin').length).toBe(0);
});
});

0 comments on commit 6082f9a

Please sign in to comment.