setup.ts 2.1 KB

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