setup.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(urls: string | string[]) {
  15. if (Array.isArray(urls)) {
  16. urls.forEach(function(url) {
  17. normalIntercept(url, 'success', {delay: 100});
  18. });
  19. return;
  20. }
  21. return normalIntercept(urls, 'success', {delay: 100});
  22. }
  23. export function intercept(
  24. url: string,
  25. response: (e: {
  26. url: string;
  27. reply: (params: StaticResponse) => void;
  28. search: URLSearchParams;
  29. }) => void,
  30. ) {
  31. cy.intercept(parseUrl(url), function(res) {
  32. function reply(params: StaticResponse) {
  33. return res.reply({delay: 100, ...params});
  34. }
  35. const url = new URL(res.url);
  36. const search = new URLSearchParams(url.search);
  37. response({url: res.url, reply, search});
  38. }).as(url);
  39. }
  40. export function dictionaryIntercept() {
  41. intercept('/dictionary/getDictionary', function({search, reply}) {
  42. const type = search.get('type');
  43. switch (type) {
  44. case '部门字典':
  45. return reply({fixture: 'dictonary/department'});
  46. case '物料类型':
  47. return reply({fixture: 'dictonary/materialType'});
  48. case '仓库':
  49. return reply({fixture: 'dictonary/warehouse'});
  50. case '物料字典':
  51. return reply({fixture: 'dictonary/material'});
  52. case '供应商/客户':
  53. return reply({fixture: 'dictonary/customer'});
  54. case '公司':
  55. return reply({fixture: 'dictonary/company'});
  56. case '计量单位':
  57. return reply({fixture: 'dictonary/unit'});
  58. }
  59. });
  60. }
  61. export function loginSetup() {
  62. cy.visit('/');
  63. cy.get('input[name="accountName"]').type('admin');
  64. cy.get('input[name="accountPassword"]').type('tld123');
  65. cy.getTestId('login_form').submit();
  66. }
  67. export function loginIntercept() {
  68. normalIntercept('/user/login', 'login').as('loginIntercept');
  69. }
  70. export function menuIntercept() {
  71. normalIntercept('/menu/getUserMenuPc', 'menu/basic').as('menuIntercept');
  72. }
  73. export function optionsIntercept() {
  74. normalIntercept('/role/roleBefore', 'options/role');
  75. normalIntercept('/storage/getStorageAll', 'options/storage');
  76. normalIntercept('/userMaterial/getUser', 'options/user');
  77. }
  78. export function exportIntercept(urls: string | string[]) {
  79. if (Array.isArray(urls)) {
  80. urls.forEach(function(url) {
  81. successIntercept(url);
  82. });
  83. return;
  84. }
  85. successIntercept(urls).as('export');
  86. }
  87. export function beforeSetup(autoLogin = true) {
  88. indexedDB.deleteDatabase('filterGroup');
  89. loginIntercept();
  90. menuIntercept();
  91. dictionaryIntercept();
  92. optionsIntercept();
  93. normalIntercept('/gsAccess/', 'gsSuccess', {delay: 100});
  94. successIntercept('/adjustment/addTableAdjustment');
  95. intercept('/adjustment/getTableAdjustment', function({reply}) {
  96. reply({body: {msg: '200', data: null}});
  97. });
  98. if (autoLogin)
  99. loginSetup();
  100. }