-
-
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.
[DataEntry > NewEntry] Prevent double submission (#2946)
- Loading branch information
1 parent
8677250
commit 6ea0515
Showing
2 changed files
with
187 additions
and
40 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
202 changes: 168 additions & 34 deletions
202
src/components/DataEntry/DataEntryTable/NewEntry/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 |
---|---|---|
@@ -1,50 +1,184 @@ | ||
import { createRef } from "react"; | ||
import { type ReactElement, createRef } from "react"; | ||
import { Provider } from "react-redux"; | ||
import renderer from "react-test-renderer"; | ||
import { type ReactTestRenderer, act, create } from "react-test-renderer"; | ||
import configureMockStore from "redux-mock-store"; | ||
|
||
import "tests/reactI18nextMock"; | ||
|
||
import NewEntry from "components/DataEntry/DataEntryTable/NewEntry"; | ||
import { | ||
GlossWithSuggestions, | ||
VernWithSuggestions, | ||
} from "components/DataEntry/DataEntryTable/EntryCellComponents"; | ||
import NewEntry, { | ||
NewEntryId, | ||
} from "components/DataEntry/DataEntryTable/NewEntry"; | ||
import { newWritingSystem } from "types/writingSystem"; | ||
|
||
jest.mock("@mui/material/Autocomplete", () => "div"); | ||
jest.mock( | ||
"@mui/material/Autocomplete", | ||
() => (props: any) => mockAutocomplete(props) | ||
); | ||
|
||
jest.mock("components/Pronunciations/PronunciationsFrontend", () => "div"); | ||
|
||
/** Bypass the Autocomplete and render its internal input with the props of both. */ | ||
const mockAutocomplete = (props: { | ||
renderInput: (params: any) => ReactElement; | ||
}): ReactElement => { | ||
const { renderInput, ...params } = props; | ||
return renderInput(params); | ||
}; | ||
|
||
const mockAddNewAudio = jest.fn(); | ||
const mockAddNewEntry = jest.fn(); | ||
const mockDelNewAudio = jest.fn(); | ||
const mockSetNewGloss = jest.fn(); | ||
const mockSetNewNote = jest.fn(); | ||
const mockSetNewVern = jest.fn(); | ||
const mockSetSelectedDup = jest.fn(); | ||
const mockSetSelectedSense = jest.fn(); | ||
const mockRepNewAudio = jest.fn(); | ||
const mockResetNewEntry = jest.fn(); | ||
const mockUpdateWordWithNewGloss = jest.fn(); | ||
|
||
const mockStore = configureMockStore()({ treeViewState: { open: false } }); | ||
|
||
let renderer: ReactTestRenderer; | ||
|
||
const renderNewEntry = async ( | ||
vern = "", | ||
gloss = "", | ||
note = "" | ||
): Promise<void> => { | ||
await act(async () => { | ||
renderer = create( | ||
<Provider store={mockStore}> | ||
<NewEntry | ||
analysisLang={newWritingSystem()} | ||
vernacularLang={newWritingSystem()} | ||
// Parent component handles new entry state: | ||
addNewEntry={mockAddNewEntry} | ||
resetNewEntry={mockResetNewEntry} | ||
updateWordWithNewGloss={mockUpdateWordWithNewGloss} | ||
newAudio={[]} | ||
addNewAudio={mockAddNewAudio} | ||
delNewAudio={mockDelNewAudio} | ||
repNewAudio={mockRepNewAudio} | ||
newGloss={gloss} | ||
setNewGloss={mockSetNewGloss} | ||
newNote={note} | ||
setNewNote={mockSetNewNote} | ||
newVern={vern} | ||
setNewVern={mockSetNewVern} | ||
vernInput={createRef<HTMLInputElement>()} | ||
// Parent component handles vern suggestion state: | ||
setSelectedDup={mockSetSelectedDup} | ||
setSelectedSense={mockSetSelectedSense} | ||
suggestedVerns={[]} | ||
suggestedDups={[]} | ||
/> | ||
</Provider> | ||
); | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe("NewEntry", () => { | ||
it("renders without crashing", () => { | ||
renderer.act(() => { | ||
renderer.create( | ||
<Provider store={mockStore}> | ||
<NewEntry | ||
analysisLang={newWritingSystem()} | ||
vernacularLang={newWritingSystem()} | ||
// Parent component handles new entry state: | ||
addNewEntry={jest.fn()} | ||
resetNewEntry={jest.fn()} | ||
updateWordWithNewGloss={jest.fn()} | ||
newAudio={[]} | ||
addNewAudio={jest.fn()} | ||
delNewAudio={jest.fn()} | ||
repNewAudio={jest.fn()} | ||
newGloss={""} | ||
setNewGloss={jest.fn()} | ||
newNote={""} | ||
setNewNote={jest.fn()} | ||
newVern={""} | ||
setNewVern={jest.fn()} | ||
vernInput={createRef<HTMLInputElement>()} | ||
// Parent component handles vern suggestion state: | ||
setSelectedDup={jest.fn()} | ||
setSelectedSense={jest.fn()} | ||
suggestedVerns={[]} | ||
suggestedDups={[]} | ||
/> | ||
</Provider> | ||
); | ||
it("does not submit without a vernacular", async () => { | ||
await renderNewEntry("", "gloss"); | ||
await act(async () => { | ||
renderer.root.findByType(GlossWithSuggestions).props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not submit with vernacular Enter if gloss is empty", async () => { | ||
await renderNewEntry("vern", ""); | ||
await act(async () => { | ||
renderer.root.findByType(VernWithSuggestions).props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("does submit with gloss Enter if gloss is empty", async () => { | ||
await renderNewEntry("vern", ""); | ||
await act(async () => { | ||
renderer.root.findByType(GlossWithSuggestions).props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("resets when the delete button is clicked", async () => { | ||
await renderNewEntry(); | ||
expect(mockResetNewEntry).not.toHaveBeenCalled(); | ||
await act(async () => { | ||
renderer.root | ||
.findByProps({ id: NewEntryId.ButtonDelete }) | ||
.props.onClick(); | ||
}); | ||
expect(mockResetNewEntry).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("resets new entry after awaiting add", async () => { | ||
await renderNewEntry("vern", "gloss"); | ||
|
||
// Use a mock timer to control when addNewEntry completes | ||
jest.useFakeTimers(); | ||
mockAddNewEntry.mockImplementation( | ||
async () => await new Promise((res) => setTimeout(res, 1000)) | ||
); | ||
|
||
// Submit a new entry | ||
await act(async () => { | ||
renderer.root.findByType(GlossWithSuggestions).props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
expect(mockResetNewEntry).not.toHaveBeenCalled(); | ||
|
||
// Run the timers and confirm a reset | ||
await act(async () => { | ||
jest.runAllTimers(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
expect(mockResetNewEntry).toHaveBeenCalledTimes(1); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
|
||
it("doesn't allow double submission", async () => { | ||
await renderNewEntry("vern", "gloss"); | ||
|
||
// Use a mock timer to control when addNewEntry completes | ||
jest.useFakeTimers(); | ||
mockAddNewEntry.mockImplementation( | ||
async () => await new Promise((res) => setTimeout(res, 1000)) | ||
); | ||
|
||
// Submit a new entry | ||
const gloss = renderer.root.findByType(GlossWithSuggestions); | ||
await act(async () => { | ||
gloss.props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
expect(mockResetNewEntry).not.toHaveBeenCalled(); | ||
|
||
// Attempt a second submission before the first one completes | ||
await act(async () => { | ||
gloss.props.handleEnter(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
expect(mockResetNewEntry).not.toHaveBeenCalled(); | ||
|
||
// Run the timers and confirm no second submission | ||
await act(async () => { | ||
jest.runAllTimers(); | ||
}); | ||
expect(mockAddNewEntry).toHaveBeenCalledTimes(1); | ||
expect(mockResetNewEntry).toHaveBeenCalledTimes(1); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
}); |