From 39651bb6b358f2045f11abb2363e5d108341a511 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Wed, 4 Jul 2018 00:12:28 +0200 Subject: [PATCH] chore: add unit test for package access normalize --- test/unit/api/config-utils.spec.js | 83 ++++++++++++++++++- .../partials/config/yaml/pkgs-custom.yaml | 17 ++++ .../yaml/pkgs-nosuper-wildcard-custom.yaml | 13 +++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 test/unit/partials/config/yaml/pkgs-custom.yaml create mode 100644 test/unit/partials/config/yaml/pkgs-nosuper-wildcard-custom.yaml diff --git a/test/unit/api/config-utils.spec.js b/test/unit/api/config-utils.spec.js index 6382d8483..a76879d8f 100644 --- a/test/unit/api/config-utils.spec.js +++ b/test/unit/api/config-utils.spec.js @@ -1,8 +1,9 @@ // @flow + import path from 'path'; import {spliceURL} from '../../../src/utils/string'; import {parseConfigFile} from '../../../src/lib/utils'; -import {normalisePackageAccess} from '../../../src/lib/config-utils'; +import {getMatchedPackagesSpec, hasProxyTo, normalisePackageAccess} from '../../../src/lib/config-utils'; import {PACKAGE_ACCESS, ROLES} from '../../../src/lib/constants'; describe('Config Utilities', () => { @@ -86,17 +87,95 @@ describe('Config Utilities', () => { const {packages} = parseConfigFile(parsePartial('pkgs-empty')); const access = normalisePackageAccess(packages); expect(access).toBeDefined(); + const scoped = access[`${PACKAGE_ACCESS.SCOPE}`]; expect(scoped).toBeUndefined(); - const all = access[`${PACKAGE_ACCESS.ALL}`]; + // ** should be added by default + const all = access[`${PACKAGE_ACCESS.ALL}`]; expect(all).toBeDefined(); + expect(all.access).toBeUndefined(); expect(all.publish).toBeUndefined(); }); }); + describe('getMatchedPackagesSpec', () => { + test('should test basic config', () => { + const {packages} = parseConfigFile(parsePartial('pkgs-custom')); + // $FlowFixMe + expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook'); + // $FlowFixMe + expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google'); + // $FlowFixMe + expect(getMatchedPackagesSpec('vue', packages).proxy).toMatch('npmjs'); + // $FlowFixMe + expect(getMatchedPackagesSpec('@scope/vue', packages).proxy).toMatch('npmjs'); + }); + + test('should test no ** wildcard on config', () => { + const {packages} = parseConfigFile(parsePartial('pkgs-nosuper-wildcard-custom')); + // $FlowFixMe + expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook'); + // $FlowFixMe + expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google'); + // $FlowFixMe + expect(getMatchedPackagesSpec('@fake/angular', packages).proxy).toMatch('npmjs'); + expect(getMatchedPackagesSpec('vue', packages)).toBeUndefined(); + expect(getMatchedPackagesSpec('@scope/vue', packages)).toBeUndefined(); + }); + }); + + describe('hasProxyTo', () => { + test('should test basic config', () => { + const packages = normalisePackageAccess(parseConfigFile(parsePartial('pkgs-basic')).packages); + // react + expect(hasProxyTo('react', 'facebook', packages)).toBeFalsy(); + expect(hasProxyTo('react', 'google', packages)).toBeFalsy(); + // vue + expect(hasProxyTo('vue', 'google', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy(); + // angular + expect(hasProxyTo('angular', 'google', packages)).toBeFalsy(); + expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy(); + expect(hasProxyTo('angular', 'npmjs', packages)).toBeTruthy(); + }); + + test('should test resolve based on custom package access', () => { + const packages = normalisePackageAccess(parseConfigFile(parsePartial('pkgs-custom')).packages); + // react + expect(hasProxyTo('react', 'facebook', packages)).toBeTruthy(); + expect(hasProxyTo('react', 'google', packages)).toBeFalsy(); + // vue + expect(hasProxyTo('vue', 'google', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy(); + // angular + expect(hasProxyTo('angular', 'google', packages)).toBeTruthy(); + expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy(); + expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); + }); + + test('should not resolve any proxy', () => { + const packages = normalisePackageAccess(parseConfigFile(parsePartial('pkgs-empty')).packages); + // react + expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy(); + expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy(); + // vue + expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); + expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); + // angular + expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); + expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); + expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); + // private + expect(hasProxyTo('private', 'fake', packages)).toBeFalsy(); + }); + }); + describe('spliceURL', () => { test('should splice two strings and generate a url', () => { const url: string = spliceURL('http://domain.com', '/-/static/logo.png'); diff --git a/test/unit/partials/config/yaml/pkgs-custom.yaml b/test/unit/partials/config/yaml/pkgs-custom.yaml new file mode 100644 index 000000000..4ee938acf --- /dev/null +++ b/test/unit/partials/config/yaml/pkgs-custom.yaml @@ -0,0 +1,17 @@ +packages: + 'react': + access: admin + publish: admin + proxy: facebook + 'angular': + access: admin + publish: admin + proxy: google + '@*/*': + access: $all + publish: $authenticated + proxy: npmjs + '**': + access: $all + publish: $authenticated + proxy: npmjs diff --git a/test/unit/partials/config/yaml/pkgs-nosuper-wildcard-custom.yaml b/test/unit/partials/config/yaml/pkgs-nosuper-wildcard-custom.yaml new file mode 100644 index 000000000..d86a53f56 --- /dev/null +++ b/test/unit/partials/config/yaml/pkgs-nosuper-wildcard-custom.yaml @@ -0,0 +1,13 @@ +packages: + 'react': + access: admin + publish: admin + proxy: facebook + 'angular': + access: admin + publish: admin + proxy: google + '@fake/*': + access: $all + publish: $authenticated + proxy: npmjs