Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / utils / replaceLocalURL.test.ts
blobf17bdf400ebf23cfdee59c98d46c613a6b10c3b9
1 import { replaceLocalURL } from './replaceLocalURL';
3 const replaceLocation = (href: string) => {
4     window = Object.create(window);
6     const url = new URL(href);
8     Object.defineProperty(window, 'location', {
9         value: {
10             hostname: url.hostname,
11             host: url.host,
12         },
13         writable: true, // possibility to override
14     });
17 describe('replaceLocalURL', () => {
18     const realLocation = window.location;
20     afterEach(() => {
21         window.location = realLocation;
22     });
24     describe('localhost', () => {
25         beforeEach(() => {
26             replaceLocation('https://localhost:8080/u/0');
27         });
29         it('should not replace local URLs', () => {
30             const url = 'https://drive-api.proton.me/test';
32             expect(replaceLocalURL(url)).toBe(url);
33         });
34     });
36     describe('proton.me', () => {
37         beforeEach(() => {
38             replaceLocation('https://drive.proton.me/u/0');
39         });
41         it('should not replace local URLs', () => {
42             const url = 'https://drive-api.proton.me/test';
44             expect(replaceLocalURL(url)).toBe(url);
45         });
46     });
48     describe('proton.local', () => {
49         beforeEach(() => {
50             replaceLocation('https://drive.proton.local/u/0');
51         });
53         [
54             ['https://drive.proton.black/test', 'https://drive.proton.local/test'],
55             ['https://drive.env.proton.black/test', 'https://drive.proton.local/test'],
56             ['https://drive-api.proton.black/test', 'https://drive-api.proton.local/test'],
57             ['https://drive-api.env.proton.black/test', 'https://drive-api.proton.local/test'],
58         ].forEach((item) => {
59             const input = item[0];
60             const output = item[1];
62             it(`${input} => ${output}`, () => {
63                 expect(replaceLocalURL(input)).toBe(output);
64             });
65         });
66     });
68     describe('proton.local:8888', () => {
69         beforeEach(() => {
70             replaceLocation('https://drive.proton.local:8888/u/0');
71         });
73         [
74             ['https://drive.proton.black/test', 'https://drive.proton.local:8888/test'],
75             ['https://drive.env.proton.black/test', 'https://drive.proton.local:8888/test'],
76             ['https://drive-api.proton.black/test', 'https://drive-api.proton.local:8888/test'],
77             ['https://drive-api.env.proton.black/test', 'https://drive-api.proton.local:8888/test'],
78         ].forEach((item) => {
79             const input = item[0];
80             const output = item[1];
82             it(`${input} => ${output}`, () => {
83                 expect(replaceLocalURL(input)).toBe(output);
84             });
85         });
86     });
87 });