test: refactor testing in inputs.test.ts

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

View File

@ -1,4 +1,5 @@
import { jest } from "@jest/globals"; import { jest } from "@jest/globals";
import type { Inputs } from "./inputs.js";
jest.unstable_mockModule("@actions/core", () => ({ jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(), getBooleanInput: jest.fn(),
@ -7,23 +8,7 @@ jest.unstable_mockModule("@actions/core", () => ({
})); }));
describe("get action inputs", () => { describe("get action inputs", () => {
describe("with default values", () => { const defaultInputs: Inputs = {
beforeEach(async () => {
const { getBooleanInput, getInput, getMultilineInput } = await import(
"@actions/core"
);
jest.mocked(getBooleanInput).mockReturnValue(false);
jest.mocked(getInput).mockReturnValue("");
jest.mocked(getMultilineInput).mockReturnValue([]);
});
it("should get the action inputs", async () => {
const { getInputs } = await import("./inputs.js");
const inputs = getInputs();
expect(inputs).toStrictEqual({
sourceDir: ".", sourceDir: ".",
buildDir: "build", buildDir: "build",
generator: "", generator: "",
@ -33,84 +18,77 @@ describe("get action inputs", () => {
cxxFlags: "", cxxFlags: "",
options: [], options: [],
args: [], args: [],
runBuild: false, runBuild: true,
buildArgs: [], buildArgs: [],
}); };
});
});
describe("with specified values", () => {
beforeEach(async () => { beforeEach(async () => {
const { getBooleanInput, getInput, getMultilineInput } = await import( const core = await import("@actions/core");
"@actions/core"
);
jest.mocked(getBooleanInput).mockImplementation((name) => { jest.mocked(core.getBooleanInput).mockImplementation((name) => {
switch (name) { return name == "run-build";
case "run-build":
return true;
}
throw new Error(`invalid input name: ${name}`);
}); });
jest.mocked(getInput).mockImplementation((name) => { jest.mocked(core.getInput).mockReturnValue("");
switch (name) { jest.mocked(core.getMultilineInput).mockReturnValue([]);
case "source-dir":
return "some-source";
case "build-dir":
return "some-build";
case "generator":
return "some-generator";
case "c-compiler":
return "some-c-compiler";
case "cxx-compiler":
return "some-cxx-compiler";
}
throw new Error(`invalid input name: ${name}`);
}); });
jest.mocked(getMultilineInput).mockImplementation((name) => { it("should get the action inputs with nothing specified", async () => {
switch (name) {
case "c-flags":
return ["some-c-flag another-c-flag", "some-other-c-flag"];
case "cxx-flags":
return ["some-cxx-flag another-cxx-flag", "some-other-cxx-flag"];
case "options":
return ["some-options another-options", "some-other-options"];
case "args":
return ["some-args another-args", "some-other-args"];
case "build-args":
return [
"some-build-args another-build-args",
"some-other-build-args",
];
}
throw new Error(`invalid input name: ${name}`);
});
});
it("should get the action inputs", async () => {
const { getInputs } = await import("./inputs.js"); const { getInputs } = await import("./inputs.js");
const inputs = getInputs(); expect(getInputs()).toStrictEqual(defaultInputs);
});
expect(inputs).toStrictEqual({ it("should get the action inputs with all specified", async () => {
sourceDir: "some-source", const { getInputs } = await import("./inputs.js");
buildDir: "some-build", const core = await import("@actions/core");
generator: "some-generator",
cCompiler: "some-c-compiler", jest.mocked(core.getBooleanInput).mockReturnValue(false);
cxxCompiler: "some-cxx-compiler",
cFlags: "some-c-flag another-c-flag some-other-c-flag", jest.mocked(core.getInput).mockImplementation((name) => {
cxxFlags: "some-cxx-flag another-cxx-flag some-other-cxx-flag", switch (name) {
options: ["some-options", "another-options", "some-other-options"], case "source-dir":
args: ["some-args", "another-args", "some-other-args"], return "project";
runBuild: true, case "build-dir":
buildArgs: [ return "output";
"some-build-args", case "generator":
"another-build-args", return "Ninja";
"some-other-build-args", case "c-compiler":
], return "clang";
}); case "cxx-compiler":
return "clang++";
}
return "";
});
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
switch (name) {
case "c-flags":
return ["-Werror -Wall", "-Wextra"];
case "cxx-flags":
return ["-Werror -Wall", "-Wextra -Wpedantic"];
case "options":
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({
sourceDir: "project",
buildDir: "output",
generator: "Ninja",
cCompiler: "clang",
cxxCompiler: "clang++",
cFlags: "-Werror -Wall -Wextra",
cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: false,
buildArgs: ["--target", "foo", "--parallel", "8"],
}); });
}); });
}); });