Skip to content

Commit

Permalink
refactor: add assertion utils separately for main and renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Aug 12, 2023
1 parent 5c41452 commit c4dd049
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 25 deletions.
1 change: 0 additions & 1 deletion packages/app-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"lint": "eslint .",
"pre-commit": "lint-staged",
"test": "unit-test",
"test-full": "unit-test",
"typecheck": "tsc --project tsconfig.json --noEmit"
},
"devDependencies": {
Expand Down
15 changes: 0 additions & 15 deletions packages/app-common/src/utils/assert.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/app-common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './assert';
export * from './function';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test fn \`assertIsNever\` should throw an error 1`] = `""hello world" should be "never" type."`;
10 changes: 10 additions & 0 deletions packages/app-main/src/main/utils/assert/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AssertionError } from 'node:assert';

import { assertIsNever } from '../index';

describe(`Test fn \`${assertIsNever.name}\``, () => {
it('should throw an error', () => {
expect(() => assertIsNever('hello world' as never)).toThrow(AssertionError);
expect(() => assertIsNever('hello world' as never)).toThrowErrorMatchingSnapshot();
});
});
8 changes: 8 additions & 0 deletions packages/app-main/src/main/utils/assert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AssertionError } from 'node:assert';

/**
* The function for asserting whether a value's type is `never`.
*/
export function assertIsNever(value: never): never {
throw new AssertionError({ message: `"${String(value)}" should be "never" type.` });
}
3 changes: 2 additions & 1 deletion packages/app-main/src/main/window/windowManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WindowType } from '@ter/app-common/apis/app';
import type { Nullable } from '@ter/app-common/types';
import { assertIsNever } from '@ter/app-common/utils';

import { assertIsNever } from '../utils/assert';

import type { AbstractWindow } from './abstractWindow';
import { MainWindow } from './mainWindow';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Nullable } from '@ter/app-common/types';
import { assert, noop } from '@ter/app-common/utils';
import { noop } from '@ter/app-common/utils';
import { act, render } from '@testing-library/react';
import { useState } from 'react';

import { validateHookValueNotChanged } from 'src/__tests__/hook';
import { assert } from 'src/utils/assert';

import { usePersistFn } from '../usePersistFn';

Expand Down
8 changes: 5 additions & 3 deletions packages/app-renderer/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WindowType } from '@ter/app-common/apis/app';
import { assert } from '@ter/app-common/utils';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

Expand All @@ -8,14 +7,17 @@ import './index.css';
import { appAPI } from './apis';
import { MainWindow } from './MainWindow';
import { reportWebVitals } from './reportWebVitals';
import { assert, assertIsNever } from './utils/assert';

function Window(): JSX.Element | null {
function Window(): JSX.Element {
const type = appAPI.windowType;

// eslint-disable-next-line default-case
switch (type) {
case WindowType.MAIN:
return <MainWindow />;

default:
assertIsNever(type);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test fn \`assert\` should assert an expression 1`] = `"Assertion Error."`;

exports[`Test fn \`assertIsNever\` should throw an error 1`] = `"hello world should be "never" type."`;
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { assert, assertIsNever } from '../assert';
import { AssertionError, assert, assertIsNever } from '../index';

describe(`Test fn \`${assert.name}\``, () => {
it('should assert an expression', () => {
const str = 'hello world';

expect(() => assert(typeof str === 'string')).not.toThrow();
expect(() => assert(typeof str === 'number')).toThrow();
expect(() => assert(typeof str === 'number')).toThrow(AssertionError);
expect(() => assert(typeof str === 'number')).toThrowErrorMatchingSnapshot();
});
});

describe(`Test fn \`${assertIsNever.name}\``, () => {
it('should throw an error', () => {
expect(() => assertIsNever('hello world' as never)).toThrow();
expect(() => assertIsNever('hello world' as never)).toThrow(AssertionError);
expect(() => assertIsNever('hello world' as never)).toThrowErrorMatchingSnapshot();
});
});
17 changes: 17 additions & 0 deletions packages/app-renderer/src/utils/assert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class AssertionError extends Error {}

/**
* @param value The input that is checked for being truthy.
*/
export function assert(value: unknown, message?: string): asserts value {
if (!value) {
throw new AssertionError(message ?? 'Assertion Error.');
}
}

/**
* The function for asserting whether a value's type is `never`.
*/
export function assertIsNever(value: never): never {
throw new AssertionError(`${String(value)} should be "never" type.`);
}

0 comments on commit c4dd049

Please sign in to comment.