diff --git a/src/components/App/DefaultState.ts b/src/components/App/DefaultState.ts index bd02c8bf73..25186a03dd 100644 --- a/src/components/App/DefaultState.ts +++ b/src/components/App/DefaultState.ts @@ -5,7 +5,7 @@ import { defaultState as exportProjectState } from "components/ProjectExport/Red import { defaultState as pronunciationsState } from "components/Pronunciations/Redux/PronunciationsReduxTypes"; import { defaultState as treeViewState } from "components/TreeView/Redux/TreeViewReduxTypes"; import { defaultState as characterInventoryState } from "goals/CharacterInventory/Redux/CharacterInventoryReduxTypes"; -import { defaultState as mergeDuplicateGoal } from "goals/MergeDuplicates/Redux/MergeDupsReducer"; +import { defaultState as mergeDuplicateGoal } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { defaultState as reviewEntriesState } from "goals/ReviewEntries/Redux/ReviewEntriesReduxTypes"; import { defaultState as analyticsState } from "types/Redux/analyticsReduxTypes"; diff --git a/src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/tests/index.test.tsx b/src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/tests/index.test.tsx index 82735cbdb2..b35f4eb8b3 100644 --- a/src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/tests/index.test.tsx +++ b/src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/tests/index.test.tsx @@ -1,21 +1,24 @@ import { IconButton } from "@mui/material"; import { Provider } from "react-redux"; -import { ReactTestRenderer, act, create } from "react-test-renderer"; +import { type ReactTestRenderer, act, create } from "react-test-renderer"; import configureMockStore from "redux-mock-store"; import "tests/reactI18nextMock"; -import { GramCatGroup, Sense } from "api/models"; +import { GramCatGroup, type Sense } from "api/models"; import { defaultState } from "components/App/DefaultState"; import MergeDragDrop from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop"; import DragSense from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/DragSense"; import DropWord from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/DropWord"; import { convertSenseToMergeTreeSense, - defaultSidebar, + defaultTree, newMergeTreeWord, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; -import { MergeTreeState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; +import { + type MergeTreeState, + defaultState as mergeState, +} from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { newSemanticDomain } from "types/semanticDomain"; import { newDefinition, @@ -90,6 +93,7 @@ const wordFoo2 = { // vern: foo // senses: bar, baz const mockTwoWordState = (): MergeTreeState => ({ + ...mergeState, data: { senses: { [senseBah.guid]: convertSenseToMergeTreeSense(senseBah, wordFoo1.id, 0), @@ -100,7 +104,7 @@ const mockTwoWordState = (): MergeTreeState => ({ words: { [wordFoo1.id]: wordFoo1, [wordFoo2.id]: wordFoo2 }, }, tree: { - sidebar: defaultSidebar, + ...defaultTree, words: { [wordFoo1.id]: newMergeTreeWord(wordFoo1.vernacular, { word1_senseA: [senseBah.guid, senseBaj.guid], @@ -111,7 +115,6 @@ const mockTwoWordState = (): MergeTreeState => ({ }), }, }, - mergeWords: [], }); const renderMergeDragDrop = async ( diff --git a/src/goals/MergeDuplicates/MergeDupsTreeTypes.ts b/src/goals/MergeDuplicates/MergeDupsTreeTypes.ts index 09c03f3823..ce1dbf2dc7 100644 --- a/src/goals/MergeDuplicates/MergeDupsTreeTypes.ts +++ b/src/goals/MergeDuplicates/MergeDupsTreeTypes.ts @@ -1,7 +1,7 @@ import { v4 } from "uuid"; -import { Flag, Sense, Status, Word } from "api/models"; -import { Hash } from "types/hash"; +import { type Flag, type Sense, Status, type Word } from "api/models"; +import { type Hash } from "types/hash"; import { newFlag, newSense } from "types/word"; export interface MergeTreeSense { @@ -16,6 +16,8 @@ export interface MergeData { senses: Hash; } +export const defaultData: MergeData = { words: {}, senses: {} }; + export interface MergeTreeReference { wordId: string; mergeSenseId: string; diff --git a/src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts b/src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts index 27bd01e34d..55de7981f0 100644 --- a/src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts +++ b/src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts @@ -3,34 +3,27 @@ import { v4 } from "uuid"; import { GramCatGroup, - MergeSourceWord, - MergeWords, + type MergeSourceWord, + type MergeWords, Status, - Word, + type Word, } from "api/models"; import { + type MergeData, + type MergeTreeSense, + type MergeTreeWord, convertSenseToMergeTreeSense, convertWordToMergeTreeWord, defaultSidebar, defaultTree, - MergeData, - MergeTreeSense, - MergeTreeWord, newMergeTreeWord, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; import { newMergeWords } from "goals/MergeDuplicates/MergeDupsTypes"; -import { MergeTreeState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; +import { defaultState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { StoreActionTypes } from "rootActions"; -import { Hash } from "types/hash"; +import { type Hash } from "types/hash"; import { compareFlags } from "utilities/wordUtilities"; -const defaultData = { words: {}, senses: {} }; -export const defaultState: MergeTreeState = { - data: defaultData, - tree: defaultTree, - mergeWords: [], -}; - const mergeDuplicatesSlice = createSlice({ name: "mergeDupStepReducer", initialState: defaultState, @@ -268,8 +261,8 @@ const mergeDuplicatesSlice = createSlice({ }); wordsTree[word.id] = convertWordToMergeTreeWord(word); }); - state.tree.words = wordsTree; state.data = { senses, words }; + state.tree = { ...defaultTree, words: wordsTree }; state.mergeWords = []; } }, diff --git a/src/goals/MergeDuplicates/Redux/MergeDupsReduxTypes.ts b/src/goals/MergeDuplicates/Redux/MergeDupsReduxTypes.ts index 73ab2148b3..3633741662 100644 --- a/src/goals/MergeDuplicates/Redux/MergeDupsReduxTypes.ts +++ b/src/goals/MergeDuplicates/Redux/MergeDupsReduxTypes.ts @@ -1,10 +1,28 @@ -import { Flag, MergeWords } from "api/models"; +import { type Flag, type MergeWords } from "api/models"; import { - MergeData, - MergeTree, - MergeTreeReference, + type MergeData, + type MergeTree, + type MergeTreeReference, + defaultData, + defaultTree, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; +// Redux state + +export interface MergeTreeState { + data: MergeData; + tree: MergeTree; + mergeWords: MergeWords[]; +} + +export const defaultState: MergeTreeState = { + data: defaultData, + tree: defaultTree, + mergeWords: [], +}; + +// Action payloads + export interface CombineSenseMergePayload { src: MergeTreeReference; dest: MergeTreeReference; @@ -15,12 +33,6 @@ export interface FlagWordPayload { flag: Flag; } -export interface MergeTreeState { - data: MergeData; - tree: MergeTree; - mergeWords: MergeWords[]; -} - export interface MoveSensePayload extends OrderSensePayload { destWordId: string; } diff --git a/src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx b/src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx index 8032e10d1f..31e36f5c1d 100644 --- a/src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx +++ b/src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx @@ -1,9 +1,9 @@ -import { MergeWords, Sense, Status, Word } from "api/models"; +import { type MergeWords, type Sense, Status, type Word } from "api/models"; import { defaultState } from "components/App/DefaultState"; import { + type MergeData, + type MergeTree, defaultTree, - MergeData, - MergeTree, newMergeTreeSense, newMergeTreeWord, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; @@ -14,6 +14,7 @@ import { mergeAll, setData, } from "goals/MergeDuplicates/Redux/MergeDupsActions"; +import { defaultState as defaultMergeState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { goalDataMock } from "goals/MergeDuplicates/Redux/tests/MergeDupsDataMock"; import { setupStore } from "store"; import { GoalType } from "types/goals"; @@ -78,14 +79,15 @@ const S1 = senses["S1"].guid; const S2 = senses["S2"].guid; const S3 = senses["S3"].guid; const S4 = senses["S4"].guid; -const data: MergeData = { words: { WA: wordA, WB: wordB }, senses: {} }; -data.senses[S1] = { - ...newMergeTreeSense("S1", idA, 0, S1), - protected: true, +const data: MergeData = { + words: { WA: wordA, WB: wordB }, + senses: { + [S1]: { ...newMergeTreeSense("S1", idA, 0, S1), protected: true }, + [S2]: newMergeTreeSense("S2", idA, 1, S2), + [S3]: newMergeTreeSense("S3", idB, 0, S3), + [S4]: newMergeTreeSense("S4", idB, 1, S4), + }, }; -data.senses[S2] = newMergeTreeSense("S2", idA, 1, S2); -data.senses[S3] = newMergeTreeSense("S3", idB, 0, S3); -data.senses[S4] = newMergeTreeSense("S4", idB, 1, S4); beforeEach(jest.clearAllMocks); @@ -98,7 +100,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -112,7 +114,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -137,7 +139,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -166,7 +168,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -185,7 +187,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -202,7 +204,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -220,7 +222,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); await store.dispatch(mergeAll()); @@ -238,7 +240,6 @@ describe("MergeDupActions", () => { it("creates an action to add MergeDups data", () => { const goal = new MergeDups(); goal.steps = [{ words: [...goalDataMock.plannedWords[0]] }]; - const store = setupStore(); store.dispatch(dispatchMergeStepData(goal)); const setDataAction = setData(goalDataMock.plannedWords[0]); @@ -254,7 +255,7 @@ describe("MergeDupActions", () => { const tree: MergeTree = { ...defaultTree, words: { WA, WB } }; const store = setupStore({ ...preloadedState, - mergeDuplicateGoal: { data, tree, mergeWords: [] }, + mergeDuplicateGoal: { ...defaultMergeState, data, tree }, }); store.dispatch(deferMerge()); expect(mockGraylistAdd).toHaveBeenCalledTimes(1); diff --git a/src/goals/MergeDuplicates/Redux/tests/MergeDupsDataMock.ts b/src/goals/MergeDuplicates/Redux/tests/MergeDupsDataMock.ts index cfe62e0b3c..00e759af71 100644 --- a/src/goals/MergeDuplicates/Redux/tests/MergeDupsDataMock.ts +++ b/src/goals/MergeDuplicates/Redux/tests/MergeDupsDataMock.ts @@ -5,10 +5,11 @@ import { defaultState } from "components/App/DefaultState"; import { convertSenseToMergeTreeSense, convertWordToMergeTreeWord, - defaultSidebar, + defaultTree, newMergeTreeWord, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; import { type MergeDupsData } from "goals/MergeDuplicates/MergeDupsTypes"; +import { defaultState as mergeState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { type RootState } from "store"; import { newSense, newWord, simpleWord } from "types/word"; @@ -122,6 +123,7 @@ export const mergeTwoWordsScenario: GetMergeWordsScenario = { return { ...persistedDefaultState, mergeDuplicateGoal: { + ...mergeState, data: { senses: { [senseBah.guid]: convertSenseToMergeTreeSense( @@ -146,7 +148,7 @@ export const mergeTwoWordsScenario: GetMergeWordsScenario = { }, }, tree: { - sidebar: defaultSidebar, + ...defaultTree, words: { [wordFoo2.id]: convertWordToMergeTreeWord({ ...wordFoo2, @@ -154,7 +156,6 @@ export const mergeTwoWordsScenario: GetMergeWordsScenario = { }), }, }, - mergeWords: [], }, }; }, @@ -184,6 +185,7 @@ export const mergeTwoSensesScenario: GetMergeWordsScenario = { return { ...persistedDefaultState, mergeDuplicateGoal: { + ...mergeState, data: { senses: { [senseBah.guid]: convertSenseToMergeTreeSense( @@ -208,7 +210,7 @@ export const mergeTwoSensesScenario: GetMergeWordsScenario = { }, }, tree: { - sidebar: defaultSidebar, + ...defaultTree, words: { [wordFoo2.id]: newMergeTreeWord(wordFoo2.vernacular, { word2_senseA: [senseBar.guid], @@ -216,7 +218,6 @@ export const mergeTwoSensesScenario: GetMergeWordsScenario = { }), }, }, - mergeWords: [], }, }; }, @@ -246,6 +247,7 @@ export const mergeTwoDefinitionsScenario: GetMergeWordsScenario = { return { ...persistedDefaultState, mergeDuplicateGoal: { + ...mergeState, data: { senses: { [senseBah.guid]: convertSenseToMergeTreeSense( @@ -270,7 +272,7 @@ export const mergeTwoDefinitionsScenario: GetMergeWordsScenario = { }, }, tree: { - sidebar: defaultSidebar, + ...defaultTree, words: { [wordFoo2.id]: newMergeTreeWord(wordFoo2.vernacular, { word2_senseA: [senseBar.guid], @@ -278,7 +280,6 @@ export const mergeTwoDefinitionsScenario: GetMergeWordsScenario = { }), }, }, - mergeWords: [], }, }; }, diff --git a/src/goals/MergeDuplicates/Redux/tests/MergeDupsReducer.test.tsx b/src/goals/MergeDuplicates/Redux/tests/MergeDupsReducer.test.tsx index 5785df9d05..cdfb809dbf 100644 --- a/src/goals/MergeDuplicates/Redux/tests/MergeDupsReducer.test.tsx +++ b/src/goals/MergeDuplicates/Redux/tests/MergeDupsReducer.test.tsx @@ -1,10 +1,10 @@ -import { Action, PayloadAction } from "@reduxjs/toolkit"; +import { type Action, type PayloadAction } from "@reduxjs/toolkit"; import { + type MergeTreeReference, + type MergeTreeWord, convertSenseToMergeTreeSense, - defaultSidebar, - MergeTreeReference, - MergeTreeWord, + defaultTree, newMergeTreeWord, } from "goals/MergeDuplicates/MergeDupsTreeTypes"; import { @@ -17,18 +17,19 @@ import { orderSense, setData, } from "goals/MergeDuplicates/Redux/MergeDupsActions"; -import mergeDupStepReducer, { +import mergeDupStepReducer from "goals/MergeDuplicates/Redux/MergeDupsReducer"; +import { + type MergeTreeState, defaultState, -} from "goals/MergeDuplicates/Redux/MergeDupsReducer"; -import { MergeTreeState } from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; +} from "goals/MergeDuplicates/Redux/MergeDupsReduxTypes"; import { mergeTwoDefinitionsScenario, mergeTwoSensesScenario, mergeTwoWordsScenario, } from "goals/MergeDuplicates/Redux/tests/MergeDupsDataMock"; -import { StoreAction, StoreActionTypes } from "rootActions"; +import { type StoreAction, StoreActionTypes } from "rootActions"; import { setupStore } from "store"; -import { Hash } from "types/hash"; +import { type Hash } from "types/hash"; import { newFlag, testWordList } from "types/word"; jest.mock("uuid"); @@ -91,15 +92,8 @@ describe("MergeDupsReducer", () => { }; } const mockState: MergeTreeState = { - data: { - words: {}, - senses: {}, - }, - tree: { - sidebar: defaultSidebar, - words: testTreeWords(), - }, - mergeWords: [], + ...defaultState, + tree: { ...defaultTree, words: testTreeWords() }, }; function checkTreeWords( action: Action | PayloadAction,