import {
  intercept,
  loginIntercept,
  loginSetup,
  menuIntercept,
  menuTrigger,
  successIntercept,
  tableBtnClick,
  validateDelete,
  validatePut,
  validateTableList,
  validateTableSearch,
} from './utils';

describe('菜单管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    menuTrigger(3, 0);
  });

  beforeEach(function() {
    intercept('/menu/getPage', function({reply, url: reqUrl}) {
      const url = new URL(reqUrl);
      const search = new URLSearchParams(url.search);

      if (search.get('pId') !== '0') {
        if (search.has('name') && search.get('name').length)
          return reply({fixture: 'menu/childSearchList'});

        const page = search.get('page');
        return reply({fixture: `menu/childList${page}`});
      }

      if (search.has('name') && search.get('name').length)
        return reply({fixture: 'menu/searchList'});

      const page = search.get('page');
      return reply({fixture: `menu/list${page}`});
    });

    successIntercept('/menu/addMenu');
    successIntercept('/menu/updateMenu');
    successIntercept('/menu/delMenu');
  });

  const TABLE_NAME = 'menu_table',
        MODAL_NAME = 'menu_modal',
        LABEL = '菜单';

  it('表格', function() {
    validateTableList(TABLE_NAME);
  });

  it('搜索', function() {
    const validate = validateTableSearch(TABLE_NAME, '菜单搜索');

    cy.get('#filter_menuName').type('name');

    validate();
  });

  it('删除', function() {
    validateDelete(TABLE_NAME, LABEL);
  });

  const {validateAdd, validateEdit} = validatePut(MODAL_NAME, LABEL);

  it('新增', function() {
    validateAdd(function() {
      cy.getTestId('field_menuName').type('菜单名称');
      cy.getTestId('field_menuUrl').type('/test');
      cy.getTestId('field_menuOrderBy').clear().type('8');
    });
  });

  it('修改', function() {
    validateEdit(TABLE_NAME, function() {
      cy.getTestId('field_menuName').should('have.value', '用户设置');
      cy.getTestId('field_menuUrl').should('have.value', '.');
      cy.getTestId('field_menuOrderBy').should('have.value', '1');
    });
  });

  const CHILD_TALBE_NAME = 'child_menu_table',
        CHILD_MODAL_NAME = 'child_menu_modal',
        CHILD_LABEL = '菜单';

  it('子菜单展示', function() {
    tableBtnClick(TABLE_NAME, 2);

    cy.getTestId('child_menu').should('exist').and('be.visible');

    validateTableList(CHILD_TALBE_NAME);
  });

  it('子菜单关闭', function() {
    tableBtnClick(TABLE_NAME, 2);

    cy.getTestId('child_menu').should('exist').and('be.visible');

    cy.getTestId('child_menu_back').click();
    cy.getTestId('child_menu').should('not.exist');
  });

  it('子菜单查询', function() {
    tableBtnClick(TABLE_NAME, 2);

    const validate = validateTableSearch(
      CHILD_TALBE_NAME,
      'childMenuSearch',
      {btnTestId: 'child_menu_search_btn'},
    );
    cy.get('#filter_childMenuName').type('子菜单名称');

    validate();
  });

  it('子菜单删除', function() {
    tableBtnClick(TABLE_NAME, 2);

    validateDelete(CHILD_TALBE_NAME, CHILD_LABEL);
  });

  const {
    validateAdd: childValidateAdd,
    validateEdit: childValidateEdit,
  } = validatePut(
    CHILD_MODAL_NAME,
    CHILD_LABEL,
    {addBtnTestId: 'child_menu_add_btn'},
  );

  it('子菜单新增', function() {
    tableBtnClick(TABLE_NAME, 2);

    childValidateAdd(function() {
      cy.getTestId('field_menuName').type('菜单名称');
      cy.getTestId('field_menuUrl').type('/test');
      cy.getTestId('field_menuOrderBy').clear().type('8');
    });
  });

  it('子菜单修改', function() {
    tableBtnClick(TABLE_NAME, 2);

    childValidateEdit(CHILD_TALBE_NAME, function() {
      cy.getTestId('field_menuName').should('have.value', '用户管理');
      cy.getTestId('field_menuUrl').should('have.value', '/user');
      cy.getTestId('field_menuOrderBy').should('have.value', '0');
    });
  });
});
