Skip to content

Commit 4b7bd96

Browse files
committed
test: add tests for preview-schema-service
1 parent 9536b60 commit 4b7bd96

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

test/services/playground/preview-app-livesync-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PreviewSdkServiceMock extends EventEmitter implements IPreviewSdkService {
7373
return "my_cool_qr_code_url";
7474
}
7575

76-
public initialize(getInitialFiles: (device: Device) => Promise<FilesPayload>) {
76+
public initialize(projectDir: string, getInitialFiles: (device: Device) => Promise<FilesPayload>) {
7777
this.getInitialFiles = async (device) => {
7878
const filesPayload = await getInitialFiles(device);
7979
initialFiles.push(...filesPayload.files);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Yok } from "../../../lib/common/yok";
2+
import { PreviewSchemaService } from "../../../lib/services/livesync/playground/preview-schema-service";
3+
import { PubnubKeys } from "../../../lib/services/livesync/playground/preview-app-constants";
4+
import { assert } from "chai";
5+
6+
function createTestInjector(): IInjector {
7+
const injector = new Yok();
8+
injector.register("previewSchemaService", PreviewSchemaService);
9+
injector.register("projectDataService", () => ({}));
10+
injector.register("errors", () => ({}));
11+
12+
return injector;
13+
}
14+
15+
const nsPlaySchema = {
16+
name: 'nsplay',
17+
scannerAppId: 'org.nativescript.play',
18+
scannerAppStoreId: '1263543946',
19+
previewAppId: 'org.nativescript.preview',
20+
previewAppStoreId: '1264484702',
21+
msvKey: 'cli',
22+
publishKey: PubnubKeys.PUBLISH_KEY,
23+
subscribeKey: PubnubKeys.SUBSCRIBE_KEY,
24+
default: true
25+
};
26+
27+
const ksPlaySchema = {
28+
name: 'ksplay',
29+
scannerAppId: 'com.kinvey.scanner',
30+
scannerAppStoreId: '1263543946',
31+
previewAppId: 'com.kinvey.preview',
32+
previewAppStoreId: '1264484702',
33+
msvKey: 'kinveyStudio',
34+
publishKey: PubnubKeys.PUBLISH_KEY,
35+
subscribeKey: PubnubKeys.SUBSCRIBE_KEY
36+
};
37+
38+
describe.only("PreviewSchemaService", () => {
39+
let injector: IInjector;
40+
let previewSchemaService: IPreviewSchemaService;
41+
42+
beforeEach(() => {
43+
injector = createTestInjector();
44+
previewSchemaService = injector.resolve("previewSchemaService");
45+
});
46+
47+
const testCases = [
48+
{
49+
name: "should return default nsplay schema when no previewAppSchema in nsconfig",
50+
previewAppSchema: <any>null,
51+
expectedSchema: nsPlaySchema
52+
},
53+
{
54+
name: "should return nsplay schema when { 'previewAppSchema': 'nsplay' } in nsconfig",
55+
previewAppSchema: "nsplay",
56+
expectedSchema: nsPlaySchema
57+
},
58+
{
59+
name: "should return ksplay schema when { 'previewAppSchema': 'ksplay' } in nsconfig",
60+
previewAppSchema: "ksplay",
61+
expectedSchema: ksPlaySchema
62+
},
63+
{
64+
name: "should throw an error when invalid previewAppSchema is specified in nsconfig",
65+
previewAppSchema: "someInvalidSchema",
66+
expectedToThrow: true
67+
}
68+
];
69+
70+
_.each(testCases, testCase => {
71+
it(`${testCase.name}`, () => {
72+
const projectDataService = injector.resolve("projectDataService");
73+
projectDataService.getProjectData = () => ({ previewAppSchema: testCase.previewAppSchema });
74+
75+
let actualError = null;
76+
if (testCase.expectedToThrow) {
77+
const errors = injector.resolve("errors");
78+
errors.failWithoutHelp = (err: string) => actualError = err;
79+
}
80+
81+
const schemaData = previewSchemaService.getSchemaData("someTestProjectDir");
82+
83+
assert.deepEqual(schemaData, testCase.expectedSchema);
84+
if (testCase.expectedToThrow) {
85+
assert.isNotNull(actualError);
86+
}
87+
});
88+
});
89+
});

test/services/preview-sdk-service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ import { PreviewSdkService } from "../../lib/services/livesync/playground/previe
22
import { Yok } from "../../lib/common/yok";
33
import { assert } from "chai";
44
import { LoggerStub } from "../stubs";
5+
import { PreviewSchemaService } from "../../lib/services/livesync/playground/preview-schema-service";
56

67
const createTestInjector = (): IInjector => {
78
const testInjector = new Yok();
89
testInjector.register("logger", LoggerStub);
910
testInjector.register("config", {});
1011
testInjector.register("previewSdkService", PreviewSdkService);
1112
testInjector.register("previewDevicesService", {});
13+
testInjector.register("previewSchemaService", PreviewSchemaService);
1214
testInjector.register("previewAppLogProvider", {});
1315
testInjector.register("httpClient", {
1416
httpRequest: async (options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> => undefined
1517
});
1618
testInjector.register("projectDataService", {
1719
getProjectData: () => ({})
1820
});
21+
testInjector.register("errors", {});
1922

2023
return testInjector;
2124
};

0 commit comments

Comments
 (0)