test: simplify mocking in inputs.test.ts testing

This commit is contained in:
Alfi Maulana 2024-03-24 23:43:04 +07:00
parent 667f2d1359
commit 1ac900e15c
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4

View File

@ -22,15 +22,28 @@ describe("get action inputs", () => {
buildArgs: [], buildArgs: [],
}; };
let booleanInputs: { [key: string]: boolean };
let stringInputs: { [key: string]: string };
let multilineInputs: { [key: string]: string[] };
beforeEach(async () => { beforeEach(async () => {
const core = await import("@actions/core"); const core = await import("@actions/core");
booleanInputs = { "run-build": true };
stringInputs = {};
multilineInputs = {};
jest.mocked(core.getBooleanInput).mockImplementation((name) => { jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return name == "run-build"; return booleanInputs[name] ?? false;
}); });
jest.mocked(core.getInput).mockReturnValue(""); jest.mocked(core.getInput).mockImplementation((name) => {
jest.mocked(core.getMultilineInput).mockReturnValue([]); return stringInputs[name] ?? "";
});
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
return multilineInputs[name] ?? [];
});
}); });
it("should get the action inputs with nothing specified", async () => { it("should get the action inputs with nothing specified", async () => {
@ -41,41 +54,29 @@ describe("get action inputs", () => {
it("should get the action inputs with all specified", async () => { it("should get the action inputs with all specified", async () => {
const { getInputs } = await import("./inputs.js"); const { getInputs } = await import("./inputs.js");
const core = await import("@actions/core");
jest.mocked(core.getBooleanInput).mockReturnValue(false); booleanInputs = {
...booleanInputs,
"run-build": false,
};
jest.mocked(core.getInput).mockImplementation((name) => { stringInputs = {
switch (name) { ...stringInputs,
case "source-dir": "source-dir": "project",
return "project"; "build-dir": "output",
case "build-dir": generator: "Ninja",
return "output"; "c-compiler": "clang",
case "generator": "cxx-compiler": "clang++",
return "Ninja"; };
case "c-compiler":
return "clang";
case "cxx-compiler":
return "clang++";
}
return "";
});
jest.mocked(core.getMultilineInput).mockImplementation((name) => { multilineInputs = {
switch (name) { ...multilineInputs,
case "c-flags": "c-flags": ["-Werror -Wall", "-Wextra"],
return ["-Werror -Wall", "-Wextra"]; "cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"],
case "cxx-flags": options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
return ["-Werror -Wall", "-Wextra -Wpedantic"]; args: ["-Wdev -Wdeprecated", "--fresh"],
case "options": "build-args": ["--target foo", "--parallel 8"],
return ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"]; };
case "args":
return ["-Wdev -Wdeprecated", "--fresh"];
case "build-args":
return ["--target foo", "--parallel 8"];
}
return [];
});
expect(getInputs()).toStrictEqual({ expect(getInputs()).toStrictEqual({
sourceDir: "project", sourceDir: "project",