|
| 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 | +}); |
0 commit comments