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

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

  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 = 'PDA菜单';

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

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

    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_menuID').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_menuID').should('have.value', '.');
      cy.getTestId('field_menuOrderBy').should('have.value', '1');
    });
  });
});
