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

describe('部门管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    menuTrigger(1, 2);
  });

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

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

        const page = search.get('page');
        reply({fixture: page === '1' ? 'department/list1' : 'department/list2'});
      },
    );

    successIntercept('/department/addDepartment');
    successIntercept('/department/updateDepartment');
    successIntercept('/department/delDepartment');
  });

  it('部门列表', function() {
    validateTableList('department_table');
  });

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

    cy.get('#filter_departmentCode').type('TLD0004');
    cy.get('#filter_departmentName').clear();
    validate('searchCode');

    cy.get('#filter_departmentName').type('name');
    cy.get('#filter_departmentCode').clear();
    validate('searchName');
  });

  const {validateAdd, validateEdit} = validatePut(
    'department_modal',
    '部门',
  );

  it('新增部门', function() {
    function fillField() {
      cy.getTestId('field_departmentName').type('测试部门名称');
    }

    validateAdd(fillField);
  });

  it('修改部门', function() {
    function validateField() {
      cy.getTestId('field_departmentName').should('have.value', '包装部门');
    }

    validateEdit('department_table', validateField);
  });

  it('删除部门', function() {
    validateDelete('department_table');
  });
});
