-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
2525ccd
commit 0771f05
Showing
4 changed files
with
100 additions
and
15 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
97 changes: 88 additions & 9 deletions
97
src/goals/ReviewEntries/ReviewEntriesTable/CellComponents/tests/DeleteCell.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 |
---|---|---|
@@ -1,24 +1,103 @@ | ||
import { Provider } from "react-redux"; | ||
import renderer from "react-test-renderer"; | ||
import { ReactTestRenderer, act, create } from "react-test-renderer"; | ||
import configureMockStore from "redux-mock-store"; | ||
|
||
import "tests/reactI18nextMock"; | ||
|
||
import { CancelConfirmDialog } from "components/Dialogs"; | ||
import { defaultState as reviewEntriesState } from "goals/ReviewEntries/Redux/ReviewEntriesReduxTypes"; | ||
import DeleteCell from "goals/ReviewEntries/ReviewEntriesTable/CellComponents/DeleteCell"; | ||
import DeleteCell, { | ||
buttonId, | ||
buttonIdCancel, | ||
buttonIdConfirm, | ||
} from "goals/ReviewEntries/ReviewEntriesTable/CellComponents/DeleteCell"; | ||
import mockWords from "goals/ReviewEntries/tests/WordsMock"; | ||
|
||
// Dialog uses portals, which are not supported in react-test-renderer. | ||
jest.mock("@mui/material", () => { | ||
const materialUiCore = jest.requireActual("@mui/material"); | ||
return { | ||
...jest.requireActual("@mui/material"), | ||
Dialog: materialUiCore.Container, | ||
}; | ||
}); | ||
|
||
jest.mock("backend", () => ({ | ||
deleteFrontierWord: () => mockDeleteFrontierWord(), | ||
})); | ||
jest.mock("types/hooks", () => { | ||
return { | ||
...jest.requireActual("types/hooks"), | ||
useAppDispatch: | ||
() => | ||
(...args: any[]) => | ||
Promise.resolve(args), | ||
}; | ||
}); | ||
|
||
const mockDeleteFrontierWord = jest.fn(); | ||
|
||
const mockStore = configureMockStore()({ reviewEntriesState }); | ||
const mockWord = mockWords()[0]; | ||
const buttonIdDelete = buttonId(mockWord.id); | ||
|
||
let cellHandle: ReactTestRenderer; | ||
|
||
const renderDeleteCell = async (): Promise<void> => { | ||
await act(async () => { | ||
cellHandle = create( | ||
<Provider store={mockStore}> | ||
<DeleteCell rowData={mockWord} /> | ||
</Provider> | ||
); | ||
}); | ||
}; | ||
|
||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
await renderDeleteCell(); | ||
}); | ||
|
||
describe("DeleteCell", () => { | ||
it("renders", () => { | ||
renderer.act(() => { | ||
renderer.create( | ||
<Provider store={mockStore}> | ||
<DeleteCell rowData={mockWord} /> | ||
</Provider> | ||
); | ||
it("has working dialog buttons", async () => { | ||
const dialog = cellHandle.root.findByType(CancelConfirmDialog); | ||
const deleteButton = cellHandle.root.findByProps({ id: buttonIdDelete }); | ||
const cancelButton = cellHandle.root.findByProps({ id: buttonIdCancel }); | ||
const confButton = cellHandle.root.findByProps({ id: buttonIdConfirm }); | ||
|
||
expect(dialog.props.open).toBeFalsy(); | ||
await act(async () => { | ||
deleteButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeTruthy(); | ||
await act(async () => { | ||
cancelButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeFalsy(); | ||
await act(async () => { | ||
deleteButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeTruthy(); | ||
await act(async () => { | ||
await confButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeFalsy(); | ||
}); | ||
|
||
it("only deletes after confirmation", async () => { | ||
const deleteButton = cellHandle.root.findByProps({ id: buttonIdDelete }); | ||
const cancelButton = cellHandle.root.findByProps({ id: buttonIdCancel }); | ||
const confButton = cellHandle.root.findByProps({ id: buttonIdConfirm }); | ||
|
||
await act(async () => { | ||
deleteButton.props.onClick(); | ||
cancelButton.props.onClick(); | ||
deleteButton.props.onClick(); | ||
}); | ||
expect(mockDeleteFrontierWord).not.toBeCalled(); | ||
await act(async () => { | ||
await confButton.props.onClick(); | ||
}); | ||
expect(mockDeleteFrontierWord).toBeCalled(); | ||
}); | ||
}); |
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