-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6c2aa4
commit 4255f88
Showing
9 changed files
with
321 additions
and
8 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
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
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
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
86 changes: 86 additions & 0 deletions
86
src/filtering/deleteFavouriteFilterDialogue.component.test.tsx
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,86 @@ | ||
import { RenderResult, screen, waitFor } from '@testing-library/react'; | ||
import userEvent, { UserEvent } from '@testing-library/user-event'; | ||
import favouriteFiltersJson from '../mocks/favouriteFilters.json'; | ||
import { renderComponentWithProviders } from '../testUtils'; | ||
import DeleteFavouriteFilterDialogue, { | ||
DeleteFavouriteFilterDialogueProps, | ||
} from './deleteFavouriteFilterDialogue.component'; | ||
|
||
describe('delete favourite filter dialogue', () => { | ||
let props: DeleteFavouriteFilterDialogueProps; | ||
let user: UserEvent; | ||
const onClose = vi.fn(); | ||
|
||
const createView = (): RenderResult => { | ||
return renderComponentWithProviders( | ||
<DeleteFavouriteFilterDialogue {...props} /> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
props = { | ||
open: true, | ||
onClose: onClose, | ||
favouriteFilter: favouriteFiltersJson[0], | ||
}; | ||
user = userEvent.setup(); | ||
}); | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
it('renders correctly', async () => { | ||
createView(); | ||
expect(screen.getByText('Delete Favourite filter')).toBeInTheDocument(); | ||
expect( | ||
screen.getByTestId('delete-favourite-filter-name') | ||
).toHaveTextContent('test'); | ||
}); | ||
|
||
it('calls onClose when Close button is clicked', async () => { | ||
createView(); | ||
const closeButton = screen.getByRole('button', { name: 'Close' }); | ||
await user.click(closeButton); | ||
|
||
await waitFor(() => { | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('displays warning message when favourite filter data is not loaded', async () => { | ||
props = { | ||
...props, | ||
favouriteFilter: undefined, | ||
}; | ||
createView(); | ||
const continueButton = screen.getByRole('button', { name: 'Continue' }); | ||
await user.click(continueButton); | ||
const helperTexts = screen.getByText( | ||
'No data provided, Please refresh and try again' | ||
); | ||
expect(helperTexts).toBeInTheDocument(); | ||
expect(onClose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('displays warning message when api errors', async () => { | ||
props = { | ||
...props, | ||
favouriteFilter: favouriteFiltersJson[2], | ||
}; | ||
createView(); | ||
const continueButton = screen.getByRole('button', { name: 'Continue' }); | ||
await user.click(continueButton); | ||
const helperTexts = screen.getByText('error'); | ||
expect(helperTexts).toBeInTheDocument(); | ||
expect(onClose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls handleDeleteFavouriteFilter when continue button is clicked with a valid favourite filter name', async () => { | ||
createView(); | ||
const continueButton = screen.getByRole('button', { name: 'Continue' }); | ||
await user.click(continueButton); | ||
|
||
await waitFor(() => { | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,91 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
FormHelperText, | ||
} from '@mui/material'; | ||
import React, { useState } from 'react'; | ||
|
||
import { AxiosError } from 'axios'; | ||
import { useDeleteFavouriteFilter } from '../api/favouriteFilters'; | ||
import { FavouriteFilter } from '../app.types'; | ||
|
||
export interface DeleteFavouriteFilterDialogueProps { | ||
open: boolean; | ||
onClose: () => void; | ||
favouriteFilter: FavouriteFilter | undefined; | ||
} | ||
|
||
const DeleteFavouriteFilterDialogue = ( | ||
props: DeleteFavouriteFilterDialogueProps | ||
) => { | ||
const { open, onClose, favouriteFilter } = props; | ||
|
||
const [error, setError] = useState(false); | ||
const [errorMessage, setErrorMessage] = React.useState<string | undefined>( | ||
undefined | ||
); | ||
|
||
const handleClose = React.useCallback(() => { | ||
onClose(); | ||
setError(false); | ||
setErrorMessage(undefined); | ||
}, [onClose]); | ||
|
||
const { mutateAsync: deleteFavouriteFilter } = useDeleteFavouriteFilter(); | ||
|
||
const handleDeleteFavouriteFilter = React.useCallback(() => { | ||
if (favouriteFilter) { | ||
deleteFavouriteFilter(favouriteFilter._id) | ||
.then(() => { | ||
handleClose(); | ||
}) | ||
.catch((error: AxiosError) => { | ||
setError(true); | ||
setErrorMessage((error.response?.data as { detail: string }).detail); | ||
}); | ||
} else { | ||
setError(true); | ||
setErrorMessage('No data provided, Please refresh and try again'); | ||
} | ||
}, [deleteFavouriteFilter, handleClose, favouriteFilter]); | ||
|
||
return ( | ||
<Dialog open={open} onClose={handleClose} maxWidth="lg"> | ||
<DialogTitle>Delete Favourite filter</DialogTitle> | ||
<DialogContent> | ||
Are you sure you want to delete{' '} | ||
<strong data-testid="delete-favourite-filter-name"> | ||
{favouriteFilter?.name} | ||
</strong> | ||
? | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={handleClose}>Close</Button> | ||
<Button disabled={error} onClick={handleDeleteFavouriteFilter}> | ||
Continue | ||
</Button> | ||
</DialogActions> | ||
{error && ( | ||
<Box | ||
sx={{ | ||
mx: 3, | ||
marginBottom: 3, | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}} | ||
> | ||
<FormHelperText sx={{ textAlign: 'center' }} error> | ||
{errorMessage} | ||
</FormHelperText> | ||
</Box> | ||
)} | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default DeleteFavouriteFilterDialogue; |
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
Oops, something went wrong.