Skip to content

Commit 0899198

Browse files
committed
chore: fix unit tests
1 parent 265a492 commit 0899198

File tree

2 files changed

+134
-37
lines changed

2 files changed

+134
-37
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { Yok } from "../../../lib/common/yok";
2+
import { PreviewAppFilesService } from "../../../lib/services/livesync/playground/preview-app-files-service";
3+
import { FileSystemStub, LoggerStub } from "../../stubs";
4+
import * as path from "path";
5+
import { assert } from "chai";
6+
import { FilesPayload } from "nativescript-preview-sdk";
7+
8+
const projectDir = "path/to/my/test/project";
9+
const appDirectoryPath = path.join(projectDir, "src");
10+
11+
class ProjectDataServiceMock {
12+
public getProjectData() {
13+
return {
14+
getAppDirectoryPath: () => appDirectoryPath,
15+
appDirectoryPath: () => appDirectoryPath
16+
};
17+
}
18+
}
19+
20+
class PlatformsDataMock {
21+
public getPlatformData(platform: string) {
22+
const appDestinationDirectoryPath = path.join(projectDir, "platforms", platform, "app");
23+
return {
24+
appDestinationDirectoryPath
25+
};
26+
}
27+
}
28+
29+
function createTestInjector(data?: { files: string[] }) {
30+
const injector = new Yok();
31+
injector.register("previewAppFilesService", PreviewAppFilesService);
32+
injector.register("fs", FileSystemStub);
33+
injector.register("logger", LoggerStub);
34+
injector.register("platformsData", PlatformsDataMock);
35+
injector.register("projectDataService", ProjectDataServiceMock);
36+
injector.register("projectFilesManager", {
37+
getProjectFiles: () => data ? data.files : []
38+
});
39+
return injector;
40+
}
41+
42+
function getExpectedResult(data: IPreviewAppLiveSyncData, injector: IInjector, expectedFiles: string[], platform: string): FilesPayload {
43+
const projectData = injector.resolve("projectDataService").getProjectData();
44+
const platformData = injector.resolve("platformsData").getPlatformData(platform);
45+
const files = _.map(expectedFiles, expectedFile => {
46+
return {
47+
event: 'change',
48+
file: data.bundle ? path.relative(path.join(platformData.appDestinationDirectoryPath, "app"), expectedFile) : path.relative(projectData.appDirectoryPath(), expectedFile),
49+
binary: false,
50+
fileContents: undefined
51+
};
52+
});
53+
54+
return {
55+
files,
56+
platform,
57+
hmrMode: data.useHotModuleReload ? 1 : 0,
58+
deviceId: undefined
59+
};
60+
}
61+
62+
describe("PreviewAppFilesService", () => {
63+
const testCases = [
64+
{
65+
name: ".ts files",
66+
files: ["dir1/file.js", "file.ts"],
67+
expectedFiles: [`dir1/file.js`]
68+
},
69+
{
70+
name: ".sass files",
71+
files: ["myDir1/mySubDir/myfile.css", "myDir1/mySubDir/myfile.sass"],
72+
expectedFiles: [`myDir1/mySubDir/myfile.css`]
73+
},
74+
{
75+
name: ".scss files",
76+
files: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.scss", "my/file.js"],
77+
expectedFiles: [`myDir1/mySubDir/myfile1.css`, `my/file.js`]
78+
},
79+
{
80+
name: ".less files",
81+
files: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.less", "my/file.js"],
82+
expectedFiles: [`myDir1/mySubDir/myfile1.css`, `my/file.js`]
83+
},
84+
{
85+
name: ".DS_Store file",
86+
files: ["my/test/file.js", ".DS_Store"],
87+
expectedFiles: [`my/test/file.js`]
88+
}
89+
];
90+
91+
describe("getInitialFilesPayload", () => {
92+
_.each(testCases, testCase => {
93+
_.each(["ios", "android"], platform => {
94+
_.each([true, false], bundle => {
95+
_.each([true, false], useHotModuleReload => {
96+
it(`should exclude ${testCase.name} when { platform: ${platform}, bundle: ${bundle}, useHotModuleReload: ${useHotModuleReload} }`, () => {
97+
const injector = createTestInjector({ files: testCase.files });
98+
const previewAppFilesService = injector.resolve("previewAppFilesService");
99+
const data = { projectDir, bundle, useHotModuleReload, env: {} };
100+
const result = previewAppFilesService.getInitialFilesPayload(data, platform);
101+
const expectedResult = getExpectedResult(data, injector, testCase.expectedFiles, platform);
102+
assert.deepEqual(result, expectedResult);
103+
});
104+
});
105+
});
106+
});
107+
});
108+
});
109+
110+
describe("getFilesPayload", () => {
111+
_.each(testCases, testCase => {
112+
_.each(["ios", "android"], platform => {
113+
_.each([true, false], bundle => {
114+
_.each([true, false], useHotModuleReload => {
115+
it(`should exclude ${testCase.name} when { platform: ${platform}, bundle: ${bundle}, useHotModuleReload: ${useHotModuleReload} }`, () => {
116+
const injector = createTestInjector();
117+
const previewAppFilesService: IPreviewAppFilesService = injector.resolve("previewAppFilesService");
118+
const data = { projectDir, bundle, useHotModuleReload, env: {} };
119+
const result = previewAppFilesService.getFilesPayload(data, { filesToSync: testCase.files }, platform);
120+
const expectedResult = getExpectedResult(data, injector, testCase.expectedFiles, platform);
121+
assert.deepEqual(result, expectedResult);
122+
});
123+
});
124+
});
125+
});
126+
});
127+
});
128+
});

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

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as chai from "chai";
77
import * as path from "path";
88
import { ProjectFilesManager } from "../../../lib/common/services/project-files-manager";
99
import { EventEmitter } from "events";
10+
import { PreviewAppFilesService } from "../../../lib/services/livesync/playground/preview-app-files-service";
1011

