From ee74474811eb609072e1678bcb90db33756dcf38 Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Mon, 25 Nov 2019 02:43:19 +0800 Subject: [PATCH] fix(#300): correctly reference registry url from options --- jest/jestEnvironment.js | 2 +- jest/setup.ts | 5 ++--- src/utils/url.test.ts | 10 +++++++--- src/utils/url.ts | 3 +-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/jest/jestEnvironment.js b/jest/jestEnvironment.js index d293d5d..aeb87e8 100644 --- a/jest/jestEnvironment.js +++ b/jest/jestEnvironment.js @@ -1 +1 @@ -require.requireActual('babel/polyfill'); +jest.requireActual('babel/polyfill'); diff --git a/jest/setup.ts b/jest/setup.ts index 293e0ef..65b2ff7 100644 --- a/jest/setup.ts +++ b/jest/setup.ts @@ -7,14 +7,13 @@ import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import { GlobalWithFetchMock } from 'jest-fetch-mock'; -// @ts-ignore : Only a void function can be called with the 'new' keyword configure({ adapter: new Adapter() }); // @ts-ignore : Property '__APP_VERSION__' does not exist on type 'Global'. global.__APP_VERSION__ = '1.0.0'; // @ts-ignore : Property '__VERDACCIO_BASENAME_UI_OPTIONS' does not exist on type 'Global'. -global.__VERDACCIO_BASENAME_UI_OPTIONS = {}; - +global.__VERDACCIO_BASENAME_UI_OPTIONS = { base: 'http://localhost' }; +// @ts-ignore : Property 'VERDACCIO_API_URL' does not exist on type 'Global'. global.VERDACCIO_API_URL = 'https://verdaccio.tld'; const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock; diff --git a/src/utils/url.test.ts b/src/utils/url.test.ts index 34e42bd..b98fb82 100644 --- a/src/utils/url.test.ts +++ b/src/utils/url.test.ts @@ -17,14 +17,18 @@ describe('utils', () => { expect(isEmail('')).toBeFalsy(); }); - test('getRegistryURL() - should keep slash if location is a sub directory', () => { + test('getRegistryURL() - should not change when location change', () => { + expect(getRegistryURL()).toBe('http://localhost'); history.pushState({}, 'page title', '/-/web/detail'); - expect(getRegistryURL()).toBe('http://localhost/-/web/detail'); + expect(getRegistryURL()).toBe('http://localhost'); history.pushState({}, 'page title', '/'); }); - test('getRegistryURL() - should not add slash if location is not a sub directory', () => { + test('getRegistryURL() - should change when UI options change', () => { expect(getRegistryURL()).toBe('http://localhost'); + window.__VERDACCIO_BASENAME_UI_OPTIONS.base = 'http://localhost/test'; + expect(getRegistryURL()).toBe('http://localhost/test'); + window.__VERDACCIO_BASENAME_UI_OPTIONS.base = 'http://localhost'; }); }); diff --git a/src/utils/url.ts b/src/utils/url.ts index 41d2caf..021d034 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -15,8 +15,7 @@ export function isEmail(email: string): boolean { } export function getRegistryURL(): string { - // Don't add slash if it's not a sub directory - return `${location.origin}${location.pathname === '/' ? '' : location.pathname}`; + return window.__VERDACCIO_BASENAME_UI_OPTIONS.base; } export function extractFileName(url: string): string {