aboutsummaryrefslogtreecommitdiffstats
path: root/src
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 /src
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 'src')
-rw-r--r--src/features/utils/FeatureStore.test.js93
-rw-r--r--src/helpers/array-helpers.ts2
-rw-r--r--src/helpers/password-helpers.ts4
-rw-r--r--src/helpers/url-helpers.test.ts119
-rw-r--r--src/jsUtils.test.ts113
-rw-r--r--src/themes/index.test.ts17
-rw-r--r--src/types.ts22
7 files changed, 25 insertions, 345 deletions
diff --git a/src/features/utils/FeatureStore.test.js b/src/features/utils/FeatureStore.test.js
deleted file mode 100644
index 1995431bd..000000000
--- a/src/features/utils/FeatureStore.test.js
+++ /dev/null
@@ -1,93 +0,0 @@
1import PropTypes from 'prop-types';
2import { observable } from 'mobx';
3import { FeatureStore } from './FeatureStore';
4import { createActionsFromDefinitions } from '../../actions/lib/actions';
5import { createActionBindings } from './ActionBinding';
6import { createReactions } from '../../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/src/helpers/array-helpers.ts b/src/helpers/array-helpers.ts
index 3f8806176..45ff932ba 100644
--- a/src/helpers/array-helpers.ts
+++ b/src/helpers/array-helpers.ts
@@ -1,4 +1,4 @@
1export const shuffleArray = (arr: any[]) => 1export const shuffleArray = (arr: any[]): any[] =>
2 arr 2 arr
3 .map(a => [Math.random(), a]) 3 .map(a => [Math.random(), a])
4 .sort((a, b) => a[0] - b[0]) 4 .sort((a, b) => a[0] - b[0])
diff --git a/src/helpers/password-helpers.ts b/src/helpers/password-helpers.ts
index 053321bbf..d5f2d0c49 100644
--- a/src/helpers/password-helpers.ts
+++ b/src/helpers/password-helpers.ts
@@ -1,10 +1,10 @@
1import { createHash, BinaryLike } from 'crypto'; 1import { createHash, BinaryLike } from 'crypto';
2 2
3export function hash(password: BinaryLike) { 3export function hash(password: BinaryLike): string {
4 return createHash('sha256').update(password).digest('base64'); 4 return createHash('sha256').update(password).digest('base64');
5} 5}
6 6
7export function scorePassword(password: string) { 7export function scorePassword(password: string): number {
8 let score = 0; 8 let score = 0;
9 if (!password) { 9 if (!password) {
10 return score; 10 return score;
diff --git a/src/helpers/url-helpers.test.ts b/src/helpers/url-helpers.test.ts
deleted file mode 100644
index 5af3025e9..000000000
--- a/src/helpers/url-helpers.test.ts
+++ /dev/null
@@ -1,119 +0,0 @@
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 const gmailEmbeddedUrl = 'https://www.google.com/url?q=https://github.com/ferdium/ferdium-app/issues/87&source=gmail';
78 expect(url_helpers.fixUrl(gmailEmbeddedUrl)).toEqual(gmailEmbeddedUrl); // it should NOT remove the double-slash from the embedded url in the query string
79 });
80
81 it('handles string starting with https://', () => {
82 expect(url_helpers.fixUrl('https://some/random/url')).toEqual('https://some/random/url');
83 expect(url_helpers.fixUrl('https://some//random//url')).toEqual('https://some/random/url');
84 });
85
86 it('handles string starting with file://', () => {
87 expect(url_helpers.fixUrl('file://some/random/url')).toEqual('file://some/random/url');
88 expect(url_helpers.fixUrl('file://some//random//url')).toEqual('file://some/random/url');
89 });
90 });
91
92 describe('isValidFileUrl', () => {
93 it('returns false for empty string', () => {
94 const result = url_helpers.isValidFileUrl('');
95 expect(result).toBe(false);
96 });
97
98 it('returns false for whitespace string', () => {
99 const result = url_helpers.isValidFileUrl(' ');
100 expect(result).toBe(false);
101 });
102
103 it('returns false for random string', () => {
104 const result = url_helpers.isValidFileUrl('some random string');
105 expect(result).toBe(false);
106 });
107
108 it('returns false for invalid url', () => {
109 const result = url_helpers.isValidFileUrl('sfile://google');
110 expect(result).toBe(false);
111 });
112
113 it('returns true for valid file url', () => {
114 const fileName = process.platform === 'win32' ? 'file:///c:\\' : 'file:///';
115 const result = url_helpers.isValidFileUrl(fileName);
116 expect(result).toBe(true);
117 });
118 });
119});
diff --git a/src/jsUtils.test.ts b/src/jsUtils.test.ts
deleted file mode 100644
index 34cd8f098..000000000
--- a/src/jsUtils.test.ts
+++ /dev/null
@@ -1,113 +0,0 @@
1import * as jsUtils from './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/src/themes/index.test.ts b/src/themes/index.test.ts
deleted file mode 100644
index e9f0f391b..000000000
--- a/src/themes/index.test.ts
+++ /dev/null
@@ -1,17 +0,0 @@
1import makeDefaultThemeConfig from './default';
2import makeDarkThemeConfig from './dark';
3import { theme, ThemeType } from '.';
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});
diff --git a/src/types.ts b/src/types.ts
new file mode 100644
index 000000000..db8711cd3
--- /dev/null
+++ b/src/types.ts
@@ -0,0 +1,22 @@
1declare global {
2 interface Window {
3 ferdium: any;
4 }
5
6 namespace NodeJS {
7 interface ProcessEnv {
8 GITHUB_AUTH_TOKEN: string;
9 NODE_ENV: 'development' | 'production';
10 FERDIUM_APPDATA_DIR?: string;
11 PORTABLE_EXECUTABLE_DIR?: string;
12 ELECTRON_IS_DEV?: string;
13 APPDATA?: string;
14 }
15 }
16}
17
18/**
19 * Workaround to make TS recognize this file as a module.
20 * https://fettblog.eu/typescript-augmenting-global-lib-dom/
21 */
22export {};