Skip to content

Commit

Permalink
Sort out mocking file access methods
Browse files Browse the repository at this point in the history
  • Loading branch information
charlielee committed Sep 22, 2024
1 parent 11022d8 commit f31ec4c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import FileManager from "../../../services/fileManager/FileManager";
import { renderWithProviders } from "../../../testing/TestHelper";
import ProjectSettingsModal from "./ProjectSettingsModal";
import { mockGetWorkingDirectory, mockOpenDirectoryDialog } from "../../../testing/mockFileAccess";

const getModal = () => screen.getByRole("dialog");
const modalTitle = () => screen.getByRole("heading");
Expand Down Expand Up @@ -40,9 +40,9 @@ describe("new project", () => {

it("should be able to fill in and submit form ", async () => {
const workingDirectoryName = "dirname";
// const mockOpenDirectoryDialog = jest
// .spyOn(FileManager, "openDirectoryDialogHandleCancel")
// .mockImplementation(() => Promise.resolve({ name: workingDirectoryName }) as any);
const mockedOpenDirectoryDialog = mockOpenDirectoryDialog(workingDirectoryName);
mockGetWorkingDirectory();

renderWithProviders(<ProjectSettingsModal />);

await userEvent.type(nameInput(), "My Movie");
Expand All @@ -52,8 +52,7 @@ describe("new project", () => {

await userEvent.click(chooseFolderButton());

// expect(mockOpenDirectoryDialog).toHaveBeenCalledWith("changeWorkingDirectory");

expect(mockedOpenDirectoryDialog).toHaveBeenCalledWith("changeWorkingDirectory");
await waitFor(() =>
expect(directoryPathInput()).toHaveValue(`./${workingDirectoryName}/My-Movie.bafiles`)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const ProjectSettingsModal = (): JSXElementWithTestIds => {
const workingDirectoryHandle =
await FileManager.openDirectoryDialogHandleCancel("changeWorkingDirectory");

console.log("handle");

if (workingDirectoryHandle !== undefined) {
await putOrAddWorkingDirectory(workingDirectoryHandle);
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/hooks/useProjectDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { db } from "../services/database/Database";
import { useSelector } from "react-redux";
import { RootState } from "../redux/store";

const useWorkingDirectory = () => {
const useProjectDirectory = () => {
const projectDirectoryId = useSelector((state: RootState) => state.project.projectDirectoryId);
const recentDirectoryEntry = useLiveQuery(
() => db.recentDirectories.get(projectDirectoryId ?? ""),
Expand All @@ -13,4 +13,4 @@ const useWorkingDirectory = () => {
return recentDirectoryEntry;
};

export default useWorkingDirectory;
export default useProjectDirectory;
21 changes: 21 additions & 0 deletions src/renderer/testing/mockFileAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { v4 as uuidv4 } from "uuid";
import * as RecentDirectoryEntry from "../services/database/RecentDirectoryEntry";
import FileManager from "../services/fileManager/FileManager";

export const mockOpenDirectoryDialog = (selectedDirectoryName: string) =>
jest
.spyOn(FileManager, "openDirectoryDialogHandleCancel")
.mockImplementation(() => Promise.resolve({ name: selectedDirectoryName }) as any);

export const mockGetWorkingDirectory = () =>
jest.spyOn(RecentDirectoryEntry, "getWorkingDirectory").mockImplementation(() =>
Promise.resolve({
id: uuidv4(),
type: RecentDirectoryEntry.RecentDirectoryType.WORKING_DIRECTORY,
friendlyName: "My Working Directory",
handle: {
name: "working-directory",
getDirectoryHandle: () => ({ name: "project-directory" }),
} as any,
})
);
22 changes: 0 additions & 22 deletions src/renderer/testing/mockFileSystemAccessApi.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ window.preload = api;
// Dexie expects window.indexedDB to be defined which isn't available in the Jest environment
Dexie.dependencies.indexedDB = fakeIndexedDB;
Dexie.dependencies.IDBKeyRange = FDBKeyRange;

// window.

0 comments on commit f31ec4c

Please sign in to comment.