From 6b322ad553e9fb3ee65b2968dcfe856ba42a0bfb Mon Sep 17 00:00:00 2001 From: Daniel Ruf <827205+DanielRuf@users.noreply.github.com> Date: Thu, 9 Jan 2020 06:12:50 +0100 Subject: [PATCH] fix: generate correct registry URL (#413) * Revert "Revert "fix(#300): correctly reference registry url from options" (#311)" This reverts commit d955268c25e58db11622bb26dd8278a93ba08310. * fix: generate full URL from path --- jest/jestEnvironment.js | 2 +- jest/setup.ts | 5 ++--- src/utils/url.test.ts | 10 +++++++--- src/utils/url.ts | 3 +-- tools/webpack.dev.config.babel.js | 2 +- 5 files changed, 12 insertions(+), 10 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 ce94df9..090b579 100644 --- a/jest/setup.ts +++ b/jest/setup.ts @@ -8,14 +8,13 @@ import Adapter from 'enzyme-adapter-react-16'; import { GlobalWithFetchMock } from 'jest-fetch-mock'; import 'mutationobserver-shim'; -// @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 { diff --git a/tools/webpack.dev.config.babel.js b/tools/webpack.dev.config.babel.js index 6303041..726bb84 100644 --- a/tools/webpack.dev.config.babel.js +++ b/tools/webpack.dev.config.babel.js @@ -37,7 +37,7 @@ export default { }), new HTMLWebpackPlugin({ __UI_OPTIONS: JSON.stringify({ - base: '/', + base: new URL('/', 'https://localhost:4872'), }), title: 'Verdaccio Dev UI', scope: '',