var-picker.test.tsx 5.42 KB
import { render, screen } from '@testing-library/react';
import React from 'react';
import VarPicker from './var-picker';

// const Test = () => {
//   const [number1, setNumber] = useState(0);

//   return (
//     <div>
//       <button
//         onClick={() => {
//           setNumber(5);
//         }}
//         data-testid="clickBtn"
//         type="button"
//       >
//         点击
//       </button>
//       {number1 && <span data-testid="showText">{number1}</span>}
//     </div>
//   );
// };

// "test": "jest --no-cache --config jest.config.js"
jest.mock('@qx/utils', () => ({
  getWidgetsIcon: () => <span>icon</span>,
}));

const getByTextWithFallback = (text: any) => {
  try {
    return screen.getByText(text);
  } catch (error) {
    // 如果无法找到精确的文本匹配,尝试使用包含文本的元素
    return screen.getByRole('tree', { name: new RegExp(text) });
  }
};
describe('test component', () => {
  test('click event', () => {
    render(
      <VarPicker
        onPick={jest.fn()}
        dataSource={[
          {
            key: 'FORM',
            titleStr: '表单变量',
            children: [],
          },
          {
            key: 'SYS',
            titleStr: '系统变量',
            children: [
              {
                key: '${SYS.CUR_USER}',
                titleStr: '当前人员',
                widget: 'qxObject',
                fieldGroupType: 'OBJECT',
                attrs: [
                  {
                    key: '${SYS.CUR_USER.id}',
                    titleStr: '用户ID',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_USER.name}',
                    titleStr: '用户名',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_USER.code}',
                    titleStr: '工号',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_USER.mobile}',
                    titleStr: '手机号',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_USER.email}',
                    titleStr: '邮箱',
                    fieldGroupType: 'TEXT',
                  },
                ],
              },
              {
                key: '${SYS.CUR_ORG}',
                titleStr: '当前部门',
                widget: 'qxObject',
                fieldGroupType: 'OBJECT',
                attrs: [
                  {
                    key: '${SYS.CUR_ORG.id}',
                    titleStr: '部门ID',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_ORG.code}',
                    titleStr: '部门编号',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_ORG.name}',
                    titleStr: '部门名称',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_ORG.orgLevel}',
                    titleStr: '部门层级',
                    fieldGroupType: 'TEXT',
                  },
                ],
              },
              {
                key: '${SYS.CUR_CORP}',
                titleStr: '当前公司',
                widget: 'qxObject',
                fieldGroupType: 'OBJECT',
                attrs: [
                  {
                    key: '${SYS.CUR_CORP.corpCode}',
                    titleStr: '公司编号',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.CUR_CORP.corpName}',
                    titleStr: '公司名称',
                    fieldGroupType: 'TEXT',
                  },
                ],
              },
              {
                key: '${SYS.CUR_DATE}',
                titleStr: '当前时间',
                widget: 'qxDatetime',
                fieldGroupType: 'DATE',
              },
              {
                key: '${SYS.LOGIN_INFO}',
                titleStr: '登录信息',
                widget: 'qxObject',
                fieldGroupType: 'OBJECT',
                attrs: [
                  {
                    key: '${SYS.LOGIN_INFO.ipAddr}',
                    titleStr: 'IP地址',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.LOGIN_INFO.terminal}',
                    titleStr: '终端类型',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.LOGIN_INFO.browser}',
                    titleStr: '浏览器信息',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.LOGIN_INFO.browserRoute}',
                    titleStr: '当前浏览器路由信息',
                    fieldGroupType: 'TEXT',
                  },
                  {
                    key: '${SYS.LOGIN_INFO.session}',
                    titleStr: 'session信息',
                    fieldGroupType: 'TEXT',
                  },
                ],
              },
            ],
          },
        ]}
      />,
    );

    expect(screen.getByRole('tree')).not.toBeNull();
    // userEvent.click(screen.getByTestId('clickBtn'));
    // // 异步
    // await waitFor(() => {
    // });
  });
});