1112
interface ITestCase {
1213
name: string;
@@ -101,9 +102,6 @@ function createTestInjector(options?: {
101102

102103
const injector = new Yok();
103104
injector.register("logger", LoggerMock);
104-
injector.register("platformService", {
105-
preparePlatform: async () => ({})
106-
});
107105
injector.register("hmrStatusService", {});
108106
injector.register("errors", ErrorsStub);
109107
injector.register("platformsData", {
@@ -114,7 +112,9 @@ function createTestInjector(options?: {
114112
});
115113
injector.register("projectDataService", {
116114
getProjectData: () => ({
117-
projectDir: projectDirPath
115+
projectDir: projectDirPath,
116+
getAppDirectoryPath: () => path.join(projectDirPath, "app"),
117+
appDirectoryPath: () => path.join(projectDirPath, "app")
118118
})
119119
});
120120
injector.register("previewSdkService", PreviewSdkServiceMock);
@@ -157,6 +157,7 @@ function createTestInjector(options?: {
157157
injector.register("previewDevicesService", {
158158
getConnectedDevices: () => [deviceMockData]
159159
});
160+
injector.register("previewAppFilesService", PreviewAppFilesService);
160161

161162
return injector;
162163
}
@@ -234,7 +235,7 @@ function mapFiles(files: string[]): FilePayload[] {
234235
return files.map(file => {
235236
return {
236237
event: "change",
237-
file: path.relative(path.join(platformsDirPath, "app"), path.join(platformsDirPath, "app", file)),
238+
file: path.join("..", "platforms", "app", file),
238239
fileContents: undefined,
239240
binary: false
240241
};
@@ -291,34 +292,6 @@ describe("previewAppLiveSyncService", () => {
291292
describe("syncFiles", () => {
292293
afterEach(() => reset());
293294

294-
const excludeFilesTestCases: ITestCase[] = [
295-
{
296-
name: ".ts files",
297-
appFiles: ["dir1/file.js", "file.ts"],
298-
expectedFiles: [`dir1/file.js`]
299-
},
300-
{
301-
name: ".sass files",
302-
appFiles: ["myDir1/mySubDir/myfile.css", "myDir1/mySubDir/myfile.sass"],
303-
expectedFiles: [`myDir1/mySubDir/myfile.css`]
304-
},
305-
{
306-
name: ".scss files",
307-
appFiles: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.scss", "my/file.js"],
308-
expectedFiles: [`myDir1/mySubDir/myfile1.css`, `my/file.js`]
309-
},
310-
{
311-
name: ".less files",
312-
appFiles: ["myDir1/mySubDir/myfile1.css", "myDir1/mySubDir/myfile.less", "my/file.js"],
313-
expectedFiles: [`myDir1/mySubDir/myfile1.css`, `my/file.js`]
314-
},
315-
{
316-
name: ".DS_Store file",
317-
appFiles: ["my/test/file.js", ".DS_Store"],
318-
expectedFiles: [`my/test/file.js`]
319-
}
320-
];
321-
322295
const nativeFilesTestCases: ITestCase[] = [
323296
{
324297
name: "Android manifest is changed",
@@ -380,10 +353,6 @@ describe("previewAppLiveSyncService", () => {
380353
];
381354

382355
const testCategories = [
383-
{
384-
name: "should exclude",
385-
testCases: excludeFilesTestCases
386-
},
387356
{
388357
name: "should show warning and not transfer native files when",
389358
testCases: nativeFilesTestCases.map(testCase => {

0 commit comments

Comments
 (0)