Skip to content

Commit

Permalink
Fix some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonleenaylor committed Sep 16, 2022
1 parent c0c5c85 commit 66ab169
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import configureMockStore from "redux-mock-store";

import "tests/mockReactI18next";

import { SemanticDomainFull } from "api";
import DataEntryHeader, {
getQuestions,
} from "components/DataEntry/DataEntryHeader/DataEntryHeader";
import { newSemanticDomain } from "types/semanticDomain";
import { SemanticDomainFull } from "api";

const mockStore = configureMockStore()();
const mockCallback = jest.fn();
Expand Down
9 changes: 5 additions & 4 deletions src/components/TreeView/TreeSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Grid, TextField } from "@material-ui/core";
import React, { ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
import { Key } from "ts-key-enum";

import { SemanticDomainTreeNode } from "api";
import {
getSemanticDomainTreeNode,
getSemanticDomainTreeNodeByName,
} from "backend";
import React, { ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
import { Key } from "ts-key-enum";

export interface TreeSearchProps {
currentDomain: SemanticDomainTreeNode;
Expand Down Expand Up @@ -99,7 +100,7 @@ export function useTreeSearch(props: TreeSearchProps): TreeSearchState {
// Search for domain
if (!isNaN(parseInt(input))) {
// make a blocking call to the backend API for the domain id instead of using the map
let domain = await getSemanticDomainTreeNode(input, "en");
const domain = await getSemanticDomainTreeNode(input, "en");
if (domain) {
animateSuccessfulSearch(domain, event);
// Return to indicate success and skip setting error state.
Expand Down
31 changes: 16 additions & 15 deletions src/components/TreeView/TreeViewActions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { defaultState } from "./TreeViewReducer";
import { SemanticDomain, SemanticDomainTreeNode } from "api/models";
import { getSemanticDomainTreeNode } from "backend";
import { StoreState } from "types";
import { StoreStateDispatch } from "types/Redux/actions";

export enum TreeActionType {
CLOSE_TREE = "CLOSE_TREE",
OPEN_TREE = "OPEN_TREE",
SET_DOMAIN_LANGUAGE = "SET_DOMAIN_LANGUAGE",
TRAVERSE_TREE = "TRAVERSE_TREE",
SET_CURRENT_DOMAIN = "SET_CURRENT_DOMAIN",
}

export interface TreeViewAction {
type: TreeActionType;
domain?: SemanticDomain;
language?: string;
}
import { TreeActionType, TreeViewAction } from "./TreeViewReduxTypes";

export function closeTreeAction(): TreeViewAction {
return { type: TreeActionType.CLOSE_TREE };
Expand Down Expand Up @@ -46,10 +35,22 @@ export function setCurrentDomain(
return { type: TreeActionType.SET_CURRENT_DOMAIN, domain };
}

export function updateTreeLanguage(language: string, headString = "") {
export function updateTreeLanguage(language: string) {
return async (dispatch: StoreStateDispatch) => {
if (language) {
dispatch(setDomainLanguageAction(language));
}
};
}

export function initTreeDomain(language: string) {
return async (dispatch: StoreStateDispatch, getState: () => StoreState) => {
const currentDomain = getState().treeViewState.currentDomain;
if (currentDomain === defaultState.currentDomain) {
if (!currentDomain.lang) {
currentDomain.lang = language ?? "en";
}
dispatch(traverseTreeAction(currentDomain));
}
};
}
15 changes: 6 additions & 9 deletions src/components/TreeView/TreeViewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SemanticDomain, SemanticDomainTreeNode, WritingSystem } from "api";
import TreeDepiction from "components/TreeView/TreeDepiction";
import TreeSearch from "components/TreeView/TreeSearch";
import {
initTreeDomain,
traverseTreeAction,
updateTreeLanguage,
} from "components/TreeView/TreeViewActions";
Expand Down Expand Up @@ -36,25 +37,21 @@ export function TreeView(props: TreeViewProps): ReactElement {
);
const [visible, setVisible] = useState(true);
const dispatch = useDispatch();
const { i18n } = props;

useEffect(() => {
/* Select the language used for the semantic domains.
* Primary: Has it been specified for the project?
* Secondary: What is the current browser/ui language? */
const newLang =
getSemDomWritingSystem(semDomWritingSystem)?.bcp47 ??
props.i18n.resolvedLanguage;
i18n.resolvedLanguage;
if (newLang && newLang !== semDomLanguage) {
const headString = props.t("addWords.domain") as string;
dispatch(updateTreeLanguage(newLang, headString));
dispatch(updateTreeLanguage(newLang));
// Don't update when props updates, except props.i18n.resolvedLanguage
} // eslint-disable-next-line react-hooks/exhaustive-deps
}, [
semDomLanguage,
semDomWritingSystem,
dispatch,
props.i18n.resolvedLanguage,
]);
dispatch(initTreeDomain(newLang));
}, [semDomLanguage, semDomWritingSystem, dispatch, i18n.resolvedLanguage]);

function animateHandler(domain: SemanticDomain): Promise<void> {
if (visible) {
Expand Down
8 changes: 2 additions & 6 deletions src/components/TreeView/TreeViewReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SemanticDomainTreeNode } from "api/models";
import {
TreeViewAction,
TreeActionType,
} from "components/TreeView/TreeViewActions";
} from "components/TreeView/TreeViewReduxTypes";
import i18n from "i18n";
import { StoreAction, StoreActionTypes } from "rootActions";

Expand All @@ -23,6 +23,7 @@ export const defaultState: TreeViewState = {
previous: undefined,
next: undefined,
parent: undefined,
children: undefined,
},
};

Expand All @@ -45,11 +46,6 @@ export const treeViewReducer = (
...state,
language: action.language,
};
case TreeActionType.TRAVERSE_TREE:
if (!action.domain) {
throw new Error("Cannot traverse tree without specifying domain.");
}
return { ...state };
case TreeActionType.SET_CURRENT_DOMAIN:
if (!action.domain) {
throw new Error("Cannot set the current domain to undefined.");
Expand Down
14 changes: 14 additions & 0 deletions src/components/TreeView/TreeViewReduxTypes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SemanticDomain } from "api/models";

export enum TreeActionType {
CLOSE_TREE = "CLOSE_TREE",
OPEN_TREE = "OPEN_TREE",
SET_DOMAIN_LANGUAGE = "SET_DOMAIN_LANGUAGE",
SET_CURRENT_DOMAIN = "SET_CURRENT_DOMAIN",
}

export interface TreeViewAction {
type: TreeActionType;
domain?: SemanticDomain;
language?: string;
}
5 changes: 2 additions & 3 deletions src/components/TreeView/tests/TreeSearch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { render, RenderResult, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import { act, renderHook } from "@testing-library/react-hooks";
import userEvent from "@testing-library/user-event";
import React from "react";
import { Key } from "ts-key-enum";

import "tests/mockReactI18next";
import { SemanticDomainTreeNode } from "api";
import * as backend from "backend";

import TreeSearch, {
insertDecimalPoints,
testId,
TreeSearchProps,
useTreeSearch,
} from "components/TreeView/TreeSearch";
import domMap, { mapIds } from "components/TreeView/tests/MockSemanticDomain";
import { SemanticDomainTreeNode } from "api";
import { newSemanticDomainTreeNode } from "types/semanticDomain";

// Handles
Expand Down
9 changes: 5 additions & 4 deletions src/components/TreeView/tests/TreeViewActions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";

import * as backend from "backend";
import {
setDomainLanguageAction,
traverseTreeAction,
TreeActionType,
} from "components/TreeView/TreeViewActions";
import { defaultState } from "components/TreeView/TreeViewReducer";
import { newSemanticDomainTreeNode } from "types/semanticDomain";
import * as backend from "backend";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import { TreeActionType } from "../TreeViewReduxTypes";

jest.mock("backend", () => ({
getSemanticDomainTreeNode: (id: string, lang: string) =>
Expand Down
5 changes: 2 additions & 3 deletions src/components/TreeView/tests/TreeViewComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import renderer, {
import configureMockStore from "redux-mock-store";

import "tests/mockReactI18next";
import thunk from "redux-thunk";

import TreeDepiction from "components/TreeView/TreeDepiction";
import TreeView from "components/TreeView/TreeViewComponent";
Expand All @@ -26,12 +27,10 @@ jest.mock("@material-ui/core", () => {
Zoom: realMaterialUi.Container,
};
});

const mockStore = configureMockStore()({
const mockStore = configureMockStore([thunk])({
treeViewState: {
...treeViewState,
currentDomain: mockMap[mockDomain.id],
domainMap: mockMap,
},
currentProjectState: {
project: { semDomWritingSystem: newWritingSystem() },
Expand Down
12 changes: 6 additions & 6 deletions src/components/TreeView/tests/TreeViewReducer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
TreeViewAction,
TreeActionType,
} from "components/TreeView/TreeViewActions";
} from "components/TreeView/TreeViewReduxTypes";
import {
treeViewReducer,
defaultState,
TreeViewState,
} from "components/TreeView/TreeViewReducer";
import { StoreAction, StoreActionTypes } from "rootActions";
import { TreeSemanticDomain } from "types/semanticDomain";
import { newSemanticDomain } from "types/semanticDomain";

describe("Test the TreeViewReducer", () => {
it("Returns defaultState when passed undefined", () => {
Expand All @@ -28,13 +28,13 @@ describe("Test the TreeViewReducer", () => {
treeViewReducer(
{
...defaultState,
currentDomain: defaultState.currentDomain.subdomains[0],
currentDomain: defaultState.currentDomain,
},
{ type: "Nothing" } as any as TreeViewAction
)
).toEqual({
...defaultState,
currentDomain: defaultState.currentDomain.subdomains[0],
currentDomain: defaultState.currentDomain,
});
});

Expand All @@ -57,10 +57,10 @@ describe("Test the TreeViewReducer", () => {
});

it("Returns state with a new SemanticDomain when requested to change this value", () => {
const payload = new TreeSemanticDomain("testId", "testName");
const payload = newSemanticDomain("testId", "testName");
expect(
treeViewReducer(defaultState, {
type: TreeActionType.TRAVERSE_TREE,
type: TreeActionType.SET_CURRENT_DOMAIN,
domain: payload,
})
).toEqual({ ...defaultState, currentDomain: payload });
Expand Down

0 comments on commit 66ab169

Please sign in to comment.