import {
  loginIntercept,
  loginSetup,
  menuIntercept,
  optionsIntercept,
  menuTrigger,
  selectClick,
  validateSelect,
  validateDelete,
  validatePut,
  validateTableList,
  validateTableSearch,
  successIntercept,
  intercept,
  validateExport,
  exportIntercept,
} from './utils';

describe('用户管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    optionsIntercept();
    menuTrigger(1, 0);
  });

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

        if (search.has('userName') && search.get('userName').length)
          return reply({fixture: 'user/nameSearch'});
        if (search.has('code') && search.get('code').length)
          return reply({fixture: 'user/codeSearch'});

        if (search.has('id') && search.get('id').length)
          return reply({fixture: 'user/userInfo'});

        const page = search.get('page');
        reply({fixture: page === '1' ? 'user/userList1' : 'user/userList2'});
      },
    );
    successIntercept('/user/delUser');
    successIntercept('/user/addUser');
    successIntercept('/user/updateUser');
    exportIntercept('/user/userExport');
  });

  const TABLE_NAME = 'user_table',
        MODAL_NAME = 'user_add_modal',
        LABEL = '用户';

  it('用户列表', function() {
    validateTableList(TABLE_NAME);
  });

  it('搜索', function() {
    const validate = validateTableSearch(TABLE_NAME);
    cy.get('.ant-pagination').find('li[title="2"]').click();

    cy.get('input[name="useName"]').type('admin');
    cy.get('input[name="useCode"]').clear();
    validate('searchName');

    cy.get('input[name="useName"]').clear();
    cy.get('input[name="useCode"]').type('1234');
    validate('searchCode');
  });

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

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

  it('新增操作', function() {
    validateAdd(function() {
      cy.get('input[name="userName"]').type('name');
      cy.get('input[name="userPassword"]').type('password');
      cy.get('input[name="userRealName"]').type('realname');
      cy.get('input[name="userEmail"]').type('email');
      cy.get('input[name="userLandline"]').type('userLandline');
      cy.get('input[name="userPhone"]').type('userPhone');
      selectClick('select_userDepartment', 0);
      selectClick('select_userRole', 0);
    });
  });

  it('修改操作', function() {
    validateEdit(TABLE_NAME, function() {
      cy.get('input[name="userName"]').should('have.value', '123412');
      cy.get('input[name="userPassword"]').should(
        'have.value',
        '44349BD19046F3D527D67A5D0D414B09');
      cy.get('input[name="userRealName"]').should('have.value', '测试');
      cy.get('input[name="userEmail"]').should('have.value', '123@adfa.com');
      cy.get('input[name="userLandline"]').should('have.value', '1231123');
      cy.get('input[name="userPhone"]').should('have.value', '1532');
      validateSelect('select_userDepartment', '物流部门');
      validateSelect('select_userRole', '物流管理员');
    });
  });

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