From 465b5772763ccb8d4970d4c55e30a518abb7be3e Mon Sep 17 00:00:00 2001 From: Ricardo Cino Date: Sun, 26 Jun 2022 01:26:19 +0200 Subject: chore: moved tests to ./test directory (#366) * chore: allow coverage to be generated from non-tested files --- @types/index.d.ts | 21 ----- jest.config.js | 6 +- src/features/utils/FeatureStore.test.js | 93 --------------------- src/helpers/array-helpers.ts | 2 +- src/helpers/password-helpers.ts | 4 +- src/helpers/url-helpers.test.ts | 119 --------------------------- src/jsUtils.test.ts | 113 ------------------------- src/themes/index.test.ts | 17 ---- src/types.ts | 22 +++++ test/features/utils/FeatureStore.test.js | 93 +++++++++++++++++++++ test/helpers/array-helpers.test.ts | 15 ++++ test/helpers/url-helpers.test.ts | 137 +++++++++++++++++++++++++++++++ test/jsUtils.test.ts | 113 +++++++++++++++++++++++++ test/themes/index.test.ts | 17 ++++ tsconfig.json | 4 +- 15 files changed, 404 insertions(+), 372 deletions(-) delete mode 100644 @types/index.d.ts delete mode 100644 src/features/utils/FeatureStore.test.js delete mode 100644 src/helpers/url-helpers.test.ts delete mode 100644 src/jsUtils.test.ts delete mode 100644 src/themes/index.test.ts create mode 100644 src/types.ts create mode 100644 test/features/utils/FeatureStore.test.js create mode 100644 test/helpers/array-helpers.test.ts create mode 100644 test/helpers/url-helpers.test.ts create mode 100644 test/jsUtils.test.ts create mode 100644 test/themes/index.test.ts diff --git a/@types/index.d.ts b/@types/index.d.ts deleted file mode 100644 index 015f1eb91..000000000 --- a/@types/index.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare global { - interface Window { - ferdium: any; - } - - namespace NodeJS { - interface ProcessEnv { - GITHUB_AUTH_TOKEN: string; - NODE_ENV: 'development' | 'production'; - FERDIUM_APPDATA_DIR?: string; - PORTABLE_EXECUTABLE_DIR?: string; - ELECTRON_IS_DEV?: string; - APPDATA?: string; - } - } -} -/** - * Workaround to make TS recognize this file as a module. - * https://fettblog.eu/typescript-augmenting-global-lib-dom/ - */ -export {}; diff --git a/jest.config.js b/jest.config.js index 264014997..964e4d4e6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -20,7 +20,7 @@ module.exports = { collectCoverage: true, // An array of glob patterns indicating a set of files for which coverage information should be collected - // collectCoverageFrom: undefined, + collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/internal-api'], // The directory where Jest should output its coverage files coverageDirectory: 'coverage', @@ -117,9 +117,7 @@ module.exports = { // rootDir: undefined, // A list of paths to directories that Jest should use to search for files in - roots: [ - 'src' - ], + roots: ['src/', 'test/'], // Allows you to use a custom runner instead of Jest's default test runner // runner: "jest-runner", 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 @@ -import PropTypes from 'prop-types'; -import { observable } from 'mobx'; -import { FeatureStore } from './FeatureStore'; -import { createActionsFromDefinitions } from '../../actions/lib/actions'; -import { createActionBindings } from './ActionBinding'; -import { createReactions } from '../../stores/lib/Reaction'; - -const actions = createActionsFromDefinitions( - { - countUp: {}, - }, - PropTypes.checkPropTypes, -); - -class TestFeatureStore extends FeatureStore { - @observable count = 0; - - reactionInvokedCount = 0; - - start() { - this._registerActions( - createActionBindings([[actions.countUp, this._countUp]]), - ); - this._registerReactions(createReactions([this._countReaction])); - } - - _countUp = () => { - this.count += 1; - }; - - _countReaction = () => { - this.reactionInvokedCount += 1; - }; -} - -describe('FeatureStore', () => { - let store = null; - - beforeEach(() => { - store = new TestFeatureStore(); - }); - - describe('registering actions', () => { - it('starts the actions', () => { - store.start(); - actions.countUp(); - expect(store.count).toBe(1); - }); - it('starts the reactions', () => { - store.start(); - actions.countUp(); - expect(store.reactionInvokedCount).toBe(1); - }); - }); - - describe('stopping the store', () => { - it('stops the actions', () => { - store.start(); - actions.countUp(); - store.stop(); - actions.countUp(); - expect(store.count).toBe(1); - }); - it('stops the reactions', () => { - store.start(); - actions.countUp(); - store.stop(); - store.count += 1; - expect(store.reactionInvokedCount).toBe(1); - }); - }); - - describe('toggling the store', () => { - it('restarts the actions correctly', () => { - store.start(); - actions.countUp(); - store.stop(); - actions.countUp(); - store.start(); - actions.countUp(); - expect(store.count).toBe(2); - }); - it('restarts the reactions correctly', () => { - store.start(); - actions.countUp(); - store.stop(); - actions.countUp(); - store.start(); - actions.countUp(); - expect(store.count).toBe(2); - }); - }); -}); 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 @@ -export const shuffleArray = (arr: any[]) => +export const shuffleArray = (arr: any[]): any[] => arr .map(a => [Math.random(), a]) .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 @@ import { createHash, BinaryLike } from 'crypto'; -export function hash(password: BinaryLike) { +export function hash(password: BinaryLike): string { return createHash('sha256').update(password).digest('base64'); } -export function scorePassword(password: string) { +export function scorePassword(password: string): number { let score = 0; if (!password) { 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 @@ -import * as url_helpers from './url-helpers' - -describe('url_helpers', () => { - describe('isValidExternalURL', () => { - describe('with string', () => { - it('returns false for empty string', () => { - const result = url_helpers.isValidExternalURL(''); - expect(result).toBe(false); - }); - - it('returns false for whitespace string', () => { - const result = url_helpers.isValidExternalURL(' '); - expect(result).toBe(false); - }); - - it('returns false for random string', () => { - const result = url_helpers.isValidExternalURL('some random string'); - expect(result).toBe(false); - }); - - it('returns false for invalid url', () => { - const result = url_helpers.isValidExternalURL('shttps://google'); - expect(result).toBe(false); - }); - - it('returns true for valid http url', () => { - const result = url_helpers.isValidExternalURL('http://google'); - expect(result).toBe(true); - }); - - it('returns true for valid https url', () => { - const result = url_helpers.isValidExternalURL('https://google'); - expect(result).toBe(true); - }); - }); - - describe('with URL', () => { - // Note: not testing the invalid string urls - since the URL ctor itself will raise an error - - it('returns false for invalid url', () => { - const result = url_helpers.isValidExternalURL(new URL('shttps://google')); - expect(result).toBe(false); - }); - - it('returns true for valid http url', () => { - const result = url_helpers.isValidExternalURL(new URL('http://google')); - expect(result).toBe(true); - }); - - it('returns true for valid https url', () => { - const result = url_helpers.isValidExternalURL(new URL('https://google')); - expect(result).toBe(true); - }); - }); - }); - - describe('fixUrl', () => { - it('handles with empty string', () => { - const result = url_helpers.fixUrl(''); - expect(result).toEqual(''); - }); - - it('handles with whitespace string', () => { - const result = url_helpers.fixUrl(' '); - expect(result).toEqual(' '); - }); - - it('handles with random string', () => { - const result = url_helpers.fixUrl('some random string'); - expect(result).toEqual('some random string'); - }); - - it('handles string starting with http://', () => { - expect(url_helpers.fixUrl('http://some/random/url')).toEqual('http://some/random/url'); - expect(url_helpers.fixUrl('http://some//random//url')).toEqual('http://some/random/url'); - - const gmailEmbeddedUrl = 'https://www.google.com/url?q=https://github.com/ferdium/ferdium-app/issues/87&source=gmail'; - expect(url_helpers.fixUrl(gmailEmbeddedUrl)).toEqual(gmailEmbeddedUrl); // it should NOT remove the double-slash from the embedded url in the query string - }); - - it('handles string starting with https://', () => { - expect(url_helpers.fixUrl('https://some/random/url')).toEqual('https://some/random/url'); - expect(url_helpers.fixUrl('https://some//random//url')).toEqual('https://some/random/url'); - }); - - it('handles string starting with file://', () => { - expect(url_helpers.fixUrl('file://some/random/url')).toEqual('file://some/random/url'); - expect(url_helpers.fixUrl('file://some//random//url')).toEqual('file://some/random/url'); - }); - }); - - describe('isValidFileUrl', () => { - it('returns false for empty string', () => { - const result = url_helpers.isValidFileUrl(''); - expect(result).toBe(false); - }); - - it('returns false for whitespace string', () => { - const result = url_helpers.isValidFileUrl(' '); - expect(result).toBe(false); - }); - - it('returns false for random string', () => { - const result = url_helpers.isValidFileUrl('some random string'); - expect(result).toBe(false); - }); - - it('returns false for invalid url', () => { - const result = url_helpers.isValidFileUrl('sfile://google'); - expect(result).toBe(false); - }); - - it('returns true for valid file url', () => { - const fileName = process.platform === 'win32' ? 'file:///c:\\' : 'file:///'; - const result = url_helpers.isValidFileUrl(fileName); - expect(result).toBe(true); - }); - }); -}); 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 @@ -import * as jsUtils from './jsUtils' - -describe('jsUtils', () => { - describe('ifUndefinedString', () => { - it('returns the default value for undefined input', () => { - const result = jsUtils.ifUndefinedString(undefined, 'abc'); - expect(result).toEqual('abc'); - }); - - it('returns the default value for null input', () => { - const result = jsUtils.ifUndefinedString(null, 'abc'); - expect(result).toEqual('abc'); - }); - - it('returns the non-default input value for regular string input', () => { - const result = jsUtils.ifUndefinedString('some random string', 'abc'); - expect(result).toEqual('some random string'); - }); - }); - - describe('ifUndefined', () => { - it('returns the default value for undefined input', () => { - const result = jsUtils.ifUndefined(undefined, 'abc'); - expect(result).toEqual('abc'); - }); - - it('returns the default value for null input', () => { - const result = jsUtils.ifUndefined(null, 'abc'); - expect(result).toEqual('abc'); - }); - - it('returns the non-default input value for regular string input', () => { - const result = jsUtils.ifUndefined('some random string', 'abc'); - expect(result).toEqual('some random string'); - }); - }); - - describe('ifUndefined', () => { - it('returns the default value for undefined input', () => { - const result = jsUtils.ifUndefined(undefined, false); - expect(result).toEqual(false); - }); - - it('returns the default value for null input', () => { - const result = jsUtils.ifUndefined(null, true); - expect(result).toEqual(true); - }); - - it('returns the non-default input value for regular boolean input', () => { - const result = jsUtils.ifUndefined(true, false); - expect(result).toEqual(true); - }); - }); - - describe('ifUndefined', () => { - it('returns the default value for undefined input', () => { - const result = jsUtils.ifUndefined(undefined, 123); - expect(result).toEqual(123); - }); - - it('returns the default value for null input', () => { - const result = jsUtils.ifUndefined(null, 234); - expect(result).toEqual(234); - }); - - it('returns the non-default input value for regular Number input', () => { - const result = jsUtils.ifUndefined(1234, 5678); - expect(result).toEqual(1234); - }); - }); - - describe('convertToJSON', () => { - it('returns undefined for undefined input', () => { - const result = jsUtils.convertToJSON(undefined); - expect(result).toEqual(undefined); - }); - - it('returns null for null input', () => { - const result = jsUtils.convertToJSON(null); - expect(result).toEqual(null); - }); - - it('returns the object for the object input', () => { - const result = jsUtils.convertToJSON(['a', 'b']); - expect(result).toEqual(['a', 'b']); - }); - - it('returns the parsed JSON for the string input', () => { - const result1 = jsUtils.convertToJSON('{"a":"b","c":"d"}'); - expect(result1).toEqual({ a: 'b', c: 'd' }); - - const result2 = jsUtils.convertToJSON('[{"a":"b"},{"c":"d"}]'); - expect(result2).toEqual([{ a: 'b' }, { c: 'd' }]); - }); - }); - - describe('cleanseJSObject', () => { - xit('throws error for undefined input', () => { - const result = jsUtils.cleanseJSObject(undefined); - expect(result).toThrow(); - }); - - xit('throws error for null input', () => { - const result = jsUtils.cleanseJSObject(null); - expect(result).toThrow(); - }); - - it('returns cloned object for valid input', () => { - const result = jsUtils.cleanseJSObject([{ a: 'b' }, { c: 'd' }]); - expect(result).toEqual([{ a: 'b' }, { c: 'd' }]); - }); - }); -}); 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 @@ -import makeDefaultThemeConfig from './default'; -import makeDarkThemeConfig from './dark'; -import { theme, ThemeType } from '.'; - -describe('Load theme', () => { - it('loads default theme', () => { - const { colorBackground } = theme('default' as ThemeType); - expect(colorBackground).toBe( - makeDefaultThemeConfig('default').colorBackground, - ); - }); - - it('loads dark theme', () => { - const { colorBackground } = theme('dark' as ThemeType); - expect(colorBackground).toBe(makeDarkThemeConfig('dark').colorBackground); - }); -}); 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 @@ +declare global { + interface Window { + ferdium: any; + } + + namespace NodeJS { + interface ProcessEnv { + GITHUB_AUTH_TOKEN: string; + NODE_ENV: 'development' | 'production'; + FERDIUM_APPDATA_DIR?: string; + PORTABLE_EXECUTABLE_DIR?: string; + ELECTRON_IS_DEV?: string; + APPDATA?: string; + } + } +} + +/** + * Workaround to make TS recognize this file as a module. + * https://fettblog.eu/typescript-augmenting-global-lib-dom/ + */ +export {}; 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 @@ +import PropTypes from 'prop-types'; +import { observable } from 'mobx'; +import { FeatureStore } from '../../../src/features/utils/FeatureStore'; +import { createActionsFromDefinitions } from '../../../src/actions/lib/actions'; +import { createActionBindings } from '../../../src/features/utils/ActionBinding'; +import { createReactions } from '../../../src/stores/lib/Reaction'; + +const actions = createActionsFromDefinitions( + { + countUp: {}, + }, + PropTypes.checkPropTypes, +); + +class TestFeatureStore extends FeatureStore { + @observable count = 0; + + reactionInvokedCount = 0; + + start() { + this._registerActions( + createActionBindings([[actions.countUp, this._countUp]]), + ); + this._registerReactions(createReactions([this._countReaction])); + } + + _countUp = () => { + this.count += 1; + }; + + _countReaction = () => { + this.reactionInvokedCount += 1; + }; +} + +describe('FeatureStore', () => { + let store = null; + + beforeEach(() => { + store = new TestFeatureStore(); + }); + + describe('registering actions', () => { + it('starts the actions', () => { + store.start(); + actions.countUp(); + expect(store.count).toBe(1); + }); + it('starts the reactions', () => { + store.start(); + actions.countUp(); + expect(store.reactionInvokedCount).toBe(1); + }); + }); + + describe('stopping the store', () => { + it('stops the actions', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + expect(store.count).toBe(1); + }); + it('stops the reactions', () => { + store.start(); + actions.countUp(); + store.stop(); + store.count += 1; + expect(store.reactionInvokedCount).toBe(1); + }); + }); + + describe('toggling the store', () => { + it('restarts the actions correctly', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + store.start(); + actions.countUp(); + expect(store.count).toBe(2); + }); + it('restarts the reactions correctly', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + store.start(); + actions.countUp(); + expect(store.count).toBe(2); + }); + }); +}); 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 @@ +import { shuffleArray } from '../../src/helpers/array-helpers'; + +describe('array_helpers', () => { + it('isValidExternalURL', () => { + const originalArray = ['a', 'b', 'c']; + const shuffledArray = shuffleArray(originalArray); + + // Expect the arrays to not be exactly the same + expect(shuffledArray).not.toEqual(originalArray); + + // Expect the arrays to be exactly the same + // when both are sorted alphabetically + expect(shuffledArray.sort()).toEqual(originalArray.sort()); + }); +}); 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 @@ +import * as url_helpers from '../../src/helpers/url-helpers'; + +describe('url_helpers', () => { + describe('isValidExternalURL', () => { + describe('with string', () => { + it('returns false for empty string', () => { + const result = url_helpers.isValidExternalURL(''); + expect(result).toBe(false); + }); + + it('returns false for whitespace string', () => { + const result = url_helpers.isValidExternalURL(' '); + expect(result).toBe(false); + }); + + it('returns false for random string', () => { + const result = url_helpers.isValidExternalURL('some random string'); + expect(result).toBe(false); + }); + + it('returns false for invalid url', () => { + const result = url_helpers.isValidExternalURL('shttps://google'); + expect(result).toBe(false); + }); + + it('returns true for valid http url', () => { + const result = url_helpers.isValidExternalURL('http://google'); + expect(result).toBe(true); + }); + + it('returns true for valid https url', () => { + const result = url_helpers.isValidExternalURL('https://google'); + expect(result).toBe(true); + }); + }); + + describe('with URL', () => { + // Note: not testing the invalid string urls - since the URL ctor itself will raise an error + + it('returns false for invalid url', () => { + const result = url_helpers.isValidExternalURL( + new URL('shttps://google'), + ); + expect(result).toBe(false); + }); + + it('returns true for valid http url', () => { + const result = url_helpers.isValidExternalURL(new URL('http://google')); + expect(result).toBe(true); + }); + + it('returns true for valid https url', () => { + const result = url_helpers.isValidExternalURL( + new URL('https://google'), + ); + expect(result).toBe(true); + }); + }); + }); + + describe('fixUrl', () => { + it('handles with empty string', () => { + const result = url_helpers.fixUrl(''); + expect(result).toEqual(''); + }); + + it('handles with whitespace string', () => { + const result = url_helpers.fixUrl(' '); + expect(result).toEqual(' '); + }); + + it('handles with random string', () => { + const result = url_helpers.fixUrl('some random string'); + expect(result).toEqual('some random string'); + }); + + it('handles string starting with http://', () => { + expect(url_helpers.fixUrl('http://some/random/url')).toEqual( + 'http://some/random/url', + ); + expect(url_helpers.fixUrl('http://some//random//url')).toEqual( + 'http://some/random/url', + ); + + const gmailEmbeddedUrl = + 'https://www.google.com/url?q=https://github.com/ferdium/ferdium-app/issues/87&source=gmail'; + expect(url_helpers.fixUrl(gmailEmbeddedUrl)).toEqual(gmailEmbeddedUrl); // it should NOT remove the double-slash from the embedded url in the query string + }); + + it('handles string starting with https://', () => { + expect(url_helpers.fixUrl('https://some/random/url')).toEqual( + 'https://some/random/url', + ); + expect(url_helpers.fixUrl('https://some//random//url')).toEqual( + 'https://some/random/url', + ); + }); + + it('handles string starting with file://', () => { + expect(url_helpers.fixUrl('file://some/random/url')).toEqual( + 'file://some/random/url', + ); + expect(url_helpers.fixUrl('file://some//random//url')).toEqual( + 'file://some/random/url', + ); + }); + }); + + describe('isValidFileUrl', () => { + it('returns false for empty string', () => { + const result = url_helpers.isValidFileUrl(''); + expect(result).toBe(false); + }); + + it('returns false for whitespace string', () => { + const result = url_helpers.isValidFileUrl(' '); + expect(result).toBe(false); + }); + + it('returns false for random string', () => { + const result = url_helpers.isValidFileUrl('some random string'); + expect(result).toBe(false); + }); + + it('returns false for invalid url', () => { + const result = url_helpers.isValidFileUrl('sfile://google'); + expect(result).toBe(false); + }); + + it('returns true for valid file url', () => { + const fileName = + process.platform === 'win32' ? 'file:///c:\\' : 'file:///'; + const result = url_helpers.isValidFileUrl(fileName); + expect(result).toBe(true); + }); + }); +}); 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 @@ +import * as jsUtils from '../src/jsUtils'; + +describe('jsUtils', () => { + describe('ifUndefinedString', () => { + it('returns the default value for undefined input', () => { + const result = jsUtils.ifUndefinedString(undefined, 'abc'); + expect(result).toEqual('abc'); + }); + + it('returns the default value for null input', () => { + const result = jsUtils.ifUndefinedString(null, 'abc'); + expect(result).toEqual('abc'); + }); + + it('returns the non-default input value for regular string input', () => { + const result = jsUtils.ifUndefinedString('some random string', 'abc'); + expect(result).toEqual('some random string'); + }); + }); + + describe('ifUndefined', () => { + it('returns the default value for undefined input', () => { + const result = jsUtils.ifUndefined(undefined, 'abc'); + expect(result).toEqual('abc'); + }); + + it('returns the default value for null input', () => { + const result = jsUtils.ifUndefined(null, 'abc'); + expect(result).toEqual('abc'); + }); + + it('returns the non-default input value for regular string input', () => { + const result = jsUtils.ifUndefined('some random string', 'abc'); + expect(result).toEqual('some random string'); + }); + }); + + describe('ifUndefined', () => { + it('returns the default value for undefined input', () => { + const result = jsUtils.ifUndefined(undefined, false); + expect(result).toEqual(false); + }); + + it('returns the default value for null input', () => { + const result = jsUtils.ifUndefined(null, true); + expect(result).toEqual(true); + }); + + it('returns the non-default input value for regular boolean input', () => { + const result = jsUtils.ifUndefined(true, false); + expect(result).toEqual(true); + }); + }); + + describe('ifUndefined', () => { + it('returns the default value for undefined input', () => { + const result = jsUtils.ifUndefined(undefined, 123); + expect(result).toEqual(123); + }); + + it('returns the default value for null input', () => { + const result = jsUtils.ifUndefined(null, 234); + expect(result).toEqual(234); + }); + + it('returns the non-default input value for regular Number input', () => { + const result = jsUtils.ifUndefined(1234, 5678); + expect(result).toEqual(1234); + }); + }); + + describe('convertToJSON', () => { + it('returns undefined for undefined input', () => { + const result = jsUtils.convertToJSON(undefined); + expect(result).toEqual(undefined); + }); + + it('returns null for null input', () => { + const result = jsUtils.convertToJSON(null); + expect(result).toEqual(null); + }); + + it('returns the object for the object input', () => { + const result = jsUtils.convertToJSON(['a', 'b']); + expect(result).toEqual(['a', 'b']); + }); + + it('returns the parsed JSON for the string input', () => { + const result1 = jsUtils.convertToJSON('{"a":"b","c":"d"}'); + expect(result1).toEqual({ a: 'b', c: 'd' }); + + const result2 = jsUtils.convertToJSON('[{"a":"b"},{"c":"d"}]'); + expect(result2).toEqual([{ a: 'b' }, { c: 'd' }]); + }); + }); + + describe('cleanseJSObject', () => { + xit('throws error for undefined input', () => { + const result = jsUtils.cleanseJSObject(undefined); + expect(result).toThrow(); + }); + + xit('throws error for null input', () => { + const result = jsUtils.cleanseJSObject(null); + expect(result).toThrow(); + }); + + it('returns cloned object for valid input', () => { + const result = jsUtils.cleanseJSObject([{ a: 'b' }, { c: 'd' }]); + expect(result).toEqual([{ a: 'b' }, { c: 'd' }]); + }); + }); +}); 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 @@ +import makeDefaultThemeConfig from '../../src/themes/default'; +import makeDarkThemeConfig from '../../src/themes/dark'; +import { theme, ThemeType } from '../../src/themes'; + +describe('Load theme', () => { + it('loads default theme', () => { + const { colorBackground } = theme('default' as ThemeType); + expect(colorBackground).toBe( + makeDefaultThemeConfig('default').colorBackground, + ); + }); + + it('loads dark theme', () => { + const { colorBackground } = theme('dark' as ThemeType); + expect(colorBackground).toBe(makeDarkThemeConfig('dark').colorBackground); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index c600df5cf..3e7ffc854 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,6 @@ "module": "CommonJS", "jsx": "react-jsx", "typeRoots": [ - "@types", "node_modules/@types" ], "moduleResolution": "node", @@ -46,6 +45,7 @@ }, "include": [ "src", - "scripts" + "scripts", + "test", ], } -- cgit v1.2.3-54-g00ecf