aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLibravatar Ricardo Cino <ricardo@cino.io>2022-06-26 01:26:19 +0200
committerLibravatar GitHub <noreply@github.com>2022-06-25 23:26:19 +0000
commit465b5772763ccb8d4970d4c55e30a518abb7be3e (patch)
treee929e34910dbf6a3958cdc4de77dc1dcae93e7b0 /test
parentchore: add more strict types in electron directory (#367) (diff)
downloadferdium-app-465b5772763ccb8d4970d4c55e30a518abb7be3e.tar.gz
ferdium-app-465b5772763ccb8d4970d4c55e30a518abb7be3e.tar.zst
ferdium-app-465b5772763ccb8d4970d4c55e30a518abb7be3e.zip
chore: moved tests to ./test directory (#366)
* chore: allow coverage to be generated from non-tested files
Diffstat (limited to 'test')
-rw-r--r--test/features/utils/FeatureStore.test.js93
-rw-r--r--test/helpers/array-helpers.test.ts15
-rw-r--r--test/helpers/url-helpers.test.ts137
-rw-r--r--test/jsUtils.test.ts113
-rw-r--r--test/themes/index.test.ts17
5 files changed, 375 insertions, 0 deletions
diff --git a/test/features/utils/FeatureStore.test.js b/test/features/utils/FeatureStore.test.js
new file mode 100644
index 000000000..d8d85bec1
--- /dev/null
+++ b/test/features/utils/FeatureStore.test.js
@@ -0,0 +1,93 @@
1import PropTypes from 'prop-types';
2import { observable } from 'mobx';
3import { FeatureStore } from '../../../src/features/utils/FeatureStore';
4import { createActionsFromDefinitions } from '../../../src/actions/lib/actions';
5import { createActionBindings } from '../../../src/features/utils/ActionBinding';
6import { createReactions } from '../../../src/stores/lib/Reaction';
7
8const actions = createActionsFromDefinitions(
9 {
10 countUp: {},
11 },
12 PropTypes.checkPropTypes,
13);
14
15class TestFeatureStore extends FeatureStore {
16 @observable count = 0;
17
18 reactionInvokedCount = 0;
19
20 start() {
21 this._registerActions(
22 createActionBindings([[actions.countUp, this._countUp]]),
23 );
24 this._registerReactions(createReactions([this._countReaction]));
25 }
26
27 _countUp = () => {
28 this.count += 1;
29 };
30
31 _countReaction = () => {
32 this.reactionInvokedCount += 1;
33 };
34}
35
36describe('FeatureStore', () => {
37 let store = null;
38
39 beforeEach(() => {
40 store = new TestFeatureStore();
41 });
42
43 describe('registering actions', () => {
44 it('starts the actions', () => {
45 store.start();
46 actions.countUp();
47 expect(store.count).toBe(1);
48 });
49 it('starts the reactions', () => {
50 store.start();
51 actions.countUp();
52 expect(store.reactionInvokedCount).toBe(1);
53 });
54 });
55
56 describe('stopping the store', () => {
57 it('stops the actions', () => {
58 store.start();
59 actions.countUp();
60 store.stop();
61 actions.countUp();
62 expect(store.count).toBe(1);
63 });
64 it('stops the reactions', () => {
65 store.start();
66 actions.countUp();
67 store.stop();
68 store.count += 1;
69 expect(store.reactionInvokedCount).toBe(1);
70 });
71 });
72
73 describe('toggling the store', () => {
74 it('restarts the actions correctly', () => {
75 store.start();
76 actions.countUp();
77 store.stop();
78 actions.countUp();
79 store.start();
80 actions.countUp();
81 expect(store.count).toBe(2);
82 });
83 it('restarts the reactions correctly', () => {
84 store.start();
85 actions.countUp();
86 store.stop();
87 actions.countUp();
88 store.start();
89 actions.countUp();
90 expect(store.count).toBe(2);
91 });
92 });
93});
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 @@
1import { shuffleArray } from '../../src/helpers/array-helpers';
2
3describe('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 @@
1import * as url_helpers from '../../src/helpers/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(
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});
diff --git a/test/jsUtils.test.ts b/test/jsUtils.test.ts
new file mode 100644
index 000000000..8ef69b46f
--- /dev/null
+++ b/test/jsUtils.test.ts
@@ -0,0 +1,113 @@
1import * as jsUtils from '../src/jsUtils';
2
3describe('jsUtils', () => {
4 describe('ifUndefinedString', () => {
5 it('returns the default value for undefined input', () => {
6 const result = jsUtils.ifUndefinedString(undefined, 'abc');
7 expect(result).toEqual('abc');
8 });
9
10 it('returns the default value for null input', () => {
11 const result = jsUtils.ifUndefinedString(null, 'abc');
12 expect(result).toEqual('abc');
13 });
14
15 it('returns the non-default input value for regular string input', () => {
16 const result = jsUtils.ifUndefinedString('some random string', 'abc');
17 expect(result).toEqual('some random string');
18 });
19 });
20
21 describe('ifUndefined<string>', () => {
22 it('returns the default value for undefined input', () => {
23 const result = jsUtils.ifUndefined<string>(undefined, 'abc');
24 expect(result).toEqual('abc');
25 });
26
27 it('returns the default value for null input', () => {
28 const result = jsUtils.ifUndefined<string>(null, 'abc');
29 expect(result).toEqual('abc');
30 });
31
32 it('returns the non-default input value for regular string input', () => {
33 const result = jsUtils.ifUndefined<string>('some random string', 'abc');
34 expect(result).toEqual('some random string');
35 });
36 });
37
38 describe('ifUndefined<boolean>', () => {
39 it('returns the default value for undefined input', () => {
40 const result = jsUtils.ifUndefined<boolean>(undefined, false);
41 expect(result).toEqual(false);
42 });
43
44 it('returns the default value for null input', () => {
45 const result = jsUtils.ifUndefined<boolean>(null, true);
46 expect(result).toEqual(true);
47 });
48
49 it('returns the non-default input value for regular boolean input', () => {
50 const result = jsUtils.ifUndefined<boolean>(true, false);
51 expect(result).toEqual(true);
52 });
53 });
54
55 describe('ifUndefined<number>', () => {
56 it('returns the default value for undefined input', () => {
57 const result = jsUtils.ifUndefined<number>(undefined, 123);
58 expect(result).toEqual(123);
59 });
60
61 it('returns the default value for null input', () => {
62 const result = jsUtils.ifUndefined<number>(null, 234);
63 expect(result).toEqual(234);
64 });
65
66 it('returns the non-default input value for regular Number input', () => {
67 const result = jsUtils.ifUndefined<number>(1234, 5678);
68 expect(result).toEqual(1234);
69 });
70 });
71
72 describe('convertToJSON', () => {
73 it('returns undefined for undefined input', () => {
74 const result = jsUtils.convertToJSON(undefined);
75 expect(result).toEqual(undefined);
76 });
77
78 it('returns null for null input', () => {
79 const result = jsUtils.convertToJSON(null);
80 expect(result).toEqual(null);
81 });
82
83 it('returns the object for the object input', () => {
84 const result = jsUtils.convertToJSON(['a', 'b']);
85 expect(result).toEqual(['a', 'b']);
86 });
87
88 it('returns the parsed JSON for the string input', () => {
89 const result1 = jsUtils.convertToJSON('{"a":"b","c":"d"}');
90 expect(result1).toEqual({ a: 'b', c: 'd' });
91
92 const result2 = jsUtils.convertToJSON('[{"a":"b"},{"c":"d"}]');
93 expect(result2).toEqual([{ a: 'b' }, { c: 'd' }]);
94 });
95 });
96
97 describe('cleanseJSObject', () => {
98 xit('throws error for undefined input', () => {
99 const result = jsUtils.cleanseJSObject(undefined);
100 expect(result).toThrow();
101 });
102
103 xit('throws error for null input', () => {
104 const result = jsUtils.cleanseJSObject(null);
105 expect(result).toThrow();
106 });
107
108 it('returns cloned object for valid input', () => {
109 const result = jsUtils.cleanseJSObject([{ a: 'b' }, { c: 'd' }]);
110 expect(result).toEqual([{ a: 'b' }, { c: 'd' }]);
111 });
112 });
113});
diff --git a/test/themes/index.test.ts b/test/themes/index.test.ts
new file mode 100644
index 000000000..387a296a8
--- /dev/null
+++ b/test/themes/index.test.ts
@@ -0,0 +1,17 @@
1import makeDefaultThemeConfig from '../../src/themes/default';
2import makeDarkThemeConfig from '../../src/themes/dark';
3import { theme, ThemeType } from '../../src/themes';
4
5describe('Load theme', () => {
6 it('loads default theme', () => {
7 const { colorBackground } = theme('default' as ThemeType);
8 expect(colorBackground).toBe(
9 makeDefaultThemeConfig('default').colorBackground,
10 );
11 });
12
13 it('loads dark theme', () => {
14 const { colorBackground } = theme('dark' as ThemeType);
15 expect(colorBackground).toBe(makeDarkThemeConfig('dark').colorBackground);
16 });
17});