|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { GitHub } from "../shared/options/getGitHub.js"; |
| 4 | +import { Options } from "../shared/types.js"; |
| 5 | +import { migrateWithOptions } from "./migrateWithOptions.js"; |
| 6 | + |
| 7 | +vi.mock("../shared/cli/spinners.js", () => ({ |
| 8 | + async withSpinner(_label: string, task: () => Promise<void>) { |
| 9 | + await task(); |
| 10 | + }, |
| 11 | + withSpinners: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +const mockDetectExistingContributors = vi.fn(); |
| 15 | + |
| 16 | +vi.mock("../steps/detectExistingContributors.js", () => ({ |
| 17 | + get detectExistingContributors() { |
| 18 | + return mockDetectExistingContributors; |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +const mockFinalizeDependencies = vi.fn(); |
| 23 | + |
| 24 | +vi.mock("../steps/finalizeDependencies.js", () => ({ |
| 25 | + get finalizeDependencies() { |
| 26 | + return mockFinalizeDependencies; |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +const mockInitializeGitHubRepository = vi.fn(); |
| 31 | + |
| 32 | +vi.mock("../steps/initializeGitHubRepository/index.js", () => ({ |
| 33 | + get initializeGitHubRepository() { |
| 34 | + return mockInitializeGitHubRepository; |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +const mockPopulateCSpellDictionary = vi.fn(); |
| 39 | + |
| 40 | +vi.mock("../steps/populateCSpellDictionary.js", () => ({ |
| 41 | + get populateCSpellDictionary() { |
| 42 | + return mockPopulateCSpellDictionary; |
| 43 | + }, |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("../steps/runCleanup.js", () => ({ |
| 47 | + runCleanup: vi.fn(), |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock("../shared/cli/spinners.js", () => ({ |
| 51 | + async withSpinner(_label: string, task: () => Promise<void>) { |
| 52 | + await task(); |
| 53 | + }, |
| 54 | + withSpinners: vi.fn(), |
| 55 | +})); |
| 56 | + |
| 57 | +const mockGitHubAndOptions = vi.fn(); |
| 58 | + |
| 59 | +vi.mock("../shared/options/readOptions.js", () => ({ |
| 60 | + get GitHubAndOptions() { |
| 61 | + return mockGitHubAndOptions; |
| 62 | + }, |
| 63 | +})); |
| 64 | + |
| 65 | +const optionsBase = {} as Options; |
| 66 | + |
| 67 | +describe("migrateWithOptions", () => { |
| 68 | + it("runs initializeGitHubRepository when github is truthy", async () => { |
| 69 | + const github = { |
| 70 | + octokit: {}, |
| 71 | + } as GitHub; |
| 72 | + |
| 73 | + await migrateWithOptions({ |
| 74 | + github, |
| 75 | + options: optionsBase, |
| 76 | + }); |
| 77 | + |
| 78 | + expect(mockInitializeGitHubRepository).toHaveBeenCalledWith( |
| 79 | + github.octokit, |
| 80 | + optionsBase, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not run initializeGitHubRepository when github is falsy", async () => { |
| 85 | + const options = { |
| 86 | + ...optionsBase, |
| 87 | + excludeAllContributors: true, |
| 88 | + }; |
| 89 | + |
| 90 | + await migrateWithOptions({ |
| 91 | + github: undefined, |
| 92 | + options, |
| 93 | + }); |
| 94 | + |
| 95 | + expect(mockInitializeGitHubRepository).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("does not run detectExistingContributors when excludeAllContributors is true", async () => { |
| 99 | + const options = { |
| 100 | + ...optionsBase, |
| 101 | + excludeAllContributors: true, |
| 102 | + skipAllContributorsApi: false, |
| 103 | + }; |
| 104 | + |
| 105 | + await migrateWithOptions({ |
| 106 | + github: undefined, |
| 107 | + options, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(mockDetectExistingContributors).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("does not run detectExistingContributors when skipAllContributorsApi is true", async () => { |
| 114 | + const options = { |
| 115 | + ...optionsBase, |
| 116 | + excludeAllContributors: false, |
| 117 | + skipAllContributorsApi: true, |
| 118 | + }; |
| 119 | + |
| 120 | + await migrateWithOptions({ |
| 121 | + github: undefined, |
| 122 | + options, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(mockDetectExistingContributors).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("runs detectExistingContributors when excludeAllContributors and skipAllContributorsApi are false", async () => { |
| 129 | + const options = { |
| 130 | + ...optionsBase, |
| 131 | + excludeAllContributors: false, |
| 132 | + skipAllContributorsApi: false, |
| 133 | + }; |
| 134 | + |
| 135 | + await migrateWithOptions({ |
| 136 | + github: undefined, |
| 137 | + options, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(mockDetectExistingContributors).toHaveBeenCalledWith( |
| 141 | + undefined, |
| 142 | + options, |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("runs finalizeDependencies when skipInstall is false", async () => { |
| 147 | + const options = { |
| 148 | + ...optionsBase, |
| 149 | + skipInstall: false, |
| 150 | + }; |
| 151 | + |
| 152 | + await migrateWithOptions({ |
| 153 | + github: undefined, |
| 154 | + options, |
| 155 | + }); |
| 156 | + |
| 157 | + expect(mockFinalizeDependencies).toHaveBeenCalledWith(options); |
| 158 | + }); |
| 159 | + |
| 160 | + it("does not run finalizeDependencies when skipInstall is true", async () => { |
| 161 | + const options = { |
| 162 | + ...optionsBase, |
| 163 | + skipInstall: true, |
| 164 | + }; |
| 165 | + |
| 166 | + await migrateWithOptions({ |
| 167 | + github: undefined, |
| 168 | + options, |
| 169 | + }); |
| 170 | + |
| 171 | + expect(mockFinalizeDependencies).not.toHaveBeenCalled(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("runs populateCSpellDictionary when excludeLintSpelling is false", async () => { |
| 175 | + const options = { |
| 176 | + ...optionsBase, |
| 177 | + excludeLintSpelling: false, |
| 178 | + }; |
| 179 | + |
| 180 | + await migrateWithOptions({ |
| 181 | + github: undefined, |
| 182 | + options, |
| 183 | + }); |
| 184 | + |
| 185 | + expect(mockPopulateCSpellDictionary).toHaveBeenCalled(); |
| 186 | + }); |
| 187 | + |
| 188 | + it("does not run populateCSpellDictionary when excludeLintSpelling is true", async () => { |
| 189 | + const options = { |
| 190 | + ...optionsBase, |
| 191 | + excludeLintSpelling: true, |
| 192 | + }; |
| 193 | + |
| 194 | + await migrateWithOptions({ |
| 195 | + github: undefined, |
| 196 | + options, |
| 197 | + }); |
| 198 | + |
| 199 | + expect(mockPopulateCSpellDictionary).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | +}); |
0 commit comments