|
| 1 | +const sinon = require("sinon"); |
| 2 | +var proxyquire = require("proxyquire"); |
| 3 | + |
| 4 | +import { afterEach, beforeEach } from "mocha"; |
| 5 | + |
| 6 | +suite("create command", () => { |
| 7 | + const targetUri = { fsPath: "/home/dart_frog" }; |
| 8 | + |
| 9 | + let vscodeStub: any; |
| 10 | + let childProcessStub: any; |
| 11 | + let command: any; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + vscodeStub = { |
| 15 | + window: { |
| 16 | + showErrorMessage: sinon.stub(), |
| 17 | + showInputBox: sinon.stub(), |
| 18 | + showOpenDialog: sinon.stub(), |
| 19 | + withProgress: sinon.stub(), |
| 20 | + }, |
| 21 | + }; |
| 22 | + childProcessStub = { |
| 23 | + exec: sinon.stub(), |
| 24 | + }; |
| 25 | + |
| 26 | + command = proxyquire("../../../commands/create", { |
| 27 | + vscode: vscodeStub, |
| 28 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 29 | + child_process: childProcessStub, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + sinon.restore(); |
| 35 | + }); |
| 36 | + |
| 37 | + test("project input box is shown with directory name as value", async () => { |
| 38 | + await command.create(targetUri); |
| 39 | + |
| 40 | + sinon.assert.calledOnceWithExactly(vscodeStub.window.showInputBox, { |
| 41 | + prompt: "Project name", |
| 42 | + value: "dart_frog", |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + suite("file open dialog", () => { |
| 47 | + test("is shown when Uri is undefined", async () => { |
| 48 | + vscodeStub.window.showOpenDialog.returns(Promise.resolve(undefined)); |
| 49 | + |
| 50 | + await command.create(); |
| 51 | + |
| 52 | + sinon.assert.calledOnceWithExactly(vscodeStub.window.showOpenDialog, { |
| 53 | + canSelectMany: false, |
| 54 | + openLabel: "Select a folder or file to create the project in", |
| 55 | + canSelectFolders: true, |
| 56 | + canSelectFiles: true, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test("is not shown when Uri is defined", async () => { |
| 61 | + await command.create(targetUri); |
| 62 | + |
| 63 | + sinon.assert.notCalled(vscodeStub.window.showOpenDialog); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + suite("error message", () => { |
| 68 | + test("is shown when prompt is undefined", async () => { |
| 69 | + vscodeStub.window.showInputBox.returns(undefined); |
| 70 | + |
| 71 | + await command.create(targetUri); |
| 72 | + |
| 73 | + sinon.assert.calledOnceWithExactly( |
| 74 | + vscodeStub.window.showErrorMessage, |
| 75 | + "Please enter a project name" |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test("is shown when prompt is empty", async () => { |
| 80 | + vscodeStub.window.showInputBox.returns(""); |
| 81 | + |
| 82 | + await command.create(targetUri); |
| 83 | + |
| 84 | + sinon.assert.calledOnceWithExactly( |
| 85 | + vscodeStub.window.showErrorMessage, |
| 86 | + "Please enter a project name" |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test("is shown when prompt is white spaced", async () => { |
| 91 | + vscodeStub.window.showInputBox.returns(" "); |
| 92 | + |
| 93 | + await command.create(targetUri); |
| 94 | + |
| 95 | + sinon.assert.calledOnceWithExactly( |
| 96 | + vscodeStub.window.showErrorMessage, |
| 97 | + "Please enter a project name" |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + test("is not shown when prompt is valid", async () => { |
| 102 | + vscodeStub.window.showInputBox.returns("my_project"); |
| 103 | + |
| 104 | + await command.create(targetUri); |
| 105 | + |
| 106 | + sinon.assert.neverCalledWith( |
| 107 | + vscodeStub.window.showErrorMessage, |
| 108 | + "Please enter a project name" |
| 109 | + ); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + suite("progress", () => { |
| 114 | + test("is shown when prompt is valid", async () => { |
| 115 | + const projectName = "my_project"; |
| 116 | + vscodeStub.window.showInputBox.returns(projectName); |
| 117 | + |
| 118 | + await command.create(targetUri); |
| 119 | + |
| 120 | + sinon.assert.calledOnceWithMatch(vscodeStub.window.withProgress, { |
| 121 | + location: 15, |
| 122 | + title: `Creating ${projectName} Dart Frog Project...`, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + test("is not shown when prompt is undefined", async () => { |
| 127 | + vscodeStub.window.showInputBox.returns(undefined); |
| 128 | + |
| 129 | + await command.create(targetUri); |
| 130 | + |
| 131 | + sinon.assert.notCalled(vscodeStub.window.withProgress); |
| 132 | + }); |
| 133 | + |
| 134 | + test("is not shown when prompt is empty", async () => { |
| 135 | + vscodeStub.window.showInputBox.returns(""); |
| 136 | + |
| 137 | + await command.create(targetUri); |
| 138 | + |
| 139 | + sinon.assert.notCalled(vscodeStub.window.withProgress); |
| 140 | + }); |
| 141 | + |
| 142 | + test("is not shown when prompt is white spaced", async () => { |
| 143 | + vscodeStub.window.showInputBox.returns(" "); |
| 144 | + |
| 145 | + await command.create(targetUri); |
| 146 | + |
| 147 | + sinon.assert.notCalled(vscodeStub.window.withProgress); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + test("runs `dart_frog create` command when project name is valid and uri is defined", async () => { |
| 152 | + vscodeStub.window.showInputBox.returns("my_project"); |
| 153 | + |
| 154 | + await command.create(targetUri); |
| 155 | + |
| 156 | + const progressFunction = vscodeStub.window.withProgress.getCall(0).args[1]; |
| 157 | + await progressFunction(); |
| 158 | + |
| 159 | + sinon.assert.calledOnceWithMatch( |
| 160 | + childProcessStub.exec, |
| 161 | + "dart_frog create 'my_project'", |
| 162 | + { cwd: targetUri.fsPath } |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + test("runs `dart_frog create` command when project name is valid and uri not defined", async () => { |
| 167 | + vscodeStub.window.showOpenDialog.returns(Promise.resolve([targetUri])); |
| 168 | + vscodeStub.window.showInputBox.returns("my_project"); |
| 169 | + |
| 170 | + await command.create(); |
| 171 | + |
| 172 | + const progressFunction = vscodeStub.window.withProgress.getCall(0).args[1]; |
| 173 | + await progressFunction(); |
| 174 | + |
| 175 | + sinon.assert.calledOnceWithMatch( |
| 176 | + childProcessStub.exec, |
| 177 | + "dart_frog create 'my_project'", |
| 178 | + { cwd: targetUri.fsPath } |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + test("shows error when `dart_frog create` command fails", async () => { |
| 183 | + const error = new Error("Command failed"); |
| 184 | + const createCommand = "dart_frog create 'my_project'"; |
| 185 | + |
| 186 | + vscodeStub.window.showInputBox.returns("my_project"); |
| 187 | + childProcessStub.exec.withArgs(createCommand).yields(error); |
| 188 | + |
| 189 | + await command.create(targetUri); |
| 190 | + |
| 191 | + const progressFunction = vscodeStub.window.withProgress.getCall(0).args[1]; |
| 192 | + await progressFunction(); |
| 193 | + |
| 194 | + sinon.assert.calledWith(vscodeStub.window.showErrorMessage, error.message); |
| 195 | + }); |
| 196 | +}); |
0 commit comments