-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitMode.test.ts
More file actions
156 lines (123 loc) · 5.39 KB
/
Copy pathinitMode.test.ts
File metadata and controls
156 lines (123 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@clack/prompts", () => {
const CANCEL = Symbol("cancel");
return {
CANCEL,
select: vi.fn(),
confirm: vi.fn(),
isCancel: (value: unknown) => value === CANCEL,
};
});
import { CANCEL, confirm, select } from "@clack/prompts";
import { CancelledError } from "../core/cancel.js";
import {
chooseExistingDirectoryAction,
chooseScaffoldTarget,
confirmLocalFallback,
} from "./initMode.js";
interface PromptArgs {
message: string;
options?: Array<{ value: string; label: string; hint?: string }>;
initialValue?: unknown;
}
function lastCall(mock: { mock: { calls: unknown[][] } }): PromptArgs {
return mock.mock.calls[mock.mock.calls.length - 1][0] as PromptArgs;
}
beforeEach(() => {
vi.mocked(select).mockReset();
vi.mocked(confirm).mockReset();
});
describe("chooseExistingDirectoryAction", () => {
it("offers integrate and scaffold when there is something to connect", async () => {
vi.mocked(select).mockResolvedValue("integrate" as never);
await expect(chooseExistingDirectoryAction(true)).resolves.toBe("integrate");
const args = lastCall(vi.mocked(select));
expect(args.options?.map((o) => o.value)).toEqual(["integrate", "scaffold"]);
// Scaffolding copies starter files over whatever is already there, so the
// consequence has to be on screen before the choice is made.
expect(args.options?.[1].hint).toMatch(/overwrite/);
expect(confirm).not.toHaveBeenCalled();
});
// Picking "scaffold here" off a list is not consent to write over existing
// files; the hint on the option is easy to skim past.
it("confirms the overwrite after the scaffold choice is picked", async () => {
vi.mocked(select).mockResolvedValue("scaffold" as never);
vi.mocked(confirm).mockResolvedValue(true as never);
await expect(chooseExistingDirectoryAction(true)).resolves.toBe("scaffold");
expect(confirm).toHaveBeenCalledTimes(1);
const args = lastCall(vi.mocked(confirm));
expect(args.message).toMatch(/overwrite/);
expect(args.initialValue).toBe(false);
});
it("cancels when the overwrite confirmation after the list is declined", async () => {
vi.mocked(select).mockResolvedValue("scaffold" as never);
vi.mocked(confirm).mockResolvedValue(false as never);
await expect(chooseExistingDirectoryAction(true)).rejects.toBeInstanceOf(
CancelledError,
);
});
it("does not confirm anything when integrating", async () => {
vi.mocked(select).mockResolvedValue("integrate" as never);
await expect(chooseExistingDirectoryAction(true)).resolves.toBe("integrate");
expect(confirm).not.toHaveBeenCalled();
});
it("asks for confirmation instead when integrating is not possible", async () => {
vi.mocked(confirm).mockResolvedValue(true as never);
await expect(chooseExistingDirectoryAction(false)).resolves.toBe("scaffold");
expect(select).not.toHaveBeenCalled();
const args = lastCall(vi.mocked(confirm));
expect(args.message).toMatch(/not empty/);
expect(args.message).toMatch(/overwrite/);
// Writing over a directory that already has files is not the safe default.
expect(args.initialValue).toBe(false);
});
it("cancels when the confirmation is declined", async () => {
vi.mocked(confirm).mockResolvedValue(false as never);
await expect(chooseExistingDirectoryAction(false)).rejects.toBeInstanceOf(
CancelledError,
);
});
it("cancels when either prompt is interrupted", async () => {
vi.mocked(select).mockResolvedValue(CANCEL as never);
await expect(chooseExistingDirectoryAction(true)).rejects.toBeInstanceOf(
CancelledError,
);
vi.mocked(confirm).mockResolvedValue(CANCEL as never);
await expect(chooseExistingDirectoryAction(false)).rejects.toBeInstanceOf(
CancelledError,
);
});
});
describe("chooseScaffoldTarget", () => {
it("leads with managed and reports how many applications are available", async () => {
vi.mocked(select).mockResolvedValue("managed" as never);
await expect(chooseScaffoldTarget(3)).resolves.toBe("managed");
const args = lastCall(vi.mocked(select));
expect(args.initialValue).toBe("managed");
expect(args.options?.[0].hint).toContain("3");
expect(args.options?.map((o) => o.value)).toEqual(["managed", "local"]);
});
it("returns the local choice", async () => {
vi.mocked(select).mockResolvedValue("local" as never);
await expect(chooseScaffoldTarget(1)).resolves.toBe("local");
});
it("cancels when interrupted", async () => {
vi.mocked(select).mockResolvedValue(CANCEL as never);
await expect(chooseScaffoldTarget(1)).rejects.toBeInstanceOf(CancelledError);
});
});
describe("confirmLocalFallback", () => {
it("resolves when the developer accepts the local stack", async () => {
vi.mocked(confirm).mockResolvedValue(true as never);
await expect(confirmLocalFallback()).resolves.toBeUndefined();
expect(lastCall(vi.mocked(confirm)).initialValue).toBe(true);
});
it("cancels rather than scaffolding a stack the developer did not want", async () => {
vi.mocked(confirm).mockResolvedValue(false as never);
await expect(confirmLocalFallback()).rejects.toBeInstanceOf(CancelledError);
});
it("cancels when interrupted", async () => {
vi.mocked(confirm).mockResolvedValue(CANCEL as never);
await expect(confirmLocalFallback()).rejects.toBeInstanceOf(CancelledError);
});
});