diff options
Diffstat (limited to 'test/helpers')
-rw-r--r-- | test/helpers/array-helpers.test.ts | 15 | ||||
-rw-r--r-- | test/helpers/url-helpers.test.ts | 137 |
2 files changed, 152 insertions, 0 deletions
diff --git a/test/helpers/array-helpers.test.ts b/test/helpers/array-helpers.test.ts new file mode 100644 index 000000000..e8060c2c4 --- /dev/null +++ b/test/helpers/array-helpers.test.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | import { shuffleArray } from '../../src/helpers/array-helpers'; | ||
2 | |||
3 | describe('array_helpers', () => { | ||
4 | it('isValidExternalURL', () => { | ||
5 | const originalArray = ['a', 'b', 'c']; | ||
6 | const shuffledArray = shuffleArray(originalArray); | ||
7 | |||
8 | // Expect the arrays to not be exactly the same | ||
9 | expect(shuffledArray).not.toEqual(originalArray); | ||
10 | |||
11 | // Expect the arrays to be exactly the same | ||
12 | // when both are sorted alphabetically | ||
13 | expect(shuffledArray.sort()).toEqual(originalArray.sort()); | ||
14 | }); | ||
15 | }); | ||
diff --git a/test/helpers/url-helpers.test.ts b/test/helpers/url-helpers.test.ts new file mode 100644 index 000000000..9ef06b905 --- /dev/null +++ b/test/helpers/url-helpers.test.ts | |||
@@ -0,0 +1,137 @@ | |||
1 | import * as url_helpers from '../../src/helpers/url-helpers'; | ||
2 | |||
3 | describe('url_helpers', () => { | ||
4 | describe('isValidExternalURL', () => { | ||
5 | describe('with string', () => { | ||
6 | it('returns false for empty string', () => { | ||
7 | const result = url_helpers.isValidExternalURL(''); | ||
8 | expect(result).toBe(false); | ||
9 | }); | ||
10 | |||
11 | it('returns false for whitespace string', () => { | ||
12 | const result = url_helpers.isValidExternalURL(' '); | ||
13 | expect(result).toBe(false); | ||
14 | }); | ||
15 | |||
16 | it('returns false for random string', () => { | ||
17 | const result = url_helpers.isValidExternalURL('some random string'); | ||
18 | expect(result).toBe(false); | ||
19 | }); | ||
20 | |||
21 | it('returns false for invalid url', () => { | ||
22 | const result = url_helpers.isValidExternalURL('shttps://google'); | ||
23 | expect(result).toBe(false); | ||
24 | }); | ||
25 | |||
26 | it('returns true for valid http url', () => { | ||
27 | const result = url_helpers.isValidExternalURL('http://google'); | ||
28 | expect(result).toBe(true); | ||
29 | }); | ||
30 | |||
31 | it('returns true for valid https url', () => { | ||
32 | const result = url_helpers.isValidExternalURL('https://google'); | ||
33 | expect(result).toBe(true); | ||
34 | }); | ||
35 | }); | ||
36 | |||
37 | describe('with URL', () => { | ||
38 | // Note: not testing the invalid string urls - since the URL ctor itself will raise an error | ||
39 | |||
40 | it('returns false for invalid url', () => { | ||
41 | const result = url_helpers.isValidExternalURL( | ||
42 | new URL('shttps://google'), | ||
43 | ); | ||
44 | expect(result).toBe(false); | ||
45 | }); | ||
46 | |||
47 | it('returns true for valid http url', () => { | ||
48 | const result = url_helpers.isValidExternalURL(new URL('http://google')); | ||
49 | expect(result).toBe(true); | ||
50 | }); | ||
51 | |||
52 | it('returns true for valid https url', () => { | ||
53 | const result = url_helpers.isValidExternalURL( | ||
54 | new URL('https://google'), | ||
55 | ); | ||
56 | expect(result).toBe(true); | ||
57 | }); | ||
58 | }); | ||
59 | }); | ||
60 | |||
61 | describe('fixUrl', () => { | ||
62 | it('handles with empty string', () => { | ||
63 | const result = url_helpers.fixUrl(''); | ||
64 | expect(result).toEqual(''); | ||
65 | }); | ||
66 | |||
67 | it('handles with whitespace string', () => { | ||
68 | const result = url_helpers.fixUrl(' '); | ||
69 | expect(result).toEqual(' '); | ||
70 | }); | ||
71 | |||
72 | it('handles with random string', () => { | ||
73 | const result = url_helpers.fixUrl('some random string'); | ||
74 | expect(result).toEqual('some random string'); | ||
75 | }); | ||
76 | |||
77 | it('handles string starting with http://', () => { | ||
78 | expect(url_helpers.fixUrl('http://some/random/url')).toEqual( | ||
79 | 'http://some/random/url', | ||
80 | ); | ||
81 | expect(url_helpers.fixUrl('http://some//random//url')).toEqual( | ||
82 | 'http://some/random/url', | ||
83 | ); | ||
84 | |||
85 | const gmailEmbeddedUrl = | ||
86 | 'https://www.google.com/url?q=https://github.com/ferdium/ferdium-app/issues/87&source=gmail'; | ||
87 | expect(url_helpers.fixUrl(gmailEmbeddedUrl)).toEqual(gmailEmbeddedUrl); // it should NOT remove the double-slash from the embedded url in the query string | ||
88 | }); | ||
89 | |||
90 | it('handles string starting with https://', () => { | ||
91 | expect(url_helpers.fixUrl('https://some/random/url')).toEqual( | ||
92 | 'https://some/random/url', | ||
93 | ); | ||
94 | expect(url_helpers.fixUrl('https://some//random//url')).toEqual( | ||
95 | 'https://some/random/url', | ||
96 | ); | ||
97 | }); | ||
98 | |||
99 | it('handles string starting with file://', () => { | ||
100 | expect(url_helpers.fixUrl('file://some/random/url')).toEqual( | ||
101 | 'file://some/random/url', | ||
102 | ); | ||
103 | expect(url_helpers.fixUrl('file://some//random//url')).toEqual( | ||
104 | 'file://some/random/url', | ||
105 | ); | ||
106 | }); | ||
107 | }); | ||
108 | |||
109 | describe('isValidFileUrl', () => { | ||
110 | it('returns false for empty string', () => { | ||
111 | const result = url_helpers.isValidFileUrl(''); | ||
112 | expect(result).toBe(false); | ||
113 | }); | ||
114 | |||
115 | it('returns false for whitespace string', () => { | ||
116 | const result = url_helpers.isValidFileUrl(' '); | ||
117 | expect(result).toBe(false); | ||
118 | }); | ||
119 | |||
120 | it('returns false for random string', () => { | ||
121 | const result = url_helpers.isValidFileUrl('some random string'); | ||
122 | expect(result).toBe(false); | ||
123 | }); | ||
124 | |||
125 | it('returns false for invalid url', () => { | ||
126 | const result = url_helpers.isValidFileUrl('sfile://google'); | ||
127 | expect(result).toBe(false); | ||
128 | }); | ||
129 | |||
130 | it('returns true for valid file url', () => { | ||
131 | const fileName = | ||
132 | process.platform === 'win32' ? 'file:///c:\\' : 'file:///'; | ||
133 | const result = url_helpers.isValidFileUrl(fileName); | ||
134 | expect(result).toBe(true); | ||
135 | }); | ||
136 | }); | ||
137 | }); | ||