setup.ts 2.1 KB

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