|
| 1 | +import { Yok } from "../lib/common/yok"; |
| 2 | +import * as stubs from "./stubs"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import { NodePackageManager } from "../lib/node-package-manager"; |
| 5 | + |
| 6 | +function createTestInjector(configuration: { |
| 7 | +} = {}): IInjector { |
| 8 | + const injector = new Yok(); |
| 9 | + injector.register("hostInfo", {}); |
| 10 | + injector.register("errors", stubs.ErrorsStub); |
| 11 | + injector.register("logger", stubs.LoggerStub); |
| 12 | + injector.register("childProcess", stubs.ChildProcessStub); |
| 13 | + injector.register("httpClient", {}); |
| 14 | + injector.register("fs", stubs.FileSystemStub); |
| 15 | + injector.register("npm", NodePackageManager); |
| 16 | + |
| 17 | + return injector; |
| 18 | +} |
| 19 | + |
| 20 | +describe("node-package-manager", () => { |
| 21 | + |
| 22 | + describe("getPackageNameParts", () => { |
| 23 | + [ |
| 24 | + { |
| 25 | + name: "should return both name and version when valid fullName passed", |
| 26 | + templateFullName: "[email protected]", |
| 27 | + expectedVersion: "1.0.0", |
| 28 | + expectedName: "some-template", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "should return both name and version when valid fullName with scope passed", |
| 32 | + templateFullName: "@nativescript/[email protected]", |
| 33 | + expectedVersion: "1.0.0", |
| 34 | + expectedName: "@nativescript/some-template", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "should return only name when version is not specified and the template is scoped", |
| 38 | + templateFullName: "@nativescript/some-template", |
| 39 | + expectedVersion: "", |
| 40 | + expectedName: "@nativescript/some-template", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "should return only name when version is not specified", |
| 44 | + templateFullName: "some-template", |
| 45 | + expectedVersion: "", |
| 46 | + expectedName: "some-template", |
| 47 | + } |
| 48 | + ].forEach(testCase => { |
| 49 | + it(testCase.name, async () => { |
| 50 | + const testInjector = createTestInjector(); |
| 51 | + const npm = testInjector.resolve<NodePackageManager>("npm"); |
| 52 | + const templateNameParts = await npm.getPackageNameParts(testCase.templateFullName); |
| 53 | + assert.strictEqual(templateNameParts.name, testCase.expectedName); |
| 54 | + assert.strictEqual(templateNameParts.version, testCase.expectedVersion); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("getPackageFullName", () => { |
| 60 | + [ |
| 61 | + { |
| 62 | + name: "should return name and version when specified", |
| 63 | + templateName: "some-template", |
| 64 | + templateVersion: "1.0.0", |
| 65 | + expectedFullName: "[email protected]", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "should return only the github url when no version specified", |
| 69 | + templateName: "https://github.com/NativeScript/template-drawer-navigation-ng#master", |
| 70 | + templateVersion: "", |
| 71 | + expectedFullName: "https://github.com/NativeScript/template-drawer-navigation-ng#master", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "should return only the name when no version specified", |
| 75 | + templateName: "some-template", |
| 76 | + templateVersion: "", |
| 77 | + expectedFullName: "some-template", |
| 78 | + } |
| 79 | + ].forEach(testCase => { |
| 80 | + it(testCase.name, async () => { |
| 81 | + const testInjector = createTestInjector(); |
| 82 | + const npm = testInjector.resolve<NodePackageManager>("npm"); |
| 83 | + const templateFullName = await npm.getPackageFullName({ name: testCase.templateName, version: testCase.templateVersion }); |
| 84 | + assert.strictEqual(templateFullName, testCase.expectedFullName); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments