Skip to content

Commit

Permalink
test(ts): use the snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Sep 18, 2024
1 parent ef9438a commit 9edcc99
Showing 1 changed file with 85 additions and 80 deletions.
165 changes: 85 additions & 80 deletions template/ts/{{cookiecutter.project_slug}}/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,119 +28,124 @@ describe("GitHub Action", () => {
const mockWriteFile = fs.writeFile as jest.MockedFunction<typeof fs.writeFile>;
const mockAxiosGet = axios.get as jest.MockedFunction<typeof axios.get>;

beforeEach(() => {
jest.resetAllMocks();
beforeAll(() => {
mockGetInput.mockReturnValue(".github/configs/setup-custom-action-by-ts.toml");
mockReadFile.mockResolvedValue(mockConfig);
mockAxiosGet.mockResolvedValue({ status: 200, data: {} });
});

it("should process text, count words, calculate sum and average correctly", async () => {
await run();

expect(mockSetOutput).toHaveBeenCalledWith("processed_text", "Hi world! Hi!");
expect(mockSetOutput).toHaveBeenCalledWith("word_count", 3);
expect(mockSetOutput).toHaveBeenCalledWith("sum", 15);
expect(mockSetOutput).toHaveBeenCalledWith("average", 3);
beforeEach(() => {
jest.resetAllMocks();
setupDefaultMocks();
});

it("should handle missing configuration file gracefully", async () => {
mockReadFile.mockRejectedValue(new Error("File not found"));
const setupDefaultMocks = () => {
mockReadFile.mockResolvedValue(mockConfig);
mockAxiosGet.mockResolvedValue({ status: 200, data: {} });
};

const setupFileMocks = (inputContent: string) => {
mockReadFile.mockImplementation((path) => {
if (path === ".github/configs/setup-custom-action-by-ts.toml") {
return Promise.resolve(mockConfig);
}
if (path === "input.txt") {
return Promise.resolve(inputContent);
}
return Promise.reject(new Error("Unexpected file read"));
});
};

await run();
describe("Text Processing", () => {
it("should process text, count words, calculate sum and average correctly", async () => {
await run();

expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: File not found");
});
expect(mockSetOutput.mock.calls).toMatchSnapshot();
});

it("should handle API request failures gracefully", async () => {
mockAxiosGet.mockRejectedValue(new Error("API error"));
it("should handle empty input text", async () => {
mockReadFile.mockResolvedValue(`
input_text = ""
number_list = []
`);

await run();
await run();

expect(mockWarning).toHaveBeenCalledWith("Failed to make API request: API error");
expect(mockSetOutput.mock.calls).toMatchSnapshot();
});
});

it("should check API reachability correctly", async () => {
await run();
describe("File Operations", () => {
it("should read from the input file and append text to the output file correctly", async () => {
setupFileMocks("Original file content.");

expect(mockAxiosGet).toHaveBeenCalledWith("https://api.example.com/data", { timeout: 10000 });
expect(mockInfo).toHaveBeenCalledWith("API https://api.example.com/data is reachable.");
});
await run();

it("should check API reachability correctly and warn on bad status", async () => {
mockAxiosGet.mockResolvedValue({ status: 500, data: {} });
expect(mockReadFile).toHaveBeenCalledWith("input.txt", "utf-8");
expect(mockWriteFile).toHaveBeenCalledWith("output.txt", "Original file content.\nGoodbye!", {
encoding: "utf-8",
});
expect(mockInfo).toHaveBeenCalledWith("Appended text to file: output.txt");
});

await run();
it("should handle missing input file gracefully", async () => {
mockReadFile.mockRejectedValue(new Error("File not found"));

expect(mockWarning).toHaveBeenCalledWith("API is not reachable, status code: 500");
});
await run();

it("should read from the input file and append text to the output file correctly", async () => {
mockReadFile.mockImplementation((path) => {
if (path === ".github/configs/setup-custom-action-by-ts.toml") {
return Promise.resolve(mockConfig);
}
if (path === "input.txt") {
return Promise.resolve("Original file content.");
}
return Promise.reject(new Error("Unexpected file read"));
expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: File not found");
});

await run();
it("should handle file write errors gracefully", async () => {
setupFileMocks("Original file content.");
mockWriteFile.mockRejectedValue(new Error("Failed to write to output file"));

await run();

expect(mockReadFile).toHaveBeenCalledWith(".github/configs/setup-custom-action-by-ts.toml", "utf-8");
expect(mockReadFile).toHaveBeenCalledWith("input.txt", "utf-8");
expect(mockWriteFile).toHaveBeenCalledWith("output.txt", "Original file content.\nGoodbye!", {
encoding: "utf-8",
expect(mockSetFailed).toHaveBeenCalledWith(
"Action failed with error: File operation failed: Failed to write to output file",
);
});
expect(mockInfo).toHaveBeenCalledWith("Appended text to file: output.txt");
});

it("should handle file read errors gracefully", async () => {
mockReadFile.mockImplementation((path) => {
if (path === ".github/configs/setup-custom-action-by-ts.toml") {
return Promise.resolve(mockConfig);
}
if (path === "input.txt") {
return Promise.reject(new Error("Failed to read input file"));
}
return Promise.reject(new Error("Unexpected file read"));
describe("API Operations", () => {
it("should check API reachability correctly", async () => {
await run();

expect(mockAxiosGet).toHaveBeenCalledWith("https://api.example.com/data", { timeout: 10000 });
expect(mockInfo).toHaveBeenCalledWith("API https://api.example.com/data is reachable.");
});

await run();
it("should handle API request failures gracefully", async () => {
mockAxiosGet.mockRejectedValue(new Error("API error"));

expect(mockSetFailed).toHaveBeenCalledWith(
"Action failed with error: File operation failed: Failed to read input file",
);
});
await run();

it("should handle file write errors gracefully", async () => {
mockReadFile.mockImplementation((path) => {
if (path === ".github/configs/setup-custom-action-by-ts.toml") {
return Promise.resolve(mockConfig);
}
if (path === "input.txt") {
return Promise.resolve("Original file content.");
}
return Promise.reject(new Error("Unexpected file read"));
expect(mockWarning).toHaveBeenCalledWith("Failed to make API request: API error");
});
mockWriteFile.mockRejectedValue(new Error("Failed to write to output file"));

await run();
it("should warn on bad API status", async () => {
mockAxiosGet.mockResolvedValue({ status: 500, data: {} });

await run();

expect(mockSetFailed).toHaveBeenCalledWith(
"Action failed with error: File operation failed: Failed to write to output file",
);
expect(mockWarning).toHaveBeenCalledWith("API is not reachable, status code: 500");
});
});

it("should use default values when config is empty", async () => {
mockReadFile.mockResolvedValue("");
describe("Configuration Handling", () => {
it("should use default values when config is empty", async () => {
mockReadFile.mockResolvedValue("");

await run();

expect(mockSetOutput.mock.calls).toMatchSnapshot();
});

it("should handle missing configuration file gracefully", async () => {
mockReadFile.mockRejectedValue(new Error("Config file not found"));

await run();
await run();

expect(mockSetOutput).toHaveBeenCalledWith("processed_text", "");
expect(mockSetOutput).toHaveBeenCalledWith("word_count", 0);
expect(mockSetOutput).toHaveBeenCalledWith("sum", 0);
expect(mockSetOutput).toHaveBeenCalledWith("average", 0);
expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: Config file not found");
});
});
});

0 comments on commit 9edcc99

Please sign in to comment.