Skip to content

Commit

Permalink
feat: add getPath function for PUBLIC_PATH
Browse files Browse the repository at this point in the history
This add function to get path from a string url from utils.
Also the function is implemented to the history module. In order to match with URLS as webpack allow.(CDN).
Test were developed for different shaped of string urls with path.
A basic test is also added for the base case in the history variable for the initialize
with base case of PUBLIC_PATH=`/`.
  • Loading branch information
johanseto authored and andrey-canon committed Jan 18, 2024
1 parent c1936ea commit 2501c53
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
ensureDefinedConfig,
mix,
parseURL,
getPath,
} from './utils';
export {
APP_TOPIC,
Expand Down
4 changes: 2 additions & 2 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Note that the env.config.js file in frontend-platform's root directory is NOT us
initialization code, it's just there for the test suite and example application.
*/
import envConfig from 'env.config'; // eslint-disable-line import/no-unresolved

import { getPath } from './utils';

Check failure on line 57 in src/initialize.js

View workflow job for this annotation

GitHub Actions / tests

'/home/runner/work/frontend-platform/frontend-platform/src/utils.js' imported multiple times
import {
publish,
} from './pubSub';
Expand Down Expand Up @@ -101,7 +101,7 @@ import { mix } from './utils';
*/
export const history = (typeof window !== 'undefined')
? createBrowserHistory({
basename: getConfig().PUBLIC_PATH,
basename: getPath(getConfig().PUBLIC_PATH),
}) : createMemoryHistory();

/**
Expand Down
11 changes: 11 additions & 0 deletions src/initialize.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PubSub from 'pubsub-js';
import { createBrowserHistory } from 'history';
import {
APP_PUBSUB_INITIALIZED,
APP_CONFIG_INITIALIZED,
Expand Down Expand Up @@ -37,6 +38,7 @@ jest.mock('./auth');
jest.mock('./analytics');
jest.mock('./i18n');
jest.mock('./auth/LocalForageCache');
jest.mock('history');

let config = null;
const newConfig = {
Expand Down Expand Up @@ -436,3 +438,12 @@ describe('initialize', () => {
expect(logError).not.toHaveBeenCalled();
});
});

describe('history', () => {
it('browser history called by default path', async () => {
// import history from initialize;
expect(createBrowserHistory).toHaveBeenCalledWith({
basename: '/',
});
});
});
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ export function parseURL(url) {
return parser;
}

/**
* Given a string URL return the path of the URL
*
*
* @param {string}
* @returns {string}
*/
export function getPath(url) {
return parseURL(url).pathname;
}

/**
* *Deprecated*: A method which converts the supplied query string into an object of
* key-value pairs and returns it. Defaults to the current query string - should perform like
Expand Down
39 changes: 39 additions & 0 deletions src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
snakeCaseObject,
convertKeyNames,
parseURL,
getPath,
getQueryParameters,
mix,
} from '.';
Expand Down Expand Up @@ -172,3 +173,41 @@ describe('ParseURL', () => {
expect(parsedURL.host).toEqual('example.com:3000');
});
});

describe('getPath', () => {
it('Path is retrieved with full url', () => {
const testURL = 'http://example.com:3000/pathname/?search=test#hash';

expect(getPath(testURL)).toEqual('/pathname/');
});

it('Path is retrieved with only path', () => {
const testURL = '/learning/';

expect(getPath(testURL)).toEqual('/learning/');
});

it('Path is retrieved without protocol', () => {
const testURL = '//example.com:3000/accounts/';

expect(getPath(testURL)).toEqual('/accounts/');
});

it('Path is retrieved with base `/`', () => {
const testURL = '/';

expect(getPath(testURL)).toEqual('/');
});

it('Path is retrieved without port', () => {
const testURL = 'https://example.com/accounts/';

expect(getPath(testURL)).toEqual('/accounts/');
});

it('Path is retrieved without CDN shape', () => {
const testURL = 'https://d20blt6w1kfasr.cloudfront.net/learning/';

expect(getPath(testURL)).toEqual('/learning/');
});
});

0 comments on commit 2501c53

Please sign in to comment.