-
-
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
0209609
commit d36914b
Showing
3 changed files
with
162 additions
and
7 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
129 changes: 129 additions & 0 deletions
129
src/goals/CharacterInventory/CharInv/CharacterDetail/tests/index.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,129 @@ | ||
import { Provider } from "react-redux"; | ||
import { | ||
ReactTestInstance, | ||
ReactTestRenderer, | ||
act, | ||
create, | ||
} from "react-test-renderer"; | ||
import configureMockStore from "redux-mock-store"; | ||
|
||
import "tests/reactI18nextMock"; | ||
|
||
import CharacterDetail from "goals/CharacterInventory/CharInv/CharacterDetail"; | ||
import { | ||
buttonIdCancel, | ||
buttonIdConfirm, | ||
buttonIdSubmit, | ||
} from "goals/CharacterInventory/CharInv/CharacterDetail/FindAndReplace"; | ||
import CharacterReplaceDialog from "goals/CharacterInventory/CharInv/CharacterDetail/FindAndReplace/CharacterReplaceDialog"; | ||
import { defaultState } from "goals/CharacterInventory/Redux/CharacterInventoryReducer"; | ||
import { StoreState } from "types"; | ||
|
||
// 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( | ||
"goals/CharacterInventory/CharInv/CharacterDetail/FindAndReplace/FindAndReplaceActions", | ||
() => ({ | ||
findAndReplace: () => mockFindAndReplace(), | ||
}) | ||
); | ||
jest.mock("types/hooks", () => { | ||
return { | ||
...jest.requireActual("types/hooks"), | ||
useAppDispatch: () => (args: any) => Promise.resolve(args), | ||
}; | ||
}); | ||
|
||
const mockClose = jest.fn(); | ||
const mockFindAndReplace = jest.fn(); | ||
|
||
let charMaster: ReactTestRenderer; | ||
|
||
const mockChar = "#"; | ||
// mockPrefix is a single character whose only appearance in the component | ||
// is in an example of a word containing the mockChar. | ||
const mockPrefix = "@"; | ||
const mockWord = mockPrefix + mockChar; | ||
const mockState: Partial<StoreState> = { | ||
characterInventoryState: { ...defaultState, allWords: [mockWord] }, | ||
}; | ||
const mockStore = configureMockStore()(mockState); | ||
|
||
async function renderCharacterDetail(): Promise<void> { | ||
await act(async () => { | ||
charMaster = create( | ||
<Provider store={mockStore}> | ||
<CharacterDetail character={mockChar} close={mockClose} /> | ||
</Provider> | ||
); | ||
}); | ||
} | ||
|
||
const hasText = (item: ReactTestInstance, text: string): boolean => { | ||
const found = item.findAll( | ||
(node) => node.children.length === 1 && node.children[0] === text | ||
); | ||
return found.length !== 0; | ||
}; | ||
|
||
beforeEach(async () => { | ||
jest.resetAllMocks(); | ||
await renderCharacterDetail(); | ||
}); | ||
|
||
describe("CharacterDetail", () => { | ||
it("renders with example word", () => { | ||
expect(hasText(charMaster.root, mockPrefix)).toBeTruthy(); | ||
}); | ||
|
||
describe("FindAndReplace", () => { | ||
it("has working dialog", async () => { | ||
const dialog = charMaster.root.findByType(CharacterReplaceDialog); | ||
const submitButton = charMaster.root.findByProps({ id: buttonIdSubmit }); | ||
const cancelButton = charMaster.root.findByProps({ id: buttonIdCancel }); | ||
const confButton = charMaster.root.findByProps({ id: buttonIdConfirm }); | ||
|
||
expect(dialog.props.open).toBeFalsy(); | ||
await act(async () => { | ||
submitButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeTruthy(); | ||
await act(async () => { | ||
cancelButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeFalsy(); | ||
await act(async () => { | ||
submitButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeTruthy(); | ||
await act(async () => { | ||
await confButton.props.onClick(); | ||
}); | ||
expect(dialog.props.open).toBeFalsy(); | ||
}); | ||
|
||
it("only submits after confirmation", async () => { | ||
const submitButton = charMaster.root.findByProps({ id: buttonIdSubmit }); | ||
const cancelButton = charMaster.root.findByProps({ id: buttonIdCancel }); | ||
const confButton = charMaster.root.findByProps({ id: buttonIdConfirm }); | ||
|
||
await act(async () => { | ||
submitButton.props.onClick(); | ||
cancelButton.props.onClick(); | ||
submitButton.props.onClick(); | ||
}); | ||
expect(mockFindAndReplace).not.toBeCalled(); | ||
await act(async () => { | ||
await confButton.props.onClick(); | ||
}); | ||
expect(mockFindAndReplace).toBeCalled(); | ||
}); | ||
}); | ||
}); |