-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data.ts
145 lines (113 loc) · 4.97 KB
/
project-data.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
import { ProjectData } from "../lib/project-data";
import { Yok } from "../lib/common/yok";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import { assert } from "chai";
import * as stubs from "./stubs";
import * as path from "path";
describe("projectData", () => {
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("projectHelper", {
projectDir: null,
sanitizeName: (name: string) => name
});
testInjector.register("fs", {
exists: () => true,
readJson: (): any => null,
readText: (): any => null
});
testInjector.register("staticConfig", {
CLIENT_NAME_KEY_IN_PROJECT_FILE: "nativescript",
PROJECT_FILE_NAME: "package.json"
});
testInjector.register("errors", stubs.ErrorsStub);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("options", {});
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("androidResourcesMigrationService", {
hasMigrated: () => true
});
testInjector.register("projectData", ProjectData);
return testInjector;
};
const prepareTest = (opts?: { packageJsonData?: { dependencies?: IStringDictionary, devDependencies: IStringDictionary }, nsconfigData?: { shared: boolean } }): IProjectData => {
const testInjector = createTestInjector();
const fs = testInjector.resolve("fs");
fs.exists = (filePath: string) => filePath && (path.basename(filePath) === "package.json" || (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData));
fs.readText = (filePath: string) => {
if (path.basename(filePath) === "package.json") {
return JSON.stringify({
nativescript: {
id: "com.test.testid"
},
dependencies: opts && opts.packageJsonData && opts.packageJsonData.dependencies,
devDependencies: opts && opts.packageJsonData && opts.packageJsonData.devDependencies
});
} else if (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData) {
return JSON.stringify(opts.nsconfigData);
}
return null;
};
const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = "projectDir";
const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();
return projectData;
};
describe("projectType", () => {
const assertProjectType = (dependencies: any, devDependencies: any, expectedProjecType: string) => {
const projectData = prepareTest({
packageJsonData: {
dependencies,
devDependencies
}
});
assert.deepEqual(projectData.projectType, expectedProjecType);
};
it("detects project as Angular when @angular/core exists as dependency", () => {
assertProjectType({ "@angular/core": "*" }, null, "Angular");
});
it("detects project as Angular when nativescript-angular exists as dependency", () => {
assertProjectType({ "nativescript-angular": "*" }, null, "Angular");
});
it("detects project as Vue.js when nativescript-vue exists as dependency", () => {
assertProjectType({ "nativescript-vue": "*" }, null, "Vue.js");
});
it("detects project as Vue.js when nativescript-vue exists as dependency and typescript is devDependency", () => {
assertProjectType({ "nativescript-vue": "*" }, { "typescript": "*" }, "Vue.js");
});
it("detects project as React when react-nativescript exists as dependency and typescript is devDependency", () => {
assertProjectType({ "react-nativescript": "*" }, null, "React");
});
it("detects project as Svelte when react-nativescript exists as dependency and typescript is devDependency", () => {
assertProjectType({ "svelte-native": "*" }, null, "Svelte");
});
it("detects project as TypeScript when nativescript-dev-typescript exists as dependency", () => {
assertProjectType(null, { "nativescript-dev-typescript": "*" }, "Pure TypeScript");
});
it("detects project as TypeScript when typescript exists as dependency", () => {
assertProjectType(null, { "typescript": "*" }, "Pure TypeScript");
});
it("detects project as JavaScript when no other project type is detected", () => {
assertProjectType(null, null, "Pure JavaScript");
});
});
describe("isShared", () => {
it("is false when nsconfig.json does not exist", () => {
const projectData = prepareTest();
assert.isFalse(projectData.isShared);
});
it("is false when nsconfig.json exists, but shared value is not populated", () => {
const projectData = prepareTest({ nsconfigData: { shared: undefined } });
assert.isFalse(projectData.isShared);
});
it("is false when nsconfig.json exists and shared value is false", () => {
const projectData = prepareTest({ nsconfigData: { shared: false } });
assert.isFalse(projectData.isShared);
});
it("is true when nsconfig.json exists and shared value is true", () => {
const projectData = prepareTest({ nsconfigData: { shared: true } });
assert.isTrue(projectData.isShared);
});
});
});