setup.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {StaticResponse, RouteHandler} from 'cypress/types/net-stubbing';
  2. function parseUrl(url: string) {
  3. return `http://*${url.indexOf('/') === 0 ? '' : '/'}${url}${
  4. url.includes('*') ? '' : '*'
  5. }`;
  6. }
  7. export function normalIntercept(
  8. url: string,
  9. fixture: string,
  10. options?: Omit<RouteHandler, 'fixture'>,
  11. ) {
  12. return cy.intercept(parseUrl(url), {fixture, ...(options ?? {})});
  13. }
  14. export function successIntercept(url: string) {
  15. return normalIntercept(url, 'success', {delay: 100});
  16. }
  17. export function intercept(
  18. url: string,
  19. response: (e: {
  20. url: string;
  21. reply: (params: StaticResponse) => void;
  22. search: URLSearchParams;
  23. }) => void,
  24. ) {
  25. cy.intercept(parseUrl(url), function (res) {
  26. function reply(params: StaticResponse) {
  27. return res.reply({delay: 100, ...params});
  28. }
  29. const url = new URL(res.url);
  30. const search = new URLSearchParams(url.search);
  31. response({url: res.url, reply, search});
  32. });
  33. }
  34. export function dictionaryIntercept() {
  35. intercept('/dictionary/getDictionary', function ({search, reply}) {
  36. const type = search.get('type');
  37. switch (type) {
  38. case '物料字典':
  39. return reply({fixture: 'dictonary/goods'});
  40. case '部门字典':
  41. return reply({fixture: 'dictonary/department'});
  42. case '物料类型':
  43. return reply({fixture: 'dictonary/goodsType'});
  44. }
  45. });
  46. }
  47. export function loginSetup() {
  48. cy.visit('/');
  49. cy.get('input[name="accountName"]').type('admin');
  50. cy.get('input[name="accountPassword"]').type('tld123');
  51. cy.getTestId('login_form').submit();
  52. }
  53. export function loginIntercept() {
  54. normalIntercept('/user/login', 'login').as('loginIntercept');
  55. }
  56. export function menuIntercept() {
  57. normalIntercept('/menu/getUserMenu', 'menu/basic').as('menuIntercept');
  58. }
  59. export function optionsIntercept() {
  60. normalIntercept('/role/roleBefore', 'role/options');
  61. normalIntercept('/department/getDepartBefor', 'department/options');
  62. normalIntercept('/storage/getStorageAll', 'storage/options');
  63. }
  64. export function exportIntercept(url: string) {
  65. successIntercept(url).as('export');
  66. }
  67. export function beforeSetup() {
  68. loginIntercept();
  69. menuIntercept();
  70. }