github-pages-deploy-action/__tests__/git.test.ts

151 lines
3.7 KiB
TypeScript
Raw Normal View History

// Initial env variable setup for tests.
process.env["INPUT_FOLDER"] = "build";
process.env["GITHUB_SHA"] = "123";
import { action } from "../src/constants";
import { deploy, generateBranch, init, switchToBaseBranch } from "../src/git";
2020-01-12 09:26:08 +08:00
import { execute } from "../src/execute";
const originalAction = JSON.stringify(action);
2020-01-12 09:26:08 +08:00
jest.mock("../src/execute", () => ({
execute: jest.fn()
}));
describe("git", () => {
afterEach(() => {
Object.assign(action, JSON.parse(originalAction));
});
describe("init", () => {
it("should execute three commands if a GitHub token is provided", async () => {
Object.assign(action, {
build: "build",
gitHubToken: "123",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
2020-01-15 04:14:15 +08:00
expect(execute).toBeCalledTimes(6);
expect(call).toBe("Initialization step complete...");
});
it("should execute three commands if a Access token is provided", async () => {
Object.assign(action, {
build: "build",
accessToken: "123",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
2020-01-15 04:14:15 +08:00
expect(execute).toBeCalledTimes(6);
expect(call).toBe("Initialization step complete...");
});
it("should fail if there is no provided GitHub Token or Access Token", async () => {
Object.assign(action, {
build: "build",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
expect(execute).toBeCalledTimes(0);
expect(call).toBe("Initialization step complete...");
});
it("should fail if the build folder begins with a /", async () => {
Object.assign(action, {
accessToken: "123",
build: "/",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
expect(execute).toBeCalledTimes(0);
expect(call).toBe("Initialization step complete...");
});
it("should fail if the build folder begins with a ./", async () => {
Object.assign(action, {
accessToken: "123",
build: "./",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
expect(execute).toBeCalledTimes(0);
expect(call).toBe("Initialization step complete...");
});
it("should not fail if root is used", async () => {
Object.assign(action, {
accessToken: "123",
build: ".",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await init();
2020-01-15 04:14:15 +08:00
expect(execute).toBeCalledTimes(6);
expect(call).toBe("Initialization step complete...");
});
});
describe("generateBranch", () => {
it("should execute five commands", async () => {
const call = await generateBranch();
2020-01-15 03:25:10 +08:00
expect(execute).toBeCalledTimes(6);
expect(call).toBe("Deployment branch creation step complete... ✅");
});
});
describe("switchToBaseBranch", () => {
it("should execute one command", async () => {
const call = await switchToBaseBranch();
expect(execute).toBeCalledTimes(1);
expect(call).toBe("Switched to the base branch...");
});
});
describe("deploy", () => {
it("should execute five commands", async () => {
Object.assign(action, {
build: "build",
gitHubToken: "123",
pusher: {
name: "asd",
email: "as@cat"
}
});
const call = await deploy();
// Includes the call to generateBranch
2020-01-15 03:25:10 +08:00
expect(execute).toBeCalledTimes(18);
expect(call).toBe("Commit step complete...");
});
});
});