-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreuninstall.ts
154 lines (130 loc) · 5.55 KB
/
preuninstall.ts
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
import { assert } from "chai";
import { Yok } from "../../yok";
import { PreUninstallCommand } from "../../commands/preuninstall";
import * as path from "path";
const helpers = require("../../helpers");
describe("preuninstall", () => {
const profileDir = "profileDir";
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("extensibilityService", {
removeAllExtensions: (): void => undefined
});
testInjector.register("fs", {
deleteFile: (pathToFile: string): void => undefined
});
testInjector.register("packageInstallationManager", {
clearInspectorCache: (): void => undefined
});
testInjector.register("settingsService", {
getProfileDir: (): string => profileDir
});
testInjector.register("opener", {
open: (filename: string, appname?: string): void => undefined
});
testInjector.register("analyticsService", {
trackEventActionInGoogleAnalytics: async (data: IEventActionData): Promise<void> => undefined,
finishTracking: async(): Promise<void> => undefined
});
testInjector.registerCommand("dev-preuninstall", PreUninstallCommand);
return testInjector;
};
it("cleans the KillSwitches/cli file", async () => {
helpers.doesCurrentNpmCommandMatch = () => false;
const testInjector = createTestInjector();
const fs = testInjector.resolve<IFileSystem>("fs");
const deletedFiles: string[] = [];
fs.deleteFile = (pathToFile: string) => {
deletedFiles.push(pathToFile);
};
const preUninstallCommand: ICommand = testInjector.resolveCommand("dev-preuninstall");
await preUninstallCommand.execute([]);
assert.deepEqual(deletedFiles, [path.join(profileDir, "KillSwitches", "cli")]);
});
it("tracks correct data in analytics", async () => {
const testData: { isInteractive: boolean, isIntentionalUninstall: boolean, expecteEventLabelData: string }[] = [
{
isIntentionalUninstall: false,
isInteractive: false,
expecteEventLabelData: `isIntentionalUninstall__false__isInteractive__false`
},
{
isIntentionalUninstall: true,
isInteractive: false,
expecteEventLabelData: `isIntentionalUninstall__true__isInteractive__false`
},
{
isIntentionalUninstall: false,
isInteractive: true,
expecteEventLabelData: `isIntentionalUninstall__false__isInteractive__true`
},
{
isIntentionalUninstall: true,
isInteractive: true,
expecteEventLabelData: `isIntentionalUninstall__true__isInteractive__true`
}
];
const testInjector = createTestInjector();
const analyticsService = testInjector.resolve<IAnalyticsService>("analyticsService");
let trackedData: IEventActionData[] = [];
analyticsService.trackEventActionInGoogleAnalytics = async (data: IEventActionData): Promise<void> => {
trackedData.push(data);
};
let isFinishTrackingCalled = false;
analyticsService.finishTracking = async (): Promise<void> => {
isFinishTrackingCalled = true;
};
const preUninstallCommand: ICommand = testInjector.resolveCommand("dev-preuninstall");
for (const testCase of testData) {
helpers.isInteractive = () => testCase.isInteractive;
helpers.doesCurrentNpmCommandMatch = () => testCase.isIntentionalUninstall;
isFinishTrackingCalled = false;
await preUninstallCommand.execute([]);
assert.deepEqual(trackedData, [{
action: "Uninstall CLI",
additionalData: testCase.expecteEventLabelData
}]);
assert.isTrue(isFinishTrackingCalled, "At the end of the command, finishTracking must be called");
trackedData = [];
}
});
it("removes all extensions and inspector cache when uninstall command is executed", async () => {
helpers.doesCurrentNpmCommandMatch = () => true;
helpers.isInteractive = () => false;
const testInjector = createTestInjector();
const fs = testInjector.resolve<IFileSystem>("fs");
const deletedFiles: string[] = [];
fs.deleteFile = (pathToFile: string) => {
deletedFiles.push(pathToFile);
};
const extensibilityService = testInjector.resolve<IExtensibilityService>("extensibilityService");
let isRemoveAllExtensionsCalled = false;
extensibilityService.removeAllExtensions = () => {
isRemoveAllExtensionsCalled = true;
};
const packageInstallationManager = testInjector.resolve<IPackageInstallationManager>("packageInstallationManager");
let isClearInspectorCacheCalled = false;
packageInstallationManager.clearInspectorCache = () => {
isClearInspectorCacheCalled = true;
};
const preUninstallCommand: ICommand = testInjector.resolveCommand("dev-preuninstall");
await preUninstallCommand.execute([]);
assert.deepEqual(deletedFiles, [path.join(profileDir, "KillSwitches", "cli")]);
assert.isTrue(isRemoveAllExtensionsCalled, "When uninstall is called, `removeAllExtensions` method must be called");
assert.isTrue(isClearInspectorCacheCalled, "When uninstall is called, `clearInspectorCache` method must be called");
});
// disabled (6/24/2020)
// it("opens the uninstall feedback form when terminal is interactive and uninstall is called", async () => {
// helpers.doesCurrentNpmCommandMatch = () => true;
// helpers.isInteractive = () => true;
// const testInjector = createTestInjector();
// const opener = testInjector.resolve<IOpener>("opener");
// const openParams: any[] = [];
// opener.open = (filename: string, appname?: string) => {
// openParams.push({ filename, appname });
// };
// const preUninstallCommand: ICommand = testInjector.resolveCommand("dev-preuninstall");
// await preUninstallCommand.execute([]);
// assert.deepEqual(openParams, [{ filename: "https://www.nativescript.org/uninstall-feedback", appname: undefined }]);
// });
});