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

describe('角色管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    menuTrigger(1, 1);
  });

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

      if (search.has('roleName') && search.get('roleName').length)
        return reply({fixture: 'role/search'});

      const page = search.get('page');
      reply({fixture: page === '1' ? 'role/list1' : 'role/list2'});
    });
    intercept('/menu/getPage', function({reply}) {
      reply({fixture: 'menu/pda'});
    });

    successIntercept('/role/addRole');
    successIntercept('/role/updateRole');
    successIntercept('/role/delRole');
    normalIntercept('/menu/getMenu', 'menu/all');
    exportIntercept('/role/export');
  });

  const TABLE_NAME = 'role_table',
        MODAL_NAME = 'role_modal',
        LABEL = '角色';

  it('角色列表', function() {
    validateTableList(TABLE_NAME);
  });

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

    cy.get('#filter_roleName').type('roleName');
    validate();
  });

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

  it('新增操作', function() {
    validateAdd(function() {
      cy.get('#operation_roleName').type('roleName');
      cy.get('#operation_roleRemarks').type('roleRemarks');
    });
  });

  it('修改操作', function() {
    validateEdit(TABLE_NAME, function() {
      cy.get('#operation_roleName').should('have.value', '仓库管理员');
      cy.get('#operation_roleRemarks').should('have.value', 'cangk');
    });
  });

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

  it('设置菜单权限', function() {
    tableBtnClick(TABLE_NAME, 2);

    cy.getTestId('role_tree_modal').find('h3')
      .should('include.html', '菜单权限');
    cy.getTestId('role_tree_modal').should('exist').and('be.visible');

    cy.getTestId('role_tree').find('.ant-tree-list-holder-inner')
      .find('.ant-tree-treenode').each(function(res, idx) {
        idx <= 3
          ? expect(res.hasClass('ant-tree-treenode-checkbox-checked')).to.true
          : expect(res.hasClass('ant-tree-treenode-checkbox-checked')).to.false;
      }).should('have.length', 9);

    validateModalBtnGroup('role_tree_modal');
    cy.getTestId('modal_btn_group').find('.ant-btn-loading').should('exist');

    cy.get('.ant-message-notice-content').should('include.text', '设置成功');
    cy.getTestId('role_tree_modal').should('not.exist');
  });

  it.only('设置pda权限', function() {
    tableBtnClick(TABLE_NAME, 3);

    cy.get('.ant-transfer-list').eq(0).find('.ant-transfer-list-content')
      .children('.ant-transfer-list-content-item').should('have.length', 4);
    cy.get('.ant-transfer-list').eq(1).find('.ant-transfer-list-content')
      .children('.ant-transfer-list-content-item').should('have.length', 2);

    cy.getTestId('role_pda_menu_modal').find('form').submit();

    validateModalBtnGroup('role_pda_menu_modal');

    cy.get('.ant-message-notice-content').should('include.text', '设置成功');
    cy.getTestId('role_tree_modal').should('not.exist');
  });

  it('导出', function() {
    validateExport();
  });
});
