Skip to content

Commit 04e741b

Browse files
committed
Add warnings for native files and add unit tests for preview-app-plugins-service
1 parent affbbea commit 04e741b

File tree

6 files changed

+208
-5
lines changed

6 files changed

+208
-5
lines changed

ios/testApp/.vscode/launch.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch on iOS",
9+
"type": "nativescript",
10+
"request": "launch",
11+
"platform": "ios",
12+
"appRoot": "${workspaceRoot}",
13+
"sourceMaps": true,
14+
"watch": true,
15+
"sourceMapPathOverrides": {
16+
"webpack:///*": "${workspaceRoot}/app/*"
17+
},
18+
"debugServer": 4712
19+
},
20+
{
21+
"name": "Attach on iOS",
22+
"type": "nativescript",
23+
"request": "attach",
24+
"platform": "ios",
25+
"appRoot": "${workspaceRoot}",
26+
"sourceMaps": true,
27+
"watch": false,
28+
"sourceMapPathOverrides": {
29+
"webpack:///*": "${workspaceRoot}/app/*"
30+
}
31+
},
32+
{
33+
"name": "Launch on Android",
34+
"type": "nativescript",
35+
"request": "launch",
36+
"platform": "android",
37+
"appRoot": "${workspaceRoot}",
38+
"sourceMaps": true,
39+
"watch": true,
40+
"sourceMapPathOverrides": {
41+
"webpack:///*": "${workspaceRoot}/app/*"
42+
}
43+
},
44+
{
45+
"name": "Attach on Android",
46+
"type": "nativescript",
47+
"request": "attach",
48+
"platform": "android",
49+
"appRoot": "${workspaceRoot}",
50+
"sourceMaps": true,
51+
"watch": false,
52+
"sourceMapPathOverrides": {
53+
"webpack:///*": "${workspaceRoot}/app/*"
54+
}
55+
}
56+
]
57+
}

lib/definitions/preview-app-livesync.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ declare global {
1010
interface IPreviewAppLiveSyncData extends IProjectDir, IAppFilesUpdaterOptionsComposition, IEnvOptions { }
1111

1212
interface IPreviewSdkService extends NodeJS.EventEmitter {
13-
qrCodeUrl: string;
1413
connectedDevices: Device[];
1514
initialize(): void;
1615
applyChanges(files: FilePayload[]): Promise<void>;
16+
shortenQrCodeUrl(): Promise<string>;
1717
stop(): void;
1818
}
1919

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
3535
}
3636

3737
private async generateQRCode(): Promise<void> {
38-
this.$qrCodeTerminalService.generate(this.$previewSdkService.qrCodeUrl);
38+
const qrCodeUrl = await this.$previewSdkService.shortenQrCodeUrl();
39+
this.$qrCodeTerminalService.generate(qrCodeUrl);
3940
}
4041

4142
private async trySyncFilesOnDevice(data: IPreviewAppLiveSyncData, device: Device, files?: string[]): Promise<void> {
4243
await this.$previewAppPluginsService.comparePluginsOnDevice(device);
44+
this.showWarningsForNativeFiles(files);
4345

4446
this.$logger.info(`Start syncing changes on device ${device.id}.`);
4547

@@ -112,5 +114,15 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
112114
};
113115
await this.$platformService.preparePlatform(prepareInfo);
114116
}
117+
118+
private async showWarningsForNativeFiles(files: string[]): Promise<void> {
119+
if (files && files.length) {
120+
for (const file of files) {
121+
if (file.indexOf(APP_RESOURCES_FOLDER_NAME) > -1) {
122+
this.$logger.warn(`Unable to apply changes from ${APP_RESOURCES_FOLDER_NAME} folder. You need to build your application in order to make changes in ${APP_RESOURCES_FOLDER_NAME} folder.`);
123+
}
124+
}
125+
}
126+
}
115127
}
116128
$injector.register("previewAppLiveSyncService", PreviewAppLiveSyncService);

lib/services/livesync/playground/preview-app-plugins-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
2222
}
2323

2424
if (devicePluginVersion && semver.gt(semver.coerce(localPluginVersion), semver.coerce(devicePluginVersion))) {
25-
this.$logger.warn(`Plugin ${localPlugin} has local version ${localPluginVersion} but preview app has ${devicePluginVersion} version of plugin. Some functionalities may not work.`);
25+
this.$logger.warn(`Plugin ${localPlugin} has local version ${localPluginVersion} but preview app has version ${devicePluginVersion}. Some functionalities may not work.`);
2626
}
2727
});
2828
}

lib/services/livesync/playground/preview-sdk-service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class PreviewSdkService extends EventEmitter implements IPreviewSdkServic
1414
super();
1515
}
1616

17-
public get qrCodeUrl(): string {
18-
return `nsplay://boot?instanceId=${this.instanceId}&pKey=${PubnubKeys.PUBLISH_KEY}&sKey=${PubnubKeys.SUBSCRIBE_KEY}&template=play-ng`;
17+
public async shortenQrCodeUrl(): Promise<string> {
18+
return this.qrCodeUrl;
1919
}
2020

2121
public initialize(): void {
@@ -40,6 +40,10 @@ export class PreviewSdkService extends EventEmitter implements IPreviewSdkServic
4040
this.messagingService.stop();
4141
}
4242

43+
private get qrCodeUrl(): string {
44+
return `nsplay://boot?instanceId=${this.instanceId}&pKey=${PubnubKeys.PUBLISH_KEY}&sKey=${PubnubKeys.SUBSCRIBE_KEY}&template=play-ng`;
45+
}
46+
4347
private getInitConfig(): Config {
4448
return {
4549
pubnubPublishKey: PubnubKeys.PUBLISH_KEY,
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { Yok } from "../../../lib/common/yok";
2+
import { PreviewAppPluginsService } from "../../../lib/services/livesync/playground/preview-app-plugins-service";
3+
import { Device } from "nativescript-preview-sdk";
4+
import { assert } from "chai";
5+
6+
let readJsonParams: string[] = [];
7+
let warnParams: string[] = [];
8+
9+
function createTestInjector(localPlugins: IStringDictionary): IInjector {
10+
const injector = new Yok();
11+
injector.register("fs", {
12+
readJson: (filePath: string) => {
13+
readJsonParams.push(filePath);
14+
return {
15+
dependencies: localPlugins
16+
};
17+
}
18+
});
19+
injector.register("logger", {
20+
trace: () => ({}),
21+
warn: (message: string) => warnParams.push(message)
22+
});
23+
injector.register("projectData", {
24+
projectDir: "testProjectDir"
25+
});
26+
injector.register("previewAppPluginsService", PreviewAppPluginsService);
27+
return injector;
28+
}
29+
30+
function createDevice(plugins: string): Device {
31+
return {
32+
id: "myTestDeviceId",
33+
platform: "iOS",
34+
model: "myTestDeviceModel",
35+
name: "myTestDeviceName",
36+
osVersion: "10.0",
37+
previewAppVersion: "28.0",
38+
runtimeVersion: "4.3.0",
39+
plugins,
40+
pluginsExpanded: false
41+
};
42+
}
43+
44+
function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictionary): any {
45+
const injector = createTestInjector(localPlugins);
46+
const previewAppPluginsService = injector.resolve("previewAppPluginsService");
47+
const device = createDevice(JSON.stringify(previewAppPlugins));
48+
49+
return {
50+
previewAppPluginsService,
51+
device
52+
};
53+
}
54+
55+
describe.only("previewAppPluginsService", () => {
56+
describe("comparePluginsOnDevice", () => {
57+
const testCases = [
58+
{
59+
name: "should show warning for plugin not included in preview app",
60+
localPlugins: {
61+
"nativescript-facebook": "2.2.3",
62+
"nativescript-theme-core": "~1.0.4",
63+
"tns-core-modules": "~4.2.0"
64+
},
65+
previewAppPlugins: {
66+
"nativescript-theme-core": "~1.0.4",
67+
"tns-core-modules": "~4.2.0"
68+
},
69+
expectedWarnings: [
70+
"Plugin nativescript-facebook is not included in preview app and will not work."
71+
]
72+
},
73+
{
74+
name: "should show warnings for plugins not included in preview app",
75+
localPlugins: {
76+
"nativescript-facebook": "2.2.3",
77+
"nativescript-theme-core": "~1.0.4",
78+
"tns-core-modules": "~4.2.0"
79+
},
80+
previewAppPlugins: {
81+
},
82+
expectedWarnings: [
83+
"Plugin nativescript-facebook is not included in preview app and will not work.",
84+
"Plugin nativescript-theme-core is not included in preview app and will not work.",
85+
"Plugin tns-core-modules is not included in preview app and will not work."
86+
]
87+
},
88+
{
89+
name: "should show warning for plugin which local version is greater than preview app's version",
90+
localPlugins: {
91+
"nativescript-theme-core": "1.1.4"
92+
},
93+
previewAppPlugins: {
94+
"nativescript-theme-core": "1.0.4"
95+
},
96+
expectedWarnings: [
97+
"Plugin nativescript-theme-core has local version 1.1.4 but preview app has version 1.0.4. Some functionalities may not work."
98+
]
99+
},
100+
{
101+
name: "should not show warnings when all plugins are included in preview app",
102+
localPlugins: {
103+
"nativescript-theme-core": "1.0.4",
104+
"nativescript-facebook": "2.2.3"
105+
},
106+
previewAppPlugins: {
107+
"nativescript-theme-core": "1.1.4",
108+
"nativescript-facebook": "2.2.3"
109+
},
110+
expectedWarnings: <string[]>[]
111+
}
112+
];
113+
114+
afterEach(() => {
115+
warnParams = [];
116+
readJsonParams = [];
117+
});
118+
119+
for (const testCase of testCases) {
120+
it(`${testCase.name}`, async () => {
121+
const { previewAppPluginsService, device } = setup(testCase.localPlugins, testCase.previewAppPlugins);
122+
123+
await previewAppPluginsService.comparePluginsOnDevice(device);
124+
125+
assert.equal(warnParams.length, testCase.expectedWarnings.length);
126+
testCase.expectedWarnings.forEach(warning => assert.include(warnParams, warning));
127+
});
128+
}
129+
});
130+
});

0 commit comments

Comments
 (0)