Skip to content

Commit 9f92281

Browse files
fix: always use window.location?.origin for Paragon fallbackThemeUrl (#806)
1 parent 6710571 commit 9f92281

File tree

5 files changed

+73
-34
lines changed

5 files changed

+73
-34
lines changed

src/react/hooks/paragon/useParagonThemeCore.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jest.mock('../../../logging');
1010
describe('useParagonThemeCore', () => {
1111
const themeOnComplete = jest.fn();
1212
let coreConfig;
13+
const originalWindowLocation = window.location;
14+
const mockWindowLocationOrigin = jest.fn();
1315

1416
beforeEach(() => {
1517
document.head.innerHTML = '';
@@ -21,9 +23,25 @@ describe('useParagonThemeCore', () => {
2123
},
2224
onComplete: themeOnComplete,
2325
};
26+
27+
Object.defineProperty(window, 'location', {
28+
value: {
29+
get origin() {
30+
return mockWindowLocationOrigin();
31+
},
32+
},
33+
});
34+
mockWindowLocationOrigin.mockReturnValue(getConfig().BASE_URL);
35+
});
36+
37+
afterEach(() => {
2438
jest.clearAllMocks();
2539
});
2640

41+
afterAll(() => {
42+
Object.defineProperty(window, 'location', originalWindowLocation);
43+
});
44+
2745
it('should load the core url and change the loading state to true', () => {
2846
renderHook(() => useParagonThemeCore(coreConfig));
2947
const createdLinkTag = document.head.querySelector('link');

src/react/hooks/paragon/useParagonThemeUrls.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { renderHook } from '@testing-library/react';
22

33
import useParagonThemeUrls from './useParagonThemeUrls';
4-
import { mergeConfig } from '../../../config';
4+
import { mergeConfig, getConfig } from '../../../config';
55

66
Object.defineProperty(global, 'PARAGON_THEME', {
77
value: {
@@ -41,8 +41,26 @@ Object.defineProperty(global, 'PARAGON_THEME', {
4141
writable: true,
4242
});
4343

44+
const originalWindowLocation = window.location;
45+
const mockWindowLocationOrigin = jest.fn();
46+
Object.defineProperty(window, 'location', {
47+
value: {
48+
get origin() {
49+
return mockWindowLocationOrigin();
50+
},
51+
},
52+
});
53+
4454
describe('useParagonThemeUrls', () => {
45-
beforeEach(() => { jest.resetAllMocks(); });
55+
beforeEach(() => {
56+
mockWindowLocationOrigin.mockReturnValue(getConfig().BASE_URL);
57+
});
58+
afterEach(() => {
59+
jest.resetAllMocks();
60+
});
61+
afterAll(() => {
62+
Object.defineProperty(window, 'location', originalWindowLocation);
63+
});
4664
it.each([
4765
[undefined, undefined],
4866
[{}, {

src/react/hooks/paragon/useParagonThemeVariants.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,29 @@ jest.mock('../../../logging');
99

1010
describe('useParagonThemeVariants', () => {
1111
const themeOnComplete = jest.fn();
12+
const originalWindowLocation = window.location;
13+
const mockWindowLocationOrigin = jest.fn();
14+
15+
beforeEach(() => {
16+
Object.defineProperty(window, 'location', {
17+
value: {
18+
get origin() {
19+
return mockWindowLocationOrigin();
20+
},
21+
},
22+
});
23+
mockWindowLocationOrigin.mockReturnValue(getConfig().BASE_URL);
24+
});
1225

1326
afterEach(() => {
1427
document.head.innerHTML = '';
1528
jest.clearAllMocks();
1629
});
1730

31+
afterAll(() => {
32+
Object.defineProperty(window, 'location', originalWindowLocation);
33+
});
34+
1835
it('should create the links tags for each theme variant and change the state to true when all variants are loaded', () => {
1936
const themeVariants = {
2037
light: {

src/react/hooks/paragon/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getConfig } from '../../../config';
21
import { basename } from '../../../initialize';
32

43
/**
@@ -17,7 +16,7 @@ export const removeExistingLinks = (existingLinks) => {
1716
* @returns {string} The default theme url.
1817
*/
1918
export const fallbackThemeUrl = (url) => {
20-
const baseUrl = getConfig().BASE_URL || window.location?.origin;
19+
const baseUrl = window.location?.origin;
2120

2221
// validates if the baseurl has the protocol to be interpreted correctly by the browser,
2322
// if is not present add '//' to use Protocol-relative URL

src/react/hooks/paragon/utils.test.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
import { fallbackThemeUrl } from './utils';
2-
import { mergeConfig } from '../../../config';
32

43
describe('fallbackThemeUrl', () => {
5-
it('if should return a relative url if the BASE_URL does not provide the protocol', () => {
6-
mergeConfig({
7-
BASE_URL: 'example.com',
8-
});
9-
10-
expect(fallbackThemeUrl('my.css')).toBe('//example.com/my.css');
11-
});
4+
const originalWindowLocation = window.location;
5+
const mockWindowLocationOrigin = jest.fn();
126

13-
it('if should return a relative url if the BASE_URL is a relative url', () => {
14-
mergeConfig({
15-
BASE_URL: '//example.com',
7+
beforeEach(() => {
8+
Object.defineProperty(window, 'location', {
9+
value: {
10+
get origin() {
11+
return mockWindowLocationOrigin();
12+
},
13+
},
1614
});
17-
expect(fallbackThemeUrl('my.css')).toBe('//example.com/my.css');
1815
});
1916

20-
it('if should return a full url if the BASE_URL provides the protocol', () => {
21-
mergeConfig({
22-
BASE_URL: 'http://example.com',
23-
});
24-
expect(fallbackThemeUrl('my.css')).toBe('http://example.com/my.css');
17+
afterEach(() => {
18+
jest.clearAllMocks();
19+
});
2520

26-
mergeConfig({
27-
BASE_URL: 'https://example.com',
28-
});
29-
expect(fallbackThemeUrl('my.css')).toBe('https://example.com/my.css');
21+
afterAll(() => {
22+
Object.defineProperty(window, 'location', originalWindowLocation);
3023
});
3124

32-
it('if should return a full url base on the window location if BASE_URL is not defined', () => {
33-
mergeConfig({
34-
BASE_URL: 'http://example.com',
35-
});
36-
expect(fallbackThemeUrl('my.css')).toBe('http://example.com/my.css');
25+
it('should return a full url based on the window location', () => {
26+
mockWindowLocationOrigin.mockReturnValue('http://example.com');
3727

38-
mergeConfig({
39-
BASE_URL: '',
40-
});
41-
expect(fallbackThemeUrl('my.css')).toBe('http://localhost/my.css');
28+
expect(fallbackThemeUrl('my.css')).toBe('http://example.com/my.css');
4229
});
4330
});

0 commit comments

Comments
 (0)