Skip to content

Commit

Permalink
Add reducer test cases for protect override
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Nov 13, 2024
1 parent 7185f48 commit e21032c
Showing 1 changed file with 57 additions and 19 deletions.
76 changes: 57 additions & 19 deletions src/goals/MergeDuplicates/Redux/tests/MergeDupsReducer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Action, type PayloadAction } from "@reduxjs/toolkit";

import { Status } from "api/models";
import {
type MergeTreeReference,
type MergeTreeWord,
Expand All @@ -17,6 +18,7 @@ import {
moveSense,
orderSense,
setData,
toggleOverrideProtection,
} from "goals/MergeDuplicates/Redux/MergeDupsActions";
import mergeDupStepReducer from "goals/MergeDuplicates/Redux/MergeDupsReducer";
import {
Expand Down Expand Up @@ -572,25 +574,61 @@ describe("MergeDupsReducer", () => {
);
});

test("setWordData", () => {
const wordList = testWordList();
const treeState = mergeDupStepReducer(undefined, setData(wordList));
// check if data has all words present
for (const word of wordList) {
const srcWordId = word.id;
expect(Object.keys(treeState.data.words)).toContain(srcWordId);
// check each sense of word
for (const [order, sense] of word.senses.entries()) {
const treeSense = convertSenseToMergeTreeSense(sense, srcWordId, order);
const senses = treeState.data.senses;
expect(Object.values(senses).map((s) => JSON.stringify(s))).toContain(
JSON.stringify(treeSense)
);
// check that this sense is somewhere in the tree
expect(
getRefByGuid(treeSense.sense.guid, treeState.tree.words)
).toBeDefined();
describe("setWordData", () => {
test("with no protected words/senses", () => {
const wordList = testWordList();
const treeState = mergeDupStepReducer(undefined, setData(wordList));
// check if data has all words present
for (const word of wordList) {
const srcWordId = word.id;
expect(Object.keys(treeState.data.words)).toContain(srcWordId);
// check each sense of word
for (const [order, sense] of word.senses.entries()) {
const treeSense = convertSenseToMergeTreeSense(
sense,
srcWordId,
order
);
const senses = treeState.data.senses;
expect(Object.values(senses).map((s) => JSON.stringify(s))).toContain(
JSON.stringify(treeSense)
);
// check that this sense is somewhere in the tree
expect(
getRefByGuid(treeSense.sense.guid, treeState.tree.words)
).toBeDefined();
}
}
}
// check that overrideProtection is reset
expect(treeState.overrideProtection).toEqual(false);
// check that hasProtected is false
expect(treeState.hasProtected).toBeFalsy();
});

test("with protected word", () => {
const wordList = testWordList();
wordList[1].accessibility = Status.Protected;
const treeState = mergeDupStepReducer(undefined, setData(wordList));
// check that hasProtected is true
expect(treeState.hasProtected).toBeTruthy();
});

test("with protected sense", () => {
const wordList = testWordList();
wordList.find((w) => w.senses.length)!.senses[0].accessibility =
Status.Protected;
const treeState = mergeDupStepReducer(undefined, setData(wordList));
// check that hasProtected is true
expect(treeState.hasProtected).toBeTruthy();
});
});

test("toggleOverrideProtection", () => {
let state = defaultState;
expect(state.overrideProtection).toBeFalsy();
state = mergeDupStepReducer(state, toggleOverrideProtection());
expect(state.overrideProtection).toBeTruthy();
state = mergeDupStepReducer(state, toggleOverrideProtection());
expect(state.overrideProtection).toBeFalsy();
});
});

0 comments on commit e21032c

Please sign in to comment.