Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
test: globally mock fs (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
goga-m authored May 24, 2021
1 parent 6301702 commit 0fcbe96
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
16 changes: 16 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ jest.mock("electron", () => {
};
});

jest.mock("fs", () => {
const fs = jest.requireActual(`fs`);

return {
...fs,
readFileSync: (filepath, encoding) => {
// Exceptions
if (filepath === "path/to/sample-export.json") return "";

// Use actual
return fs.readFileSync(filepath, encoding);
},
writeFileSync: jest.fn(),
};
});

beforeAll(async () => {
await bootEnvWithProfileFixtures({ env, shouldRestoreDefaultProfile: true });
// Mark profiles as restored, to prevent multiple restoration in profile synchronizer
Expand Down
13 changes: 4 additions & 9 deletions src/app/hooks/use-files.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import electron from "electron";

import { useFiles } from "./use-files";

jest.mock("fs", () => ({
writeFileSync: jest.fn(),
readFileSync: jest.fn().mockReturnValue({ toString: () => "{test:'test'}" }),
}));

describe("useFiles", () => {
it("should read file contents", () => {
const { result } = renderHook(() => useFiles());
expect(result.current.readFileContents("/path/to/file.json")).toEqual({
content: "{test:'test'}",
extension: ".json",
name: "file.json",
expect(result.current.readFileContents("filePath")).toEqual({
content: "test mnemonic",
extension: "",
name: "filePath",
});
});

Expand Down
11 changes: 3 additions & 8 deletions src/plugins/services/filesystem/FileSystemPluginService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ const config = {
"desktop-wallet": { permissions: ["FILESYSTEM"] },
};

jest.mock("fs", () => ({
writeFileSync: jest.fn(),
readFileSync: jest.fn(() => "test"),
}));

describe("FileSystemPluginService", () => {
let profile: Contracts.IProfile;
let manager: PluginManager;
Expand All @@ -37,7 +32,7 @@ describe("FileSystemPluginService", () => {
it("should open file", async () => {
let content: any;

jest.spyOn(electron.remote.dialog, "showOpenDialog").mockResolvedValue({ filePaths: ["/test.txt"] });
jest.spyOn(electron.remote.dialog, "showOpenDialog").mockResolvedValue({ filePaths: ["filePath"] });

const fixture = (api: PluginAPI) => {
api.filesystem()
Expand All @@ -53,15 +48,15 @@ describe("FileSystemPluginService", () => {

await new Promise((r) => setTimeout(r, 200));

expect(content).toBe("test");
expect(content).toBe("test mnemonic");
});

it("should save file", async () => {
const content = "test";

const saveSpy = jest
.spyOn(electron.remote.dialog, "showSaveDialog")
.mockResolvedValue({ filePath: "/test.txt" });
.mockResolvedValue({ filePath: "filePath" });

const fixture = (api: PluginAPI) => {
api.filesystem().askUserToSaveFile(content);
Expand Down
9 changes: 2 additions & 7 deletions src/utils/electron-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const defaultFilters = [
{ name: "All Files", extensions: ["*"] },
];

jest.mock("fs", () => ({
writeFileSync: jest.fn(),
readFileSync: jest.fn(),
}));

describe("Electron utils", () => {
describe("setThemeSource", () => {
it("should set theme source", () => {
Expand Down Expand Up @@ -150,7 +145,7 @@ describe("Electron utils", () => {
describe("when restricting the file path", () => {
it("should not throw an error if the given filepath is valid", async () => {
showSaveDialogMock = jest.spyOn(electron.remote.dialog, "showSaveDialog").mockImplementation(() => ({
filePath: "./bar.txt",
filePath: "./filePath",
}));

await expect(saveFile(null, null, { restrictToPath: "./" })).resolves.not.toThrow();
Expand Down Expand Up @@ -222,7 +217,7 @@ describe("Electron utils", () => {
describe("when restricting the file path", () => {
it("should not throw an error if the given filepath is valid", async () => {
showOpenDialogMock = jest.spyOn(electron.remote.dialog, "showOpenDialog").mockImplementation(() => ({
filePaths: ["./bar.txt"],
filePaths: ["./filePath"],
}));

await expect(openFile(null, { restrictToPath: "./" })).resolves.not.toThrow();
Expand Down

0 comments on commit 0fcbe96

Please sign in to comment.