Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/action/actions/new.action.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterEach, describe, expect, it, vi } from "vitest";

import { type Input } from "@lib/input";

import { NewAction } from "./new.action";

type Internals = Record<
"collectValues" | "scaffold" | "installDependencies" | "setupGitRepository",
() => unknown
>;

const proto = NewAction.prototype as unknown as Internals;

const run = (values: { git: boolean; skipInstall: boolean }, install: boolean, git: boolean) => {
vi.spyOn(proto, "collectValues").mockResolvedValue({
...values,
directory: "app",
gitRemote: "git@example.com:me/app.git",
});
vi.spyOn(proto, "scaffold").mockResolvedValue(undefined);
vi.spyOn(proto, "installDependencies").mockResolvedValue(install);
const setupGit = vi.spyOn(proto, "setupGitRepository").mockResolvedValue(git);
return { result: new NewAction().handle(new Map() as Input, new Map() as Input), setupGit };
};

describe("NewAction git setup", () => {
afterEach(() => vi.restoreAllMocks());

it("fails when git init or git remote add fails", async () => {
const { result, setupGit } = run({ git: true, skipInstall: true }, true, false);
expect(await result).toEqual({ success: false });
expect(setupGit).toHaveBeenCalledOnce();
});

it("succeeds when the git setup succeeds", async () => {
expect(await run({ git: true, skipInstall: false }, true, true).result).toEqual({
success: true,
});
});

it("still fails on a failed install when the git setup succeeds", async () => {
expect(await run({ git: true, skipInstall: false }, false, true).result).toEqual({
success: false,
});
});

it("does not set up git without --git", async () => {
const { result, setupGit } = run({ git: false, skipInstall: true }, true, false);
expect(await result).toEqual({ success: true });
expect(setupGit).not.toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/action/actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class NewAction extends AbstractAction {
res = await this.installDependencies(values.packageManager, distDir);
}

if (values.git) await this.setupGitRepository(values.gitRemote, distDir);
if (values.git) res = (await this.setupGitRepository(values.gitRemote, distDir)) && res;

return { success: res };
}
Expand Down
Loading