-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refined ux update a taxonomy by downloading and uploading
- Loading branch information
Showing
11 changed files
with
566 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import LoadingButton from '.'; | ||
|
||
const buttonTitle = 'Button Title'; | ||
|
||
const RootWrapper = (onClick) => ( | ||
<LoadingButton onClick={onClick}> | ||
{buttonTitle} | ||
</LoadingButton> | ||
); | ||
|
||
describe('<LoadingButton />', () => { | ||
it('renders the title and doesnt 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 titleElement = getByText(buttonTitle); | ||
expect(titleElement).toBeInTheDocument(); | ||
expect(() => getByTestId('button-loading-spinner')).toThrow('Unable to find an element'); | ||
}); | ||
|
||
it('renders the spinner correctly', () => { | ||
const longFunction = () => new Promise((resolve) => { | ||
setTimeout(resolve, 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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useState } from 'react'; | ||
|
||
import { | ||
Button, | ||
Spinner, | ||
Stack, | ||
} from '@edx/paragon'; | ||
|
||
const LoadingButton = ({ | ||
onClick, | ||
children, | ||
disabled, | ||
...props | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const loadingOnClick = async (e) => { | ||
if (onClick === undefined) { | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
try { | ||
await onClick(e); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button | ||
{...props} | ||
disabled={!!isLoading || disabled} | ||
onClick={loadingOnClick} | ||
> | ||
<Stack gap={2} direction="horizontal"> | ||
{children} | ||
{isLoading && <Spinner size="sm" animation="border" data-testid="button-loading-spinner" />} | ||
</Stack> | ||
</Button> | ||
); | ||
}; | ||
|
||
LoadingButton.propTypes = { | ||
...Button.propTypes, | ||
}; | ||
|
||
LoadingButton.defaultProps = { | ||
...Button.defaultProps, | ||
}; | ||
|
||
export default LoadingButton; |
Oops, something went wrong.