aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers
diff options
context:
space:
mode:
authorLibravatar Aditya Mangalampalli <aditya.mangalampalli@gmail.com>2022-05-12 16:34:53 -0700
committerLibravatar GitHub <noreply@github.com>2022-05-12 23:34:53 +0000
commitcc3303cbd87b1dbfe4e41ff8acf4df9e3dc0805d (patch)
tree73fd8c74939bda1ead888d6b6dccbdf68646fabb /src/helpers
parentfix: preload script detection in unit tests (diff)
downloadferdium-app-cc3303cbd87b1dbfe4e41ff8acf4df9e3dc0805d.tar.gz
ferdium-app-cc3303cbd87b1dbfe4e41ff8acf4df9e3dc0805d.tar.zst
ferdium-app-cc3303cbd87b1dbfe4e41ff8acf4df9e3dc0805d.zip
Add Unit Testing for `url-helpers.ts`; Moved all tests into single pattern so they are run as a suite (#112)
Co-authored-by: Vijay A <vraravam@users.noreply.github.com>
Diffstat (limited to 'src/helpers')
-rw-r--r--src/helpers/url-helpers.test.ts116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/helpers/url-helpers.test.ts b/src/helpers/url-helpers.test.ts
new file mode 100644
index 000000000..5ea6fa1a8
--- /dev/null
+++ b/src/helpers/url-helpers.test.ts
@@ -0,0 +1,116 @@
1import * as url_helpers from './url-helpers'
2
3describe('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(new URL('shttps://google'));
42 expect(result).toBe(false);
43 });
44
45 it('returns true for valid http url', () => {
46 const result = url_helpers.isValidExternalURL(new URL('http://google'));
47 expect(result).toBe(true);
48 });
49
50 it('returns true for valid https url', () => {
51 const result = url_helpers.isValidExternalURL(new URL('https://google'));
52 expect(result).toBe(true);
53 });
54 });
55 });
56
57 describe('fixUrl', () => {
58 it('handles with empty string', () => {
59 const result = url_helpers.fixUrl('');
60 expect(result).toEqual('');
61 });
62
63 it('handles with whitespace string', () => {
64 const result = url_helpers.fixUrl(' ');
65 expect(result).toEqual(' ');
66 });
67
68 it('handles with random string', () => {
69 const result = url_helpers.fixUrl('some random string');
70 expect(result).toEqual('some random string');
71 });
72
73 it('handles string starting with http://', () => {
74 expect(url_helpers.fixUrl('http://some/random/url')).toEqual('http://some/random/url');
75 expect(url_helpers.fixUrl('http://some//random//url')).toEqual('http://some/random/url');
76 });
77
78 it('handles string starting with https://', () => {
79 expect(url_helpers.fixUrl('https://some/random/url')).toEqual('https://some/random/url');
80 expect(url_helpers.fixUrl('https://some//random//url')).toEqual('https://some/random/url');
81 });
82
83 it('handles string starting with file://', () => {
84 expect(url_helpers.fixUrl('file://some/random/url')).toEqual('file://some/random/url');
85 expect(url_helpers.fixUrl('file://some//random//url')).toEqual('file://some/random/url');
86 });
87 });
88
89 describe('isValidFileUrl', () => {
90 it('returns false for empty string', () => {
91 const result = url_helpers.isValidFileUrl('');
92 expect(result).toBe(false);
93 });
94
95 it('returns false for whitespace string', () => {
96 const result = url_helpers.isValidFileUrl(' ');
97 expect(result).toBe(false);
98 });
99
100 it('returns false for random string', () => {
101 const result = url_helpers.isValidFileUrl('some random string');
102 expect(result).toBe(false);
103 });
104
105 it('returns false for invalid url', () => {
106 const result = url_helpers.isValidFileUrl('sfile://google');
107 expect(result).toBe(false);
108 });
109
110 it('returns true for valid file url', () => {
111 const fileName = process.platform === 'win32' ? 'file:///c:\\' : 'file:///';
112 const result = url_helpers.isValidFileUrl(fileName);
113 expect(result).toBe(true);
114 });
115 });
116});