Skip to content

Commit 69442c9

Browse files
Fatmerosen-vladimirov
authored andcommitted
test: add unit tests
1 parent f0e77df commit 69442c9

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

test/services/preview-sdk-service.ts

+98-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 { PubnubKeys } from "../../lib/services/livesync/playground/preview-app-constants";
56

67
const getPreviewSdkService = (): IPreviewSdkService => {
78
const testInjector = new Yok();
@@ -19,20 +20,108 @@ const getPreviewSdkService = (): IPreviewSdkService => {
1920

2021
describe('PreviewSdkService', () => {
2122
describe('getQrCodeUrl', () => {
22-
it('sets hmr to 1 when useHotModuleReload is true', async () => {
23-
const sdk = getPreviewSdkService();
23+
describe("hmr", () => {
24+
it('sets hmr to 1 when useHotModuleReload is true', async () => {
25+
const sdk = getPreviewSdkService();
2426

25-
const previewUrl = sdk.getQrCodeUrl({ useHotModuleReload: true });
27+
const previewUrl = sdk.getQrCodeUrl({ useHotModuleReload: true });
2628

27-
assert.isTrue(previewUrl.indexOf("hmr=1") > -1);
29+
assert.isTrue(previewUrl.indexOf("hmr=1") > -1);
30+
});
31+
it('sets hmr to 0 when useHotModuleReload is false', async () => {
32+
const sdk = getPreviewSdkService();
33+
34+
const previewUrl = sdk.getQrCodeUrl({ useHotModuleReload: false });
35+
36+
assert.isTrue(previewUrl.indexOf("hmr=0") > -1);
37+
});
38+
});
39+
40+
describe("schema", () => {
41+
const testCases: [{ name: string, schemaFromApi: string, schemaFromNsConfig: string, expectedSchemaName: string }] = [
42+
{
43+
name: "should return the schema from api",
44+
schemaFromApi: "ksplay",
45+
schemaFromNsConfig: null,
46+
expectedSchemaName: "ksplay"
47+
},
48+
{
49+
name: "should return the schema from nsconfig",
50+
schemaFromApi: null,
51+
schemaFromNsConfig: "ksplay",
52+
expectedSchemaName: "ksplay"
53+
},
54+
{
55+
name: "should return the default schema",
56+
schemaFromApi: null,
57+
schemaFromNsConfig: null,
58+
expectedSchemaName: "nsplay"
59+
}
60+
];
61+
62+
_.each(testCases, testCase => {
63+
it(`${testCase.name}`, () => {
64+
const qrCodeData = { schemaName: testCase.schemaFromApi };
65+
const qrCodeOptions = { nsConfigPreviewAppSchema: testCase.schemaFromNsConfig, qrCodeData, useHotModuleReload: true };
66+
const previewSdkService = getPreviewSdkService();
67+
68+
const qrCodeUrl = previewSdkService.getQrCodeUrl(qrCodeOptions);
69+
70+
assert.deepEqual(qrCodeUrl.split(":")[0], testCase.expectedSchemaName);
71+
});
72+
});
2873
});
29-
});
3074

31-
it('sets hmr to 0 when useHotModuleReload is false', async () => {
32-
const sdk = getPreviewSdkService();
75+
describe("publishKey", () => {
76+
const testCases = [
77+
{
78+
name: "should return the provided key from api",
79+
publishKeyFromApi: "myTestPublishKey",
80+
expectedPublishKey: "myTestPublishKey"
81+
},
82+
{
83+
name: "should return the default key",
84+
publishKeyFromApi: null,
85+
expectedPublishKey: PubnubKeys.PUBLISH_KEY
86+
}
87+
];
3388

34-
const previewUrl = sdk.getQrCodeUrl({ useHotModuleReload: false });
89+
_.each(testCases, testCase => {
90+
it(`${testCase.name}`, () => {
91+
const qrCodeOptions = { projectData: <any>{}, qrCodeData: <any>{ publishKey: testCase.publishKeyFromApi }, useHotModuleReload: true };
92+
const previewSdkService = getPreviewSdkService();
3593

36-
assert.isTrue(previewUrl.indexOf("hmr=0") > -1);
94+
const qrCodeUrl = previewSdkService.getQrCodeUrl(qrCodeOptions);
95+
96+
assert.isTrue(qrCodeUrl.indexOf(`&pKey=${testCase.expectedPublishKey}`) > -1);
97+
});
98+
});
99+
});
100+
101+
describe("subscribeKey", () => {
102+
const testCases = [
103+
{
104+
name: "should return the provided key from api",
105+
subscribeKeyFromApi: "myTestSubscribeKey",
106+
expectedSubscribeKey: "myTestSubscribeKey"
107+
},
108+
{
109+
name: "should return the default key",
110+
subscribeKeyFromApi: null,
111+
expectedSubscribeKey: PubnubKeys.SUBSCRIBE_KEY
112+
}
113+
];
114+
115+
_.each(testCases, testCase => {
116+
it(`${testCase.name}`, () => {
117+
const qrCodeOptions = { projectData: <any>{}, qrCodeData: <any>{ subscribeKey: testCase.subscribeKeyFromApi }, useHotModuleReload: true };
118+
const previewSdkService = getPreviewSdkService();
119+
120+
const qrCodeUrl = previewSdkService.getQrCodeUrl(qrCodeOptions);
121+
122+
assert.isTrue(qrCodeUrl.indexOf(`&sKey=${testCase.expectedSubscribeKey}`) > -1);
123+
});
124+
});
125+
});
37126
});
38127
});

test/stubs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export class ProjectDataStub implements IProjectData {
332332
public podfilePath: string;
333333
public isShared: boolean;
334334
public useLegacyWorkflow: boolean;
335+
public previewAppSchema: string;
335336

336337
public initializeProjectData(projectDir?: string): void {
337338
this.projectDir = this.projectDir || projectDir;

0 commit comments

Comments
 (0)