Skip to content

Commit adca13b

Browse files
committed
Rewrite tests.
1 parent ce93e02 commit adca13b

File tree

2 files changed

+48
-112
lines changed

2 files changed

+48
-112
lines changed

lib/commands/update.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from "path";
2+
import * as constants from "../constants";
23

34
export class UpdateCommand implements ICommand {
45
public allowedParameters: ICommandParameter[] = [];
@@ -14,11 +15,11 @@ export class UpdateCommand implements ICommand {
1415
this.$projectData.initializeProjectData();
1516
}
1617

17-
private folders: string[] = ["lib", "hooks", "platforms", "node_modules"];
18-
private tempFolder: string = ".tmp_backup";
18+
static readonly folders: string[] = ["lib", "hooks", "platforms", "node_modules"];
19+
static readonly tempFolder: string = ".tmp_backup";
1920

2021
public async execute(args: string[]): Promise<void> {
21-
const tmpDir = path.join(this.$projectData.projectDir, this.tempFolder);
22+
const tmpDir = path.join(this.$projectData.projectDir, UpdateCommand.tempFolder);
2223

2324
try {
2425
this.backup(tmpDir);
@@ -62,7 +63,7 @@ export class UpdateCommand implements ICommand {
6263
await this.$pluginsService.remove("tns-core-modules", this.$projectData);
6364
await this.$pluginsService.remove("tns-core-modules-widgets", this.$projectData);
6465

65-
for (const folder of this.folders) {
66+
for (const folder of UpdateCommand.folders) {
6667
this.$fs.deleteDirectory(path.join(this.$projectData.projectDir, folder));
6768
}
6869

@@ -100,8 +101,8 @@ export class UpdateCommand implements ICommand {
100101
}
101102

102103
private restoreBackup(tmpDir: string): void {
103-
this.$fs.copyFile(path.join(tmpDir, "package.json"), this.$projectData.projectDir);
104-
for (const folder of this.folders) {
104+
this.$fs.copyFile(path.join(tmpDir, constants.PACKAGE_JSON_FILE_NAME), this.$projectData.projectDir);
105+
for (const folder of UpdateCommand.folders) {
105106
this.$fs.deleteDirectory(path.join(this.$projectData.projectDir, folder));
106107

107108
const folderToCopy = path.join(tmpDir, folder);
@@ -115,8 +116,8 @@ export class UpdateCommand implements ICommand {
115116
private backup(tmpDir: string): void {
116117
this.$fs.deleteDirectory(tmpDir);
117118
this.$fs.createDirectory(tmpDir);
118-
this.$fs.copyFile(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
119-
for (const folder of this.folders) {
119+
this.$fs.copyFile(path.join(this.$projectData.projectDir, constants.PACKAGE_JSON_FILE_NAME), tmpDir);
120+
for (const folder of UpdateCommand.folders) {
120121
const folderToCopy = path.join(this.$projectData.projectDir, folder);
121122
if (this.$fs.exists(folderToCopy)) {
122123
this.$fs.copyFile(folderToCopy, tmpDir);

test/update.ts

+39-104
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function createTestInjector(
7171
return testInjector;
7272
}
7373

74-
describe.only("update command method tests", () => {
74+
describe("update command method tests", () => {
7575
describe("canExecute", () => {
7676
it("calls platform service validate", async () => {
7777
let validated = false;
@@ -127,129 +127,64 @@ describe.only("update command method tests", () => {
127127
sandbox.restore();
128128
});
129129

130-
it("calls backup and executeCore", async () => {
131-
const testInjector = createTestInjector();
132-
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
133-
const executeCoreStub: sinon.SinonStub = sinon.stub(updateCommand as any, "executeCore");
134-
const backupStub: sinon.SinonStub = sinon.stub(updateCommand as any, "backup");
135-
updateCommand.execute(["3.3.0"]);
136-
137-
assert.isTrue(backupStub.called);
138-
assert.isTrue(executeCoreStub.called);
139-
});
140-
141-
it("if backup fails, execute core not called and temp removed", async () => {
142-
const testInjector = createTestInjector();
130+
it("if backup fails, pltforms not deleted and added, temp removed", async () => {
131+
const installedPlatforms: string[] = ["android"];
132+
const testInjector = createTestInjector(installedPlatforms);
143133
const fs = testInjector.resolve("fs");
144134
const deleteDirectory: sinon.SinonStub = sandbox.stub(fs, "deleteDirectory");
135+
const platformService = testInjector.resolve("platformService");
136+
sandbox.stub(fs, "copyFile").throws();
137+
sandbox.spy(platformService, "addPlatforms");
138+
sandbox.spy(platformService, "removePlatforms");
145139
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
146-
sandbox.stub(updateCommand as any, "backup").throws();
147-
const executeCoreStub: sinon.SinonStub = sinon.stub(updateCommand as any, "executeCore");
148-
updateCommand.execute(["3.3.0"]);
149-
150-
assert.isFalse(executeCoreStub.called);
151-
assert.isTrue(deleteDirectory.calledWith(path.join(projectFolder, (updateCommand as any).tempFolder)));
152-
});
153-
});
154-
155-
describe("backup", () => {
156-
let sandbox: sinon.SinonSandbox;
157-
158-
beforeEach(() => {
159-
sandbox = sinon.sandbox.create();
160-
});
161-
162-
afterEach(() => {
163-
sandbox.restore();
164-
});
165-
166-
it("calls copy to temp for package.json and folders", async () => {
167-
const testInjector = createTestInjector();
168-
const fs = testInjector.resolve("fs");
169-
const copyFileStub = sandbox.stub(fs, "copyFile");
170-
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
171-
sinon.stub(updateCommand as any, "executeCore");
172-
updateCommand.execute(["3.3.0"]);
173-
174-
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, "package.json")));
175-
for (const folder of (updateCommand as any).folders) {
176-
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, folder)));
177-
}
178-
});
179-
});
180-
181-
describe("backup", () => {
182-
let sandbox: sinon.SinonSandbox;
183140

184-
beforeEach(() => {
185-
sandbox = sinon.sandbox.create();
186-
});
187-
188-
afterEach(() => {
189-
sandbox.restore();
141+
return updateCommand.execute(["3.3.0"]).then(() => {
142+
assert.isTrue(deleteDirectory.calledWith(path.join(projectFolder, UpdateCommand.tempFolder)));
143+
assert.isFalse(platformService.removePlatforms.calledWith(installedPlatforms));
144+
assert.isFalse(platformService.addPlatforms.calledWith(installedPlatforms));
145+
});
190146
});
191147

192-
it("calls copy to temp for package.json and folders", async () => {
148+
it("calls copy to temp for package.json and folders(backup)", async () => {
193149
const testInjector = createTestInjector();
194150
const fs = testInjector.resolve("fs");
195151
const copyFileStub = sandbox.stub(fs, "copyFile");
196152
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
197-
const tempDir = path.join(projectFolder, (updateCommand as any).tempFolder);
198-
sinon.stub(updateCommand as any, "executeCore");
199-
(updateCommand as any).backup(tempDir);
200-
201-
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, "package.json")));
202-
for (const folder of (updateCommand as any).folders) {
203-
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, folder)));
204-
}
205-
});
206-
});
207-
208-
describe("restoreBackup", () => {
209-
let sandbox: sinon.SinonSandbox;
210-
211-
beforeEach(() => {
212-
sandbox = sinon.sandbox.create();
213-
});
214-
215-
afterEach(() => {
216-
sandbox.restore();
153+
return updateCommand.execute(["3.3.0"]).then( () => {
154+
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, "package.json")));
155+
for (const folder of UpdateCommand.folders) {
156+
assert.isTrue(copyFileStub.calledWith(path.join(projectFolder, folder)));
157+
}
158+
});
217159
});
218160

219-
it("calls copy to temp for package.json and folders", async () => {
161+
it("calls copy from temp for package.json and folders to project folder(restore)", async () => {
220162
const testInjector = createTestInjector();
163+
testInjector.resolve("platformService").removePlatforms = () => {
164+
throw new Error();
165+
};
221166
const fs = testInjector.resolve("fs");
167+
const deleteDirectoryStub: sinon.SinonStub = sandbox.stub(fs, "deleteDirectory");
222168
const copyFileStub = sandbox.stub(fs, "copyFile");
223169
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
224-
const tempDir = path.join(projectFolder, (updateCommand as any).tempFolder, projectFolder);
225-
sinon.stub(updateCommand as any, "executeCore");
226-
(updateCommand as any).restoreBackup(tempDir);
227-
228-
assert.isTrue(copyFileStub.calledWith(path.join(tempDir, "package.json"), projectFolder));
229-
for (const folder of (updateCommand as any).folders) {
230-
assert.isTrue(copyFileStub.calledWith(path.join(tempDir, folder), projectFolder));
231-
}
232-
});
233-
});
234-
235-
describe("executeCore", () => {
236-
let sandbox: sinon.SinonSandbox;
170+
const tempDir = path.join(projectFolder, UpdateCommand.tempFolder);
237171

238-
beforeEach(() => {
239-
sandbox = sinon.sandbox.create();
240-
});
241-
242-
afterEach(() => {
243-
sandbox.restore();
172+
return updateCommand.execute(["3.3.0"]).then(() => {
173+
assert.isTrue(copyFileStub.calledWith(path.join(tempDir, "package.json"), projectFolder));
174+
for (const folder of UpdateCommand.folders) {
175+
assert.isTrue(deleteDirectoryStub.calledWith(path.join(projectFolder, folder)));
176+
assert.isTrue(copyFileStub.calledWith(path.join(tempDir, folder), projectFolder));
177+
}
178+
});
244179
});
245180

246181
it("calls remove for all falders", async () => {
247182
const testInjector = createTestInjector();
248183
const fs = testInjector.resolve("fs");
249184
const deleteDirectory: sinon.SinonStub = sandbox.stub(fs, "deleteDirectory");
250185
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
251-
return (updateCommand as any).executeCore([]).then(() => {
252-
for (const folder of (updateCommand as any).folders) {
186+
return updateCommand.execute([]).then(() => {
187+
for (const folder of UpdateCommand.folders) {
253188
assert.isTrue(deleteDirectory.calledWith(path.join(projectFolder, folder)));
254189
}
255190
});
@@ -262,7 +197,7 @@ describe.only("update command method tests", () => {
262197
sandbox.spy(platformService, "addPlatforms");
263198
sandbox.spy(platformService, "removePlatforms");
264199
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
265-
return (updateCommand as any).executeCore([]).then(() => {
200+
return updateCommand.execute([]).then(() => {
266201
assert(platformService.removePlatforms.calledWith(installedPlatforms));
267202
assert(platformService.addPlatforms.calledWith(installedPlatforms));
268203
});
@@ -276,7 +211,7 @@ describe.only("update command method tests", () => {
276211
sandbox.spy(platformService, "addPlatforms");
277212
sandbox.spy(platformService, "removePlatforms");
278213
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
279-
return (updateCommand as any).executeCore([version]).then(() => {
214+
return updateCommand.execute([version]).then(() => {
280215
assert(platformService.addPlatforms.calledWith([`${installedPlatforms}@${version}`]));
281216
});
282217
});
@@ -288,7 +223,7 @@ describe.only("update command method tests", () => {
288223
sandbox.spy(pluginsService, "add");
289224
sandbox.spy(pluginsService, "ensureAllDependenciesAreInstalled");
290225
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
291-
return (updateCommand as any).executeCore([]).then(() => {
226+
return updateCommand.execute([]).then(() => {
292227
assert(pluginsService.add.calledWith("tns-core-modules"));
293228
assert(pluginsService.remove.calledWith("tns-core-modules"));
294229
assert(pluginsService.remove.calledWith("tns-core-modules-widgets"));
@@ -304,7 +239,7 @@ describe.only("update command method tests", () => {
304239
sandbox.spy(pluginsService, "add");
305240
sandbox.spy(pluginsService, "ensureAllDependenciesAreInstalled");
306241
const updateCommand = testInjector.resolve<UpdateCommand>(UpdateCommand);
307-
return (updateCommand as any).executeCore([version]).then(() => {
242+
return updateCommand.execute([version]).then(() => {
308243
assert(pluginsService.add.calledWith(`tns-core-modules@${version}`));
309244
});
310245
});

0 commit comments

Comments
 (0)