GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — nativescript-cli/test/project-templates-service.ts at main · NativeScript/nativescript-cli Expand file tree/ Copy pathproject-templates-service.tsMore file actions 306 lines (287 loc) · 10.1 KB / Copy pathproject-templates-service.tsFile metadata and controls306 lines (287 loc) · 10.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 import { Yok } from "../lib/common/yok"; import * as stubs from "./stubs"; import { ProjectTemplatesService } from "../lib/services/project-templates-service"; import { assert } from "chai"; import * as path from "path"; import * as constants from "../lib/constants"; import { INpmInstallResultInfo, INodePackageManagerInstallOptions, INpmPackageNameParts, INpmInstallOptions, } from "../lib/declarations"; import { IProjectTemplatesService } from "../lib/definitions/project"; import { IInjector } from "../lib/common/definitions/yok"; import { IAnalyticsService, IFileSystem } from "../lib/common/declarations"; import { IEventActionData } from "../lib/common/definitions/google-analytics"; const nativeScriptValidatedTemplatePath = "nsValidatedTemplatePath"; function createTestInjector( configuration: { shouldNpmInstallThrow?: boolean; packageJsonContent?: any; packageVersion?: string; packageName?: string; } = {} ): IInjector { const injector = new Yok(); injector.register("errors", stubs.ErrorsStub); injector.register("logger", stubs.LoggerStub); injector.register("fs", { exists: (pathToCheck: string) => false, readJson: (pathToFile: string) => configuration.packageJsonContent || {}, }); injector.register("staticConfig", { version: "8.0.0" }); class NpmStub extends stubs.NodePackageManagerStub { public async install( packageName: string, pathToSave: string, config: INodePackageManagerInstallOptions ): Promise<INpmInstallResultInfo> { if (configuration.shouldNpmInstallThrow) { throw new Error("NPM install throws error."); } return { name: "Some Result", version: "1" }; } async getPackageNameParts( fullPackageName: string ): Promise<INpmPackageNameParts> { return { name: configuration.packageName || fullPackageName, version: configuration.packageVersion || "", }; } } injector.register("packageManager", NpmStub); class NpmInstallationManagerStub extends stubs.PackageInstallationManagerStub { async install( packageName: string, pathToSave?: string, options?: INpmInstallOptions ): Promise<string> { if (configuration.shouldNpmInstallThrow) { throw new Error("NPM install throws error."); } return Promise.resolve(nativeScriptValidatedTemplatePath); } } injector.register("packageInstallationManager", NpmInstallationManagerStub); injector.register("projectTemplatesService", ProjectTemplatesService); injector.register("analyticsService", { track: async (): Promise<any[]> => undefined, trackEventActionInGoogleAnalytics: (data: IEventActionData) => Promise.resolve(), }); injector.register("pacoteService", { manifest: (packageName: string) => { const packageJsonContent = configuration.packageJsonContent || {}; packageJsonContent.name = packageName; return Promise.resolve(packageJsonContent); }, }); return injector; } describe("project-templates-service", () => { describe("prepareTemplate", () => { // describe("returns correct path to template", () => { // // it("when reserved template name is used", async () => { // // const testInjector = createTestInjector(); // // const projectTemplatesService = testInjector.resolve< // // IProjectTemplatesService // // >("projectTemplatesService"); // // const { templatePath } = await projectTemplatesService.prepareTemplate( // // "typescript", // // "tempFolder" // // ); // // assert.strictEqual( // // path.basename(templatePath), // // nativeScriptValidatedTemplatePath // // ); // // assert.strictEqual( // // isDeleteDirectoryCalledForNodeModulesDir, // // true, // // "When correct path is returned, template's node_modules directory should be deleted." // // ); // // }); // // // // it("when reserved template name is used (case-insensitive test)", async () => { // // const testInjector = createTestInjector(); // // const projectTemplatesService = testInjector.resolve< // // IProjectTemplatesService // // >("projectTemplatesService"); // // const { templatePath } = await projectTemplatesService.prepareTemplate( // // "tYpEsCriPT", // // "tempFolder" // // ); // // assert.strictEqual( // // path.basename(templatePath), // // nativeScriptValidatedTemplatePath // // ); // // assert.strictEqual( // // isDeleteDirectoryCalledForNodeModulesDir, // // true, // // "When correct path is returned, template's node_modules directory should be deleted." // // ); // // }); // // // }); it("uses defaultTemplate when undefined is passed as parameter", async () => { const testInjector = createTestInjector(); const projectTemplatesService = testInjector.resolve< IProjectTemplatesService >("projectTemplatesService"); const { templateName } = await projectTemplatesService.prepareTemplate( undefined, //constants.RESERVED_TEMPLATE_NAMES["default"], "tempFolder" ); assert.strictEqual( templateName, constants.RESERVED_TEMPLATE_NAMES["default"] ); // assert.strictEqual( // isDeleteDirectoryCalledForNodeModulesDir, // true, // "When correct path is returned, template's node_modules directory should be deleted." // ); }); describe("sends correct information to Google Analytics", () => { let analyticsService: IAnalyticsService; let dataSentToGoogleAnalytics: IEventActionData[] = []; let testInjector: IInjector; let projectTemplatesService: IProjectTemplatesService; beforeEach(() => { testInjector = createTestInjector({ shouldNpmInstallThrow: false }); analyticsService = testInjector.resolve<IAnalyticsService>( "analyticsService" ); const fs = testInjector.resolve<IFileSystem>("fs"); fs.exists = (filePath: string) => false; dataSentToGoogleAnalytics = []; analyticsService.trackEventActionInGoogleAnalytics = async ( data: IEventActionData ): Promise<void> => { dataSentToGoogleAnalytics.push(data); }; projectTemplatesService = testInjector.resolve( "projectTemplatesService" ); }); it("sends template name when the template is used from npm", async () => { const templateName = "template-from-npm"; await projectTemplatesService.prepareTemplate( templateName, "tempFolder" ); assert.deepStrictEqual(dataSentToGoogleAnalytics, [ { action: constants.TrackActionNames.CreateProject, isForDevice: null, additionalData: templateName, }, { action: constants.TrackActionNames.UsingTemplate, additionalData: templateName, }, ]); }); it("sends template name (from template's package.json) when the template is used from local path", async () => { const templateName = "my-own-local-template"; const localTemplatePath = "/Users/username/localtemplate"; const fs = testInjector.resolve<IFileSystem>("fs"); fs.exists = (filePath: string): boolean => true; const pacoteService = testInjector.resolve<IPacoteService>( "pacoteService" ); pacoteService.manifest = () => Promise.resolve({ name: templateName }); await projectTemplatesService.prepareTemplate( localTemplatePath, "tempFolder" ); assert.deepStrictEqual(dataSentToGoogleAnalytics, [ { action: constants.TrackActionNames.CreateProject, isForDevice: null, additionalData: `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateName}`, }, { action: constants.TrackActionNames.UsingTemplate, additionalData: `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateName}`, }, ]); }); it("sends the template name (path to dirname) when the template is used from local path but there's no package.json at the root", async () => { const templateName = "localtemplate"; const localTemplatePath = `/Users/username/${templateName}`; const fs = testInjector.resolve<IFileSystem>("fs"); fs.exists = (localPath: string): boolean => path.basename(localPath) !== constants.PACKAGE_JSON_FILE_NAME; const pacoteService = testInjector.resolve<IPacoteService>( "pacoteService" ); pacoteService.manifest = () => Promise.resolve({}); await projectTemplatesService.prepareTemplate( localTemplatePath, "tempFolder" ); assert.deepStrictEqual(dataSentToGoogleAnalytics, [ { action: constants.TrackActionNames.CreateProject, isForDevice: null, additionalData: `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateName}`, }, { action: constants.TrackActionNames.UsingTemplate, additionalData: `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateName}`, }, ]); }); }); describe("uses correct version", () => { [ { name: "is correct when package version is passed", expectedVersion: "1.0.0", expectedTemplateName: "some-template", }, { name: "is correct when reserved package name with version is passed", expectedVersion: "1.0.0", expectedTemplateName: "@nativescript/template-hello-world-ts", }, { name: "is correct when scoped package name without version is passed", templateName: "@nativescript/vue-template", expectedVersion: "^8.0.0", expectedTemplateName: "@nativescript/vue-template", }, { name: "is correct when scoped package name with version is passed", expectedVersion: "1.0.0", expectedTemplateName: "@nativescript/vue-template", }, ].forEach((testCase) => { it(testCase.name, async () => { const testInjector = createTestInjector({ packageVersion: testCase.expectedVersion, packageName: testCase.expectedTemplateName, }); const projectTemplatesService = testInjector.resolve< IProjectTemplatesService >("projectTemplatesService"); const { version, templateName, } = await projectTemplatesService.prepareTemplate( testCase.templateName, "tempFolder" ); assert.strictEqual(version, testCase.expectedVersion); assert.strictEqual(templateName, testCase.expectedTemplateName); }); }); }); }); }); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |