Skip to content

Commit

Permalink
[MergeDups] Clean up state (#2896)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Jan 30, 2024
1 parent a88c14c commit df6d632
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/components/App/DefaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -90,6 +93,7 @@ const wordFoo2 = {
// vern: foo
// senses: bar, baz
const mockTwoWordState = (): MergeTreeState => ({
...mergeState,
data: {
senses: {
[senseBah.guid]: convertSenseToMergeTreeSense(senseBah, wordFoo1.id, 0),
Expand All @@ -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],
Expand All @@ -111,7 +115,6 @@ const mockTwoWordState = (): MergeTreeState => ({
}),
},
},
mergeWords: [],
});

const renderMergeDragDrop = async (
Expand Down
6 changes: 4 additions & 2 deletions src/goals/MergeDuplicates/MergeDupsTreeTypes.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,6 +16,8 @@ export interface MergeData {
senses: Hash<MergeTreeSense>;
}

export const defaultData: MergeData = { words: {}, senses: {} };

export interface MergeTreeReference {
wordId: string;
mergeSenseId: string;
Expand Down
25 changes: 9 additions & 16 deletions src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [];
}
},
Expand Down
32 changes: 22 additions & 10 deletions src/goals/MergeDuplicates/Redux/MergeDupsReduxTypes.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
39 changes: 20 additions & 19 deletions src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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);

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand Down Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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]);
Expand All @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/goals/MergeDuplicates/Redux/tests/MergeDupsDataMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -122,6 +123,7 @@ export const mergeTwoWordsScenario: GetMergeWordsScenario = {
return {
...persistedDefaultState,
mergeDuplicateGoal: {
...mergeState,
data: {
senses: {
[senseBah.guid]: convertSenseToMergeTreeSense(
Expand All @@ -146,15 +148,14 @@ export const mergeTwoWordsScenario: GetMergeWordsScenario = {
},
},
tree: {
sidebar: defaultSidebar,
...defaultTree,
words: {
[wordFoo2.id]: convertWordToMergeTreeWord({
...wordFoo2,
senses: [senseBar, senseBaz, senseBah],
}),
},
},
mergeWords: [],
},
};
},
Expand Down Expand Up @@ -184,6 +185,7 @@ export const mergeTwoSensesScenario: GetMergeWordsScenario = {
return {
...persistedDefaultState,
mergeDuplicateGoal: {
...mergeState,
data: {
senses: {
[senseBah.guid]: convertSenseToMergeTreeSense(
Expand All @@ -208,15 +210,14 @@ export const mergeTwoSensesScenario: GetMergeWordsScenario = {
},
},
tree: {
sidebar: defaultSidebar,
...defaultTree,
words: {
[wordFoo2.id]: newMergeTreeWord(wordFoo2.vernacular, {
word2_senseA: [senseBar.guid],
word2_senseB: [senseBaz.guid, senseBah.guid],
}),
},
},
mergeWords: [],
},
};
},
Expand Down Expand Up @@ -246,6 +247,7 @@ export const mergeTwoDefinitionsScenario: GetMergeWordsScenario = {
return {
...persistedDefaultState,
mergeDuplicateGoal: {
...mergeState,
data: {
senses: {
[senseBah.guid]: convertSenseToMergeTreeSense(
Expand All @@ -270,15 +272,14 @@ export const mergeTwoDefinitionsScenario: GetMergeWordsScenario = {
},
},
tree: {
sidebar: defaultSidebar,
...defaultTree,
words: {
[wordFoo2.id]: newMergeTreeWord(wordFoo2.vernacular, {
word2_senseA: [senseBar.guid],
word2_senseB: [senseBag.guid, senseBah.guid],
}),
},
},
mergeWords: [],
},
};
},
Expand Down
Loading

0 comments on commit df6d632

Please sign in to comment.