|
| 1 | +import { Octokit } from "octokit"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { SpinnerTask } from "../shared/cli/spinners.js"; |
| 5 | +import { doesRepositoryExist } from "../shared/doesRepositoryExist.js"; |
| 6 | +import { Options } from "../shared/types.js"; |
| 7 | +import { addToolAllContributors } from "../steps/addToolAllContributors.js"; |
| 8 | +import { finalizeDependencies } from "../steps/finalizeDependencies.js"; |
| 9 | +import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js"; |
| 10 | +import { runCommands } from "../steps/runCommands.js"; |
| 11 | +import { createWithOptions } from "./createWithOptions.js"; |
| 12 | + |
| 13 | +const optionsBase: Options = { |
| 14 | + access: "public", |
| 15 | + author: "Test Author", |
| 16 | + base: "common", |
| 17 | + description: "Test Description", |
| 18 | + directory: "test-directory", |
| 19 | + |
| 20 | + funding: "Test Funding", |
| 21 | + keywords: ["test", "keywords"], |
| 22 | + logo: { alt: "Test Alt", src: "test.png" }, |
| 23 | + mode: "create", |
| 24 | + owner: "Test Owner", |
| 25 | + repository: "test-repo", |
| 26 | + title: "Test Title", |
| 27 | +}; |
| 28 | + |
| 29 | +const mock$ = vi.fn(); |
| 30 | + |
| 31 | +vi.mock("execa", () => ({ |
| 32 | + get $() { |
| 33 | + return mock$; |
| 34 | + }, |
| 35 | +})); |
| 36 | + |
| 37 | +const mockOctokit = new Octokit(); |
| 38 | +const github = { |
| 39 | + auth: "auth-token", |
| 40 | + octokit: mockOctokit, |
| 41 | +}; |
| 42 | + |
| 43 | +vi.mock("../shared/cli/spinners.js", () => ({ |
| 44 | + withSpinner: async <Return>(label: string, task: SpinnerTask<Return>) => { |
| 45 | + return await task(); |
| 46 | + }, |
| 47 | + withSpinners: async ( |
| 48 | + label: string, |
| 49 | + tasks: [string, SpinnerTask<unknown>][], |
| 50 | + ) => { |
| 51 | + for (const [, task] of tasks) { |
| 52 | + await task(); |
| 53 | + } |
| 54 | + }, |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock("../steps/writing/writeStructure.js"); |
| 58 | + |
| 59 | +vi.mock("../steps/writeReadme/index.js"); |
| 60 | + |
| 61 | +vi.mock("../steps/finalizeDependencies.js"); |
| 62 | + |
| 63 | +vi.mock("../steps/runCommands.js"); |
| 64 | + |
| 65 | +vi.mock("../shared/doesRepositoryExist.js", () => ({ |
| 66 | + doesRepositoryExist: vi.fn().mockResolvedValue(true), |
| 67 | +})); |
| 68 | + |
| 69 | +vi.mock("../steps/initializeGitHubRepository/index.js", () => ({ |
| 70 | + initializeGitHubRepository: vi.fn().mockResolvedValue(true), |
| 71 | +})); |
| 72 | + |
| 73 | +vi.mock("../shared/getGitHubUserAsAllContributor.js", () => ({ |
| 74 | + getGitHubUserAsAllContributor: vi.fn().mockResolvedValue({ |
| 75 | + contributions: ["code", "doc"], |
| 76 | + name: "Test User", |
| 77 | + }), |
| 78 | +})); |
| 79 | + |
| 80 | +vi.mock("../steps/addToolAllContributors.js"); |
| 81 | + |
| 82 | +describe("createWithOptions", () => { |
| 83 | + it("calls addToolAllContributors with options when excludeAllContributors is false", async () => { |
| 84 | + const options = { |
| 85 | + ...optionsBase, |
| 86 | + excludeAllContributors: false, |
| 87 | + skipAllContributorsApi: false, |
| 88 | + }; |
| 89 | + |
| 90 | + await createWithOptions({ github, options }); |
| 91 | + expect(addToolAllContributors).toHaveBeenCalledWith(options); |
| 92 | + }); |
| 93 | + |
| 94 | + it("does not call addToolAllContributors when excludeAllContributors is true", async () => { |
| 95 | + const options = { |
| 96 | + ...optionsBase, |
| 97 | + excludeAllContributors: true, |
| 98 | + }; |
| 99 | + |
| 100 | + await createWithOptions({ github, options }); |
| 101 | + expect(addToolAllContributors).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("does not call addToolAllContributors when skipAllContributorsApi is true", async () => { |
| 105 | + const options = { |
| 106 | + ...optionsBase, |
| 107 | + skipAllContributorsApi: true, |
| 108 | + }; |
| 109 | + |
| 110 | + await createWithOptions({ github, options }); |
| 111 | + expect(addToolAllContributors).not.toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("does not call finalizeDependencies or runCommands when skipInstall is true", async () => { |
| 115 | + const options = { |
| 116 | + ...optionsBase, |
| 117 | + skipInstall: true, |
| 118 | + }; |
| 119 | + |
| 120 | + await createWithOptions({ github, options }); |
| 121 | + expect(finalizeDependencies).not.toHaveBeenCalled(); |
| 122 | + expect(runCommands).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("calls finalizeDependencies and runCommands when skipInstall is false", async () => { |
| 126 | + const options = { |
| 127 | + ...optionsBase, |
| 128 | + skipInstall: false, |
| 129 | + }; |
| 130 | + |
| 131 | + await createWithOptions({ github, options }); |
| 132 | + |
| 133 | + expect(finalizeDependencies).toHaveBeenCalledWith(options); |
| 134 | + expect(runCommands).toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("does not initialize GitHub repository if repository does not exist", async () => { |
| 138 | + const options = optionsBase; |
| 139 | + vi.mocked(doesRepositoryExist).mockResolvedValueOnce(false); |
| 140 | + await createWithOptions({ github, options }); |
| 141 | + expect(initializeGitHubRepository).not.toHaveBeenCalled(); |
| 142 | + expect(mock$).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + it("executes git commands when initializing GitHub repository when doesRepositoryExist is true", async () => { |
| 146 | + const options = optionsBase; |
| 147 | + |
| 148 | + vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true); |
| 149 | + await createWithOptions({ github, options }); |
| 150 | + |
| 151 | + expect(mock$.mock.calls).toMatchInlineSnapshot(` |
| 152 | + [ |
| 153 | + [ |
| 154 | + [ |
| 155 | + "git remote add origin https://github.com/", |
| 156 | + "/", |
| 157 | + "", |
| 158 | + ], |
| 159 | + "Test Owner", |
| 160 | + "test-repo", |
| 161 | + ], |
| 162 | + [ |
| 163 | + [ |
| 164 | + "git add -A", |
| 165 | + ], |
| 166 | + ], |
| 167 | + [ |
| 168 | + [ |
| 169 | + "git commit --message ", |
| 170 | + "", |
| 171 | + ], |
| 172 | + "feat: initialized repo ✨", |
| 173 | + ], |
| 174 | + [ |
| 175 | + [ |
| 176 | + "git push -u origin main --force", |
| 177 | + ], |
| 178 | + ], |
| 179 | + ] |
| 180 | + `); |
| 181 | + }); |
| 182 | + |
| 183 | + it("calls doesRepositoryExist with github and options when doesRepositoryExist is true", async () => { |
| 184 | + const options = optionsBase; |
| 185 | + vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true); |
| 186 | + |
| 187 | + await createWithOptions({ github, options }); |
| 188 | + |
| 189 | + expect(doesRepositoryExist).toHaveBeenCalledWith(github.octokit, options); |
| 190 | + expect(initializeGitHubRepository).toHaveBeenCalledWith( |
| 191 | + github.octokit, |
| 192 | + options, |
| 193 | + ); |
| 194 | + }); |
| 195 | +}); |
0 commit comments