import {
  beforeSetup,
  generateNetworkResult,
  intercept,
  intoMenu,
  successIntercept,
  tableBtnClick,
  validateDelete,
  validatePut,
  validateTableList,
  validateTableSearch,
} from './utils';

const basicData = {
  id: '7',
  name: '系统设置',
  url: '.',
  pId: '0',
  idCode: null,
  type: 'PC',
  page: 0,
  limit: 0,
  orderBy: '0',
  menu: null,
  img: 'xitongguanli',
  modifyUser: 'admin',
  modifyTime: '2023-03-29 13:42:47',
  pid: '0',
};

const TABLE_NAME = 'menu_table',
  TABLE_NAME2 = 'child_menu_table';

describe('菜单管理', function () {
  beforeEach(function () {
    beforeSetup();
    intoMenu('系统设置', '菜单管理');
  });

  beforeEach(function () {
    intercept('/menu/getPage', function ({search, reply}) {
      generateNetworkResult({
        search,
        reply,
        basicData,
        title: 'name',
        skipCondition(name) {
          return name === 'pId' || name === 'type';
        },
      });
    });

    successIntercept(['/menu/addMenu', '/menu/updateMenu', '/menu/delMenu']);
  });

  it('table', function () {
    validateTableList(TABLE_NAME);

    validateTableSearch(TABLE_NAME, ['menuName', 'modifyUser'], {
      toolId: 'menu_filter',
      url: '/menu/getPage',
    });
  });

  it('operation', function () {
    const {validateAdd, validateEdit} = validatePut('menu_modal', TABLE_NAME, {
      label: '菜单',
    });

    validateAdd(['menuName', 'menuIcon', 'menuOrderBy']);
    validateEdit([
      {id: 'menuName', type: 'field', value: basicData.name},
      {id: 'menuIcon', type: 'field', value: basicData.img},
      {id: 'menuOrderBy', type: 'field', value: basicData.orderBy},
    ]);

    validateDelete(TABLE_NAME, '菜单', {eq: 2, title: 'page-1'});
  });

  it('子菜单表格', function () {
    tableBtnClick(TABLE_NAME, 1);
    validateTableList(TABLE_NAME2);

    validateTableSearch(TABLE_NAME2, ['childMenuName', 'modifyUser'], {
      toolId: 'child_menu_filter',
      url: '/menu/getPage',
    });
  });

  it('子菜单操作', function () {
    tableBtnClick(TABLE_NAME, 1);

    const {validateAdd, validateEdit} = validatePut(
      'child_menu_modal',
      TABLE_NAME2,
      {
        label: '菜单',
      },
    );

    validateAdd([
      'menuName',
      'menuUrl',
      {id: 'menuOrderBy', type: 'field', value: '2'},
    ]);

    validateEdit([
      {id: 'menuName', type: 'field', value: basicData.name},
      {id: 'menuUrl', type: 'field', value: basicData.url},
      {id: 'menuOrderBy', type: 'field', value: basicData.orderBy},
    ]);

    validateDelete(TABLE_NAME2, '菜单', {title: 'page-1'});
  });
});
