diff --git a/src/generic/loading-button/LoadingButton.test.jsx b/src/generic/loading-button/LoadingButton.test.jsx index d3d54f6a89..b2dede6490 100644 --- a/src/generic/loading-button/LoadingButton.test.jsx +++ b/src/generic/loading-button/LoadingButton.test.jsx @@ -12,18 +12,21 @@ const RootWrapper = (onClick) => ( ); describe('', () => { - 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', () => { @@ -43,4 +46,22 @@ describe('', () => { 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); + }); }); diff --git a/src/generic/loading-button/index.jsx b/src/generic/loading-button/index.jsx index b092b5c792..7e5a39860b 100644 --- a/src/generic/loading-button/index.jsx +++ b/src/generic/loading-button/index.jsx @@ -15,7 +15,7 @@ const LoadingButton = ({ const [isLoading, setIsLoading] = useState(false); const loadingOnClick = async (e) => { - if (onClick === undefined) { + if (!onClick) { return; }