Skip to content

Commit

Permalink
test: improve LoadingButton tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Dec 8, 2023
1 parent b63147a commit 3613f22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/generic/loading-button/LoadingButton.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ const RootWrapper = (onClick) => (
);

describe('<LoadingButton />', () => {
it('renders the title and doesnt the spinner initially', () => {
const { getByText, getByTestId } = render(RootWrapper());
it('renders the title and doesnt handle the spinner initially', () => {
const { getByText, getByTestId } = render(RootWrapper(() => { }));
const titleElement = getByText(buttonTitle);
expect(titleElement).toBeInTheDocument();
expect(() => getByTestId('button-loading-spinner')).toThrow('Unable to find an element');
});

it('doesnt render the spinner initially without onClick function', () => {
const { getByText, getByTestId } = render(RootWrapper());
const { getByRole, getByText, getByTestId } = render(RootWrapper());
const titleElement = getByText(buttonTitle);
expect(titleElement).toBeInTheDocument();
expect(() => getByTestId('button-loading-spinner')).toThrow('Unable to find an element');

const buttonElement = getByRole('button');
expect(buttonElement.click()).not.toThrow();
});

it('renders the spinner correctly', () => {
Expand All @@ -43,4 +46,22 @@ describe('<LoadingButton />', () => {
expect(spinnerElement).not.toBeInTheDocument();
}, 2000);
});

it('renders the spinner correctly even with error', () => {
const longFunction = () => new Promise((resolve, reject) => {
setTimeout(reject, 1000);
});
const { getByRole, getByText, getByTestId } = render(RootWrapper(longFunction));
const buttonElement = getByRole('button');
buttonElement.click();
const spinnerElement = getByTestId('button-loading-spinner');
expect(spinnerElement).toBeInTheDocument();
const titleElement = getByText(buttonTitle);
expect(titleElement).toBeInTheDocument();
expect(buttonElement).toBeDisabled();
setTimeout(() => {
expect(buttonElement).toBeEnabled();
expect(spinnerElement).not.toBeInTheDocument();
}, 2000);
});
});
2 changes: 1 addition & 1 deletion src/generic/loading-button/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const LoadingButton = ({
const [isLoading, setIsLoading] = useState(false);

const loadingOnClick = async (e) => {
if (onClick === undefined) {
if (!onClick) {
return;
}

Expand Down

0 comments on commit 3613f22

Please sign in to comment.