|
1 | 1 | import { fallbackThemeUrl } from './utils'; |
2 | | -import { mergeConfig } from '../../../config'; |
3 | 2 |
|
4 | 3 | 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(); |
12 | 6 |
|
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 | + }, |
16 | 14 | }); |
17 | | - expect(fallbackThemeUrl('my.css')).toBe('//example.com/my.css'); |
18 | 15 | }); |
19 | 16 |
|
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 | + }); |
25 | 20 |
|
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); |
30 | 23 | }); |
31 | 24 |
|
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'); |
37 | 27 |
|
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'); |
42 | 29 | }); |
43 | 30 | }); |
0 commit comments