-
Notifications
You must be signed in to change notification settings - Fork 20
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
68ad197
commit aa18049
Showing
17 changed files
with
441 additions
and
101 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
121 changes: 121 additions & 0 deletions
121
packages/web/src/components/molecules/DeleteEntityModal/DeleteEntityModal.spec.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,121 @@ | ||
import React, {FC} from 'react'; | ||
|
||
import {fireEvent, render, waitFor} from '@testing-library/react'; | ||
|
||
import DashboardContext from '@contexts/DashboardContext'; | ||
|
||
import ModalContext from '@modal/context'; | ||
|
||
import {mockDashboardContextValue, mockModalContextValue} from '@utils/mocks'; | ||
|
||
import DeleteEntityModal, {DeleteEntityModalProps} from './DeleteEntityModal'; | ||
|
||
describe('DeleteEntityModal', () => { | ||
let onDelete: jest.Mock; | ||
const onConfirm = jest.fn(); | ||
const entityLabel = 'item'; | ||
const defaultStackRoute = '/items'; | ||
const name = 'Test Item'; | ||
const idToDelete = 'test-item-id'; | ||
|
||
beforeEach(() => { | ||
onDelete = jest.fn().mockImplementation(() => Promise.resolve()); | ||
}); | ||
|
||
const DeleteEntityModalWrapper: FC<DeleteEntityModalProps> = props => ( | ||
<DashboardContext.Provider value={mockDashboardContextValue}> | ||
<ModalContext.Provider value={mockModalContextValue}> | ||
<DeleteEntityModal {...props} /> | ||
</ModalContext.Provider> | ||
</DashboardContext.Provider> | ||
); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render the modal with the correct content', async () => { | ||
const {getByTestId} = render( | ||
<DeleteEntityModalWrapper | ||
onDelete={onDelete} | ||
onConfirm={onConfirm} | ||
entityLabel={entityLabel} | ||
defaultStackRoute={defaultStackRoute} | ||
name={name} | ||
idToDelete={idToDelete} | ||
/> | ||
); | ||
|
||
expect(getByTestId('delete-entity-title')).toBeInTheDocument(); | ||
expect(getByTestId('delete-entity-subtitle')).toBeInTheDocument(); | ||
expect(getByTestId('delete-entity-input')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should disable the delete button if the entered name does not match the entity name', async () => { | ||
const {getByTestId} = render( | ||
<DeleteEntityModalWrapper | ||
onDelete={onDelete} | ||
onConfirm={onConfirm} | ||
entityLabel={entityLabel} | ||
defaultStackRoute={defaultStackRoute} | ||
name={name} | ||
idToDelete={idToDelete} | ||
/> | ||
); | ||
|
||
const input = getByTestId(`delete-entity-input`); | ||
const deleteButton = getByTestId('delete-action-btn'); | ||
|
||
fireEvent.change(input, {target: {value: 'Wrong Name'}}); | ||
expect(deleteButton).toBeDisabled(); | ||
|
||
fireEvent.change(input, {target: {value: name}}); | ||
expect(deleteButton).not.toBeDisabled(); | ||
}); | ||
|
||
it('should call onDelete when the delete button is clicked with the correct id', async () => { | ||
const {getByTestId} = render( | ||
<DeleteEntityModalWrapper | ||
onDelete={onDelete} | ||
onConfirm={onConfirm} | ||
entityLabel={entityLabel} | ||
defaultStackRoute={defaultStackRoute} | ||
name={name} | ||
idToDelete={idToDelete} | ||
/> | ||
); | ||
|
||
const input = getByTestId(`delete-entity-input`); | ||
const deleteButton = getByTestId('delete-action-btn'); | ||
|
||
fireEvent.change(input, {target: {value: name}}); | ||
fireEvent.click(deleteButton); | ||
|
||
await waitFor(() => { | ||
expect(onDelete).toHaveBeenCalledWith(idToDelete); | ||
}); | ||
}); | ||
|
||
it('should call onConfirm when the delete is successful', async () => { | ||
const {getByTestId} = render( | ||
<DeleteEntityModalWrapper | ||
onDelete={onDelete} | ||
onConfirm={onConfirm} | ||
entityLabel={entityLabel} | ||
defaultStackRoute={defaultStackRoute} | ||
name={name} | ||
idToDelete={idToDelete} | ||
/> | ||
); | ||
|
||
const input = getByTestId(`delete-entity-input`); | ||
const deleteButton = getByTestId('delete-action-btn'); | ||
|
||
fireEvent.change(input, {target: {value: name}}); | ||
fireEvent.click(deleteButton); | ||
|
||
await waitFor(() => { | ||
expect(onConfirm).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
Oops, something went wrong.