-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplayground-service.ts
212 lines (196 loc) · 8.64 KB
/
playground-service.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { assert } from "chai";
import { FileSystemStub } from "../stubs";
import { PlaygroundService } from "../../lib/services/playground-service";
import { Yok } from "../../lib/common/yok";
let userSettings: any = null;
function createTestInjector(): IInjector {
const testInjector = new Yok();
testInjector.register("playgroundService", PlaygroundService);
testInjector.register("fs", FileSystemStub);
testInjector.register("projectDataService", {});
testInjector.register("userSettingsService", {});
testInjector.register("injector", testInjector);
return testInjector;
}
function mockPlaygroundService(testInjector: IInjector, data?: { projectData?: any, nativescriptKey?: any, userSettingsData?: any}) {
const projectDataService = testInjector.resolve<IProjectDataService>("projectDataService");
projectDataService.getProjectData = () => (data && data.projectData) || <IProjectData>{};
const userSettingsService = testInjector.resolve("userSettingsService");
userSettingsService.getSettingValue = async (keyName: string) => {
return data && data.userSettingsData ? data.userSettingsData[keyName] : null;
};
userSettingsService.saveSettings = async (settings: any) => { userSettings = settings; };
const fs = testInjector.resolve<IFileSystem>("fs");
fs.readJson = () => (data && data.nativescriptKey) || {};
}
describe("PlaygroundService", () => {
let testInjector: IInjector = null;
let playgroundService: IPlaygroundService = null;
beforeEach(() => {
testInjector = createTestInjector();
playgroundService = testInjector.resolve("playgroundService");
});
describe("getPlaygroundInfo", () => {
it("should return null when projectDir is not specified and no playground data is saved in userSettings file", async () => {
mockPlaygroundService(testInjector, { userSettingsData: null });
const result = await playgroundService.getPlaygroundInfo();
assert.equal(result, null);
});
it("should return saved playgroundData from userSettings file when projectDir is not specified and playground data is already saved in userSettings file", async () => {
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: false}}});
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = {id: "test-playground-identifier", usedTutorial: false};
assert.deepEqual(actualResult, expectedResult);
});
it("should return null when projectFile has no nativescript key in package.json file and no playground data is saved in userSettings file", async () => {
mockPlaygroundService(testInjector, { userSettingsData: null });
const result = await playgroundService.getPlaygroundInfo();
assert.equal(result, null);
});
it("should return saved playgroundData from userSettings file when projectFile has no nativescript key in package.json and some playground data is already saved in userSettings file", async () => {
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: true}}});
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = {id: "test-playground-identifier", usedTutorial: true};
assert.deepEqual(actualResult, expectedResult);
});
describe("should return playgroundInfo when project has playground key in package.json", () => {
it("and no usedTutorial", async () => {
const nativescriptKey = {
nativescript: {
playground: {
id: "test-guid"
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = { id: "test-guid", usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
});
it("and usedTutorial is true", async() => {
const nativescriptKey = {
nativescript: {
playground: {
id: "test-guid",
usedTutorial: true
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = { id: 'test-guid', usedTutorial: true };
assert.deepEqual(actualResult, expectedResult);
});
it("and usedTutorial is false", async () => {
const nativescriptKey = {
nativescript: {
playground: {
id: "playground-test-guid",
usedTutorial: false
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = { id: "playground-test-guid", usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
});
});
describe("should return playgroundInfo from userSettings file", () => {
it("when usedTutorial is true", async () => {
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: true}}});
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = { id: 'test-playground-identifier', usedTutorial: true };
assert.deepEqual(actualResult, expectedResult);
});
it("when usedTutorial is false", async () => {
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: false}}});
const actualResult = await playgroundService.getPlaygroundInfo();
const expectedResult = { id: 'test-playground-identifier', usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
});
});
it("should return undefined when userSettings file does not have playground key", async () => {
mockPlaygroundService(testInjector, { userSettingsData: {}});
const actualResult = await playgroundService.getPlaygroundInfo();
assert.deepEqual(actualResult, undefined);
});
it("should replace playgroundId when another id is already saved in userSettings file", async () => {
const nativescriptKey = {
nativescript: {
playground: {
id: "test-guid"
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
let actualResult = await playgroundService.getPlaygroundInfo();
let expectedResult = { id: "test-guid", usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
const secondNativescriptKey = {
nativescript: {
playground: {
id: "another-test-guid"
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey });
actualResult = await playgroundService.getPlaygroundInfo();
expectedResult = { id: 'another-test-guid', usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
assert.deepEqual(userSettings, { playground: { id: 'another-test-guid', usedTutorial: false }});
});
it("should replace usedTutorial when false value is already saved in userSettings file", async () => {
const nativescriptKey = {
nativescript: {
playground: {
id: "test-guid",
usedTutorial: false
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
let actualResult = await playgroundService.getPlaygroundInfo();
let expectedResult = { id: "test-guid", usedTutorial: false };
assert.deepEqual(actualResult, expectedResult);
const secondNativescriptKey = {
nativescript: {
playground: {
id: "another-test-guid",
usedTutorial: true
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey, userSettingsData: {playground: { id: "test-guid", usedTutorial: false }} });
actualResult = await playgroundService.getPlaygroundInfo();
expectedResult = { id: 'another-test-guid', usedTutorial: true };
assert.deepEqual(actualResult, expectedResult);
});
it("shouldn't replace usedTutorial when true value is already saved in userSettings file", async () => {
const nativescriptKey = {
nativescript: {
playground: {
id: "test-guid",
usedTutorial: true
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey });
let actualResult = await playgroundService.getPlaygroundInfo();
let expectedResult = { id: "test-guid", usedTutorial: true };
assert.deepEqual(actualResult, expectedResult);
const secondNativescriptKey = {
nativescript: {
playground: {
id: "another-test-guid",
usedTutorial: false
}
}
};
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey, userSettingsData: {playground: { id: "test-guid", usedTutorial: true }} });
actualResult = await playgroundService.getPlaygroundInfo();
expectedResult = { id: 'another-test-guid', usedTutorial: true };
assert.deepEqual(actualResult, expectedResult);
});
});
});