Skip to content

Commit c30fbef

Browse files
committed
Fix tests and extract some messages as constants
1 parent 748f753 commit c30fbef

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ export class PlaygroundStoreUrls {
1212
public static GOOGLE_PLAY_URL = "https://play.google.com/store/apps/details?id=org.nativescript.play";
1313
public static APP_STORE_URL = "https://itunes.apple.com/us/app/nativescript-playground/id1263543946?mt=8&ls=1";
1414
}
15+
16+
export class PreviewAppMessages {
17+
public static PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP = "Plugin %s is not included in preview app on device %s and will not work.";
18+
public static PLUGIN_WITH_LOWER_VERSION_IN_PREVIEW_APP = "Plugin %s has local version %s but preview app on device %s has version %s. Some functionalities may not work.";
19+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as path from "path";
22
import * as semver from "semver";
3+
import * as util from "util";
34
import { Device } from "nativescript-preview-sdk";
5+
import { PreviewAppMessages } from "./preview-app-constants";
46

57
export class PreviewAppPluginsService implements IPreviewAppPluginsService {
68
constructor(private $fs: IFileSystem,
@@ -18,11 +20,11 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
1820
this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`);
1921

2022
if (!devicePluginVersion) {
21-
this.$logger.warn(`Plugin ${localPlugin} is not included in preview app and will not work.`);
23+
this.$logger.warn(util.format(PreviewAppMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, device.id));
2224
}
2325

2426
if (devicePluginVersion && semver.gt(semver.coerce(localPluginVersion), semver.coerce(devicePluginVersion))) {
25-
this.$logger.warn(`Plugin ${localPlugin} has local version ${localPluginVersion} but preview app has version ${devicePluginVersion}. Some functionalities may not work.`);
27+
this.$logger.warn(util.format(PreviewAppMessages.PLUGIN_WITH_LOWER_VERSION_IN_PREVIEW_APP, localPlugin, localPluginVersion, device.id, devicePluginVersion));
2628
}
2729
});
2830
}

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

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IActOptions {
2121

2222
interface IAssertOptions {
2323
checkWarnings?: boolean;
24-
checkQrCode?: boolean;
24+
isComparePluginsOnDeviceCalled?: boolean;
2525
}
2626

2727
interface IActInput {
@@ -31,16 +31,20 @@ interface IActInput {
3131
actOptions?: IActOptions;
3232
}
3333

34-
let isGenerateQrCodeForCurrentAppCalled = false;
34+
let isComparePluginsOnDeviceCalled = false;
3535
let applyChangesParams: FilePayload[] = [];
3636
let readTextParams: string[] = [];
3737
let warnParams: string[] = [];
3838
const nativeFilesWarning = "Unable to apply changes from App_Resources folder. You need to build your application in order to make changes in App_Resources folder.";
3939

4040
const projectDirPath = "/path/to/my/project";
4141
const platformsDirPath = path.join(projectDirPath, "platforms");
42+
const normalizedPlatformName = 'iOS';
43+
const platformLowerCase = normalizedPlatformName.toLowerCase();
4244

43-
const deviceMockData = <Device>{ };
45+
const deviceMockData = <Device>{
46+
platform: normalizedPlatformName
47+
};
4448
const defaultProjectFiles = [
4549
"my/test/file1.js",
4650
"my/test/file2.js",
@@ -91,7 +95,8 @@ function createTestInjector(options?: {
9195
});
9296
injector.register("platformsData", {
9397
getPlatformData: () => ({
94-
appDestinationDirectoryPath: platformsDirPath
98+
appDestinationDirectoryPath: platformsDirPath,
99+
normalizedPlatformName
95100
})
96101
});
97102
injector.register("projectDataService", {
@@ -101,14 +106,11 @@ function createTestInjector(options?: {
101106
});
102107
injector.register("previewSdkService", PreviewSdkServiceMock);
103108
injector.register("previewAppPluginsService", {
104-
comparePluginsOnDevice: async () => ({})
105-
});
106-
injector.register("projectFilesManager", ProjectFilesManager);
107-
injector.register("playgroundQrCodeGenerator", {
108-
generateQrCodeForCurrentApp: async () => {
109-
isGenerateQrCodeForCurrentAppCalled = true;
109+
comparePluginsOnDevice: async () => {
110+
isComparePluginsOnDeviceCalled = true;
110111
}
111112
});
113+
injector.register("projectFilesManager", ProjectFilesManager);
112114
injector.register("previewAppLiveSyncService", PreviewAppLiveSyncService);
113115
injector.register("fs", {
114116
readText: (filePath: string) => {
@@ -123,7 +125,18 @@ function createTestInjector(options?: {
123125
}
124126
});
125127
injector.register("localToDevicePathDataFactory", {});
126-
injector.register("projectFilesProvider", {});
128+
injector.register("projectFilesProvider", {
129+
getProjectFileInfo: (filePath: string, platform: string) => {
130+
return {
131+
filePath,
132+
onDeviceFileName: path.basename(filePath),
133+
shouldIncludeFile: true
134+
};
135+
}
136+
});
137+
injector.register("hooksService", {
138+
executeBeforeHooks: () => ({})
139+
});
127140

128141
return injector;
129142
}
@@ -173,13 +186,13 @@ async function assert(expectedFiles: string[], options?: IAssertOptions) {
173186
chai.assert.deepEqual(warnParams, [nativeFilesWarning]);
174187
}
175188

176-
if (options.checkQrCode) {
177-
chai.assert.isTrue(isGenerateQrCodeForCurrentAppCalled);
189+
if (options.isComparePluginsOnDeviceCalled) {
190+
chai.assert.isTrue(isComparePluginsOnDeviceCalled);
178191
}
179192
}
180193

181194
function reset() {
182-
isGenerateQrCodeForCurrentAppCalled = false;
195+
isComparePluginsOnDeviceCalled = false;
183196
applyChangesParams = [];
184197
readTextParams = [];
185198
warnParams = [];
@@ -234,18 +247,12 @@ describe("previewAppLiveSyncService", () => {
234247

235248
let testCases: ITestCase[] = [
236249
{
237-
name: "should generate qrcode when no devices are emitted",
238-
actOptions: {
239-
emitDeviceConnected: false
240-
}
241-
},
242-
{
243-
name: "should generate qrcode when devices are emitted"
250+
name: "should compare local plugins and plugins from preview app when devices are emitted"
244251
}
245252
];
246253

247254
testCases = testCases.map(testCase => {
248-
testCase.assertOptions = { checkQrCode: true };
255+
testCase.assertOptions = { isComparePluginsOnDeviceCalled: true };
249256
return testCase;
250257
});
251258

@@ -259,27 +266,27 @@ describe("previewAppLiveSyncService", () => {
259266
{
260267
name: ".ts files",
261268
appFiles: ["dir1/file.js", "file.ts"],
262-
expectedFiles: ["dir1/file.js"]
269+
expectedFiles: [`dir1/file.${platformLowerCase}.js`]
263270
},
264271
{
265272
name: ".sass files",
266273
appFiles: ["myDir1/mySubDir/myfile.css", "myDir1/mySubDir/myfile.sass"],
267-
expectedFiles: ["myDir1/mySubDir/myfile.css"]
274+
expectedFiles: [`myDir1/mySubDir/myfile.${platformLowerCase}.css`]
268275
},
269276
{
270277
name: ".scss files",
271278
appFiles: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.scss", "my/file.js"],
272-
expectedFiles: ["myDir1/mySubDir/myfile1.css", "my/file.js"]
279+
expectedFiles: [`myDir1/mySubDir/myfile1.${platformLowerCase}.css`, `my/file.${platformLowerCase}.js`]
273280
},
274281
{
275282
name: ".less files",
276283
appFiles: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.less", "my/file.js"],
277-
expectedFiles: ["myDir1/mySubDir/myfile1.css", "my/file.js"]
284+
expectedFiles: [`myDir1/mySubDir/myfile1.${platformLowerCase}.css`, `my/file.${platformLowerCase}.js`]
278285
},
279286
{
280287
name: ".DS_Store file",
281288
appFiles: ["my/test/file.js", ".DS_Store"],
282-
expectedFiles: ["my/test/file.js"]
289+
expectedFiles: [`my/test/file.${platformLowerCase}.js`]
283290
}
284291
];
285292

@@ -309,7 +316,12 @@ describe("previewAppLiveSyncService", () => {
309316
const noAppFilesTestCases: ITestCase[] = [
310317
{
311318
name: "should transfer correctly default project files",
312-
expectedFiles: defaultProjectFiles
319+
expectedFiles: defaultProjectFiles.map(filePath => {
320+
const basename = path.basename(filePath);
321+
const extname = path.extname(filePath);
322+
const parts = basename.split(".");
323+
return path.join(path.dirname(filePath), `${parts[0]}.${platformLowerCase}${extname}`);
324+
})
313325
}
314326
];
315327

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Yok } from "../../../lib/common/yok";
22
import { PreviewAppPluginsService } from "../../../lib/services/livesync/playground/preview-app-plugins-service";
33
import { Device } from "nativescript-preview-sdk";
44
import { assert } from "chai";
5+
import * as util from "util";
6+
import { PreviewAppMessages } from "../../../lib/services/livesync/playground/preview-app-constants";
57

68
let readJsonParams: string[] = [];
79
let warnParams: string[] = [];
@@ -27,9 +29,11 @@ function createTestInjector(localPlugins: IStringDictionary): IInjector {
2729
return injector;
2830
}
2931

32+
const deviceId = "myTestDeviceId";
33+
3034
function createDevice(plugins: string): Device {
3135
return {
32-
id: "myTestDeviceId",
36+
id: deviceId,
3337
platform: "iOS",
3438
model: "myTestDeviceModel",
3539
name: "myTestDeviceName",
@@ -67,7 +71,7 @@ describe("previewAppPluginsService", () => {
6771
"tns-core-modules": "~4.2.0"
6872
},
6973
expectedWarnings: [
70-
"Plugin nativescript-facebook is not included in preview app and will not work."
74+
util.format(PreviewAppMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-facebook", deviceId)
7175
]
7276
},
7377
{
@@ -80,9 +84,9 @@ describe("previewAppPluginsService", () => {
8084
previewAppPlugins: {
8185
},
8286
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."
87+
util.format(PreviewAppMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-facebook", deviceId),
88+
util.format(PreviewAppMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-theme-core", deviceId),
89+
util.format(PreviewAppMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "tns-core-modules", deviceId)
8690
]
8791
},
8892
{
@@ -94,7 +98,7 @@ describe("previewAppPluginsService", () => {
9498
"nativescript-theme-core": "1.0.4"
9599
},
96100
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."
101+
util.format(PreviewAppMessages.PLUGIN_WITH_LOWER_VERSION_IN_PREVIEW_APP, "nativescript-theme-core", "1.1.4", deviceId, "1.0.4")
98102
]
99103
},
100104
{

0 commit comments

Comments
 (0)