Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Feb 19, 2024
1 parent c20aa65 commit 14f6b31
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/components/AppBar/tests/ProjectButtons.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import { Goal, GoalStatus } from "types/goals";
import { Path } from "types/path";
import { themeColors } from "types/theme";

jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
}));

jest.mock("backend", () => ({
hasPermission: (perm: Permission) => mockHasPermission(perm),
isSiteAdmin: () => mockIsSiteAdmin(),
}));
jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
}));
jest.mock("components/Project/ProjectActions", () => ({}));

const mockHasPermission = jest.fn();
const mockIsSiteAdmin = jest.fn();
Expand Down
1 change: 1 addition & 0 deletions src/components/AppBar/tests/SpeakerMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { randomSpeaker } from "types/project";
jest.mock("backend", () => ({
getAllSpeakers: () => mockGetAllSpeakers(),
}));
jest.mock("components/Project/ProjectActions", () => ({}));

const mockProjId = "mock-project-id";
const mockGetAllSpeakers = jest.fn();
Expand Down
1 change: 1 addition & 0 deletions src/components/AppBar/tests/UserMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock("backend/localStorage", () => ({
getAvatar: jest.fn(),
getCurrentUser: jest.fn(),
}));
jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jest.mock(
() => (props: any) => mockAutocomplete(props)
);

jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock("components/Pronunciations/PronunciationsFrontend", () => "div");

/** Bypass the Autocomplete and render its internal input with the props of both. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { newWritingSystem } from "types/writingSystem";

jest.mock("@mui/material/Autocomplete", () => "div");

jest.mock("components/Project/ProjectActions", () => ({}));

const mockStore = configureMockStore()(defaultState);
const mockVern = "Vernacular";
const mockGloss = "Gloss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jest.mock(
"components/DataEntry/DataEntryTable/RecentEntry",
() => MockRecentEntry
);
jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock("components/Pronunciations/PronunciationsFrontend", () => "div");

jest.spyOn(window, "alert").mockImplementation(() => {});
Expand Down
1 change: 1 addition & 0 deletions src/components/GoalTimeline/tests/GoalRedux.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jest.mock("backend", () => ({
jest.mock("browserRouter", () => ({
navigate: (path: Path) => mockNavigate(path),
}));
jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock("components/Pronunciations/Recorder");

const mockAddGoalToUserEdit = jest.fn();
Expand Down
1 change: 1 addition & 0 deletions src/components/GoalTimeline/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jest.mock("components/GoalTimeline/Redux/GoalActions", () => ({
asyncAddGoal: (goal: Goal) => mockChooseGoal(goal),
asyncGetUserEdits: () => jest.fn(),
}));
jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock("components/Pronunciations/Recorder");
jest.mock("types/hooks", () => {
return {
Expand Down
64 changes: 42 additions & 22 deletions src/components/Project/tests/ProjectActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
asyncUpdateCurrentProject,
clearCurrentProject,
} from "components/Project/ProjectActions";
import { defaultState as currentProjectState } from "components/Project/ProjectReduxTypes";
import { type RootState, setupStore } from "store";
import { newProject } from "types/project";
import { newUser } from "types/user";
Expand All @@ -17,6 +18,8 @@ jest.mock("backend", () => ({
getAllSemanticDomainNames: (lang?: string) => mockGetAllSemDomNames(lang),
updateProject: (proj: Project) => mockUpdateProject(proj),
}));
// Mock "i18n", else `thrown: "Error: Error: connect ECONNREFUSED ::1:80 [...]`
jest.mock("i18n", () => ({ language: "" }));

const mockGetAllProjectUsers = jest.fn();
const mockGetAllSemDomNames = jest.fn();
Expand All @@ -40,6 +43,7 @@ describe("ProjectActions", () => {
const store = setupStore({
...persistedDefaultState,
currentProjectState: {
...currentProjectState,
project: proj,
speaker: {} as Speaker,
users: [newUser()],
Expand All @@ -59,6 +63,7 @@ describe("ProjectActions", () => {
const store = setupStore({
...persistedDefaultState,
currentProjectState: {
...currentProjectState,
project: proj,
speaker: {} as Speaker,
users: [newUser()],
Expand All @@ -73,21 +78,33 @@ describe("ProjectActions", () => {
expect(users).toHaveLength(1);
});

it("fetches semantic domain names when semDomWritingSystem changes", async () => {
it("fetches semantic domain names when empty", async () => {
const store = setupStore({ ...persistedDefaultState });

// First project loaded
const proj: Project = { ...newProject(), id: mockProjId };
await store.dispatch(asyncUpdateCurrentProject(proj));
expect(mockUpdateProject).toHaveBeenCalledTimes(1);
expect(mockGetAllSemDomNames).toHaveBeenCalledTimes(1);
});

it("fetches semantic domain names when semDomWritingSystem changes", async () => {
const store = setupStore({
...persistedDefaultState,
currentProjectState: {
project: proj,
speaker: {} as Speaker,
users: [newUser()],
...currentProjectState,
project: { ...newProject(), id: mockProjId },
semanticDomains: { ["1"]: "one" },
},
});

await store.dispatch(asyncUpdateCurrentProject({ ...proj, id: "newId" }));
// Project changed but same sem dom language
const proj: Project = { ...newProject(), id: "new-proj-id" };
await store.dispatch(asyncUpdateCurrentProject(proj));
expect(mockUpdateProject).toHaveBeenCalledTimes(1);
expect(mockGetAllSemDomNames).not.toHaveBeenCalled();

// Sem dom language change in project
const lang = "es";
const semDomWritingSystem = { ...proj.semDomWritingSystem, bcp47: lang };
await store.dispatch(
Expand All @@ -101,41 +118,44 @@ describe("ProjectActions", () => {

describe("asyncRefreshProjectUsers", () => {
it("correctly affects state", async () => {
const proj: Project = { ...newProject(), id: mockProjId };
const store = setupStore({
...persistedDefaultState,
currentProjectState: {
project: proj,
...currentProjectState,
project: { ...newProject(), id: mockProjId },
semanticDomains: { ["1"]: "one" },
speaker: {} as Speaker,
users: [],
},
});
const mockUsers = [newUser(), newUser(), newUser()];
mockGetAllProjectUsers.mockResolvedValueOnce(mockUsers);
await store.dispatch(asyncRefreshProjectUsers("mockProjId"));
const { project, speaker, users } = store.getState().currentProjectState;
expect(project.id).toEqual(mockProjId);
expect(speaker).not.toBeUndefined();
expect(users).toHaveLength(mockUsers.length);
const projState = store.getState().currentProjectState;
expect(projState.project.id).toEqual(mockProjId);
expect(projState.semanticDomains).not.toBeUndefined();
expect(projState.speaker).not.toBeUndefined();
expect(projState.users).toHaveLength(mockUsers.length);
});
});

describe("clearCurrentProject", () => {
it("correctly affects state", () => {
const nonDefaultState = {
project: { ...newProject(), id: "nonempty-string" },
speaker: {} as Speaker,
users: [newUser()],
};
const store = setupStore({
...persistedDefaultState,
currentProjectState: nonDefaultState,
currentProjectState: {
...currentProjectState,
project: { ...newProject(), id: mockProjId },
semanticDomains: { ["1"]: "one" },
speaker: {} as Speaker,
users: [newUser()],
},
});
store.dispatch(clearCurrentProject());
const { project, speaker, users } = store.getState().currentProjectState;
expect(project.id).toEqual("");
expect(speaker).toBeUndefined();
expect(users).toHaveLength(0);
const projState = store.getState().currentProjectState;
expect(projState.project.id).toEqual("");
expect(projState.semanticDomains).toBeUndefined();
expect(projState.speaker).toBeUndefined();
expect(projState.users).toHaveLength(0);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock("backend", () => ({
downloadLift: (...args: any[]) => mockDownloadList(...args),
exportLift: (...args: any[]) => mockExportLift(...args),
}));
jest.mock("components/Project/ProjectActions", () => ({}));

const mockDownloadList = jest.fn();
const mockExportLift = jest.fn();
Expand Down
3 changes: 2 additions & 1 deletion src/components/ProjectScreen/tests/CreateProject.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jest.mock("backend", () => ({
projectDuplicateCheck: () => mockProjectDuplicateCheck(),
uploadLiftAndGetWritingSystems: () => mockUploadLiftAndGetWritingSystems(),
}));
// Mock "i18n", else `thrown: "Error: Error: connect ECONNREFUSED ::1:80 [...]`
jest.mock("i18n", () => ({ language: "" }));

const mockProjectDuplicateCheck = jest.fn();
const mockUploadLiftAndGetWritingSystems = jest.fn();
Expand All @@ -42,7 +44,6 @@ const mockSubmitEvent = (): Partial<React.FormEvent<HTMLFormElement>> => ({

let projectMaster: ReactTestRenderer;
let projectHandle: ReactTestInstance;
4;

beforeAll(async () => {
await act(async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/components/ProjectSettings/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jest.mock("backend", () => ({
getUserRoles: () => Promise.resolve([]),
}));
jest.mock("components/Project/ProjectActions");
// Mock "i18n", else `thrown: "Error: Error: connect ECONNREFUSED ::1:80 [...]`
jest.mock("i18n", () => ({ language: "" }));
jest.mock("types/hooks", () => {
return {
...jest.requireActual("types/hooks"),
Expand Down
1 change: 1 addition & 0 deletions src/components/SiteSettings/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock("backend", () => ({
getAllUsers: (...args: any[]) => mockGetAllUsers(...args),
getBannerText: (...args: any[]) => mockGetBannerText(...args),
}));
jest.mock("components/Project/ProjectActions", () => ({}));

const setupMocks = (): void => {
mockGetAllProjects.mockResolvedValue([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jest.mock("@mui/material", () => {
};
});

jest.mock("components/Project/ProjectActions", () => ({}));
jest.mock(
"goals/CharacterInventory/CharInv/CharacterDetail/FindAndReplace/FindAndReplaceActions",
() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { StoreState } from "types";
import theme from "types/theme";
import { newPronunciation } from "types/word";

jest.mock("components/Project/ProjectActions", () => ({}));
// Mock the store interactions
jest.mock("goals/ReviewEntries/Redux/ReviewEntriesActions", () => ({
deleteAudio: (...args: any[]) => mockDeleteAudio(...args),
Expand Down

0 comments on commit 14f6b31

Please sign in to comment.