diff --git a/jest.config.js b/jest.config.js index f4c38af2..65680fc3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -16,6 +16,7 @@ module.exports = { '/test/fileTransformer.js', }, transformIgnorePatterns: ['/node_modules/'], + setupFiles: ['/test/setup.js'], setupFilesAfterEnv: [], globals: { window: {}, diff --git a/shared/i18n/__mocks__/index.ts b/shared/i18n/__mocks__/index.ts new file mode 100644 index 00000000..da1fd0a6 --- /dev/null +++ b/shared/i18n/__mocks__/index.ts @@ -0,0 +1,3 @@ +export const t = (key: string) => { + return key; +}; diff --git a/shared/index.tsx b/shared/index.tsx index c60db4b8..7dcec451 100644 --- a/shared/index.tsx +++ b/shared/index.tsx @@ -100,3 +100,4 @@ export { version, } from './utils/environment'; export { getTextColorHex } from './utils/string-helper'; +export { sleep } from './utils/utils'; diff --git a/shared/utils/utils.ts b/shared/utils/utils.ts new file mode 100644 index 00000000..59bb6bc5 --- /dev/null +++ b/shared/utils/utils.ts @@ -0,0 +1,10 @@ +/** + * JavaScript 中的 sleep 函数 + * 参考 https://github.com/sqren/await-sleep/blob/master/index.js + * @param milliseconds 阻塞毫秒 + */ +export function sleep(milliseconds: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, milliseconds); + }); +} diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 00000000..b5a30e55 --- /dev/null +++ b/test/setup.js @@ -0,0 +1,8 @@ +// https://github.com/testing-library/react-testing-library#suppressing-unnecessary-warnings-on-react-dom-168 +const originalError = console.error; +console.error = (...args) => { + if (/Warning.*not wrapped in act/.test(args[0])) { + return; + } + originalError.call(console, ...args); +}; diff --git a/web/src/components/modals/GroupDetail/Panel/__tests__/GroupPanelTree.spec.tsx b/web/src/components/modals/GroupDetail/Panel/__tests__/GroupPanelTree.spec.tsx new file mode 100644 index 00000000..c8d544cc --- /dev/null +++ b/web/src/components/modals/GroupDetail/Panel/__tests__/GroupPanelTree.spec.tsx @@ -0,0 +1,53 @@ +jest.mock('tailchat-shared/i18n'); +import { render } from '@testing-library/react'; +import React from 'react'; +import { GroupPanel, GroupPanelType } from 'tailchat-shared'; +import { GroupPanelTree } from '../GroupPanelTree'; + +describe('GroupPanelTree', () => { + const testGroupPanels: GroupPanel[] = [ + { + id: '00', + name: 'section-1', + type: GroupPanelType.GROUP, + }, + { + id: '01', + name: 'panel-01', + type: GroupPanelType.TEXT, + parentId: '00', + }, + { + id: '02', + name: 'panel-02', + type: GroupPanelType.TEXT, + parentId: '00', + }, + { + id: '10', + name: 'section-2', + type: GroupPanelType.GROUP, + }, + { + id: '11', + name: 'panel-11', + type: GroupPanelType.TEXT, + parentId: '10', + }, + { + id: '12', + name: 'panel-12', + type: GroupPanelType.TEXT, + parentId: '10', + }, + ]; + + test('simple render snapshot', async () => { + const onChange = jest.fn(); + const wrapper = render( + + ); + + expect(wrapper.container).toMatchSnapshot(); + }); +}); diff --git a/web/src/components/modals/GroupDetail/Panel/__tests__/__snapshots__/GroupPanelTree.spec.tsx.snap b/web/src/components/modals/GroupDetail/Panel/__tests__/__snapshots__/GroupPanelTree.spec.tsx.snap new file mode 100644 index 00000000..48645600 --- /dev/null +++ b/web/src/components/modals/GroupDetail/Panel/__tests__/__snapshots__/GroupPanelTree.spec.tsx.snap @@ -0,0 +1,242 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GroupPanelTree simple render snapshot 1`] = ` +
+
+
+ +
+ +
+`;