setup.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }) => void,
  32. ) {
  33. cy.intercept(
  34. parseUrl(url),
  35. function(res) {
  36. function reply(params: StaticResponse) {
  37. return res.reply({delay: 100, ...params});
  38. }
  39. response({url: res.url, reply});
  40. },
  41. );
  42. }
  43. export function loginSetup() {
  44. cy.visit('/');
  45. cy.get('input[name="accountName"]').type('admin');
  46. cy.get('input[name="accountPassword"]').type('tld123');
  47. cy.getTestId('login_form').submit();
  48. }
  49. export function loginIntercept() {
  50. normalIntercept('/user/login', 'login').as('loginIntercept');
  51. }
  52. export function menuIntercept() {
  53. normalIntercept('/menu/getUserMenu', 'menu/basic')
  54. .as('menuIntercept');
  55. }
  56. export function optionsIntercept() {
  57. normalIntercept('/role/roleBefore', 'role/options');
  58. normalIntercept('/department/getDepartBefor', 'department/options');
  59. normalIntercept('/storage/getStorageAll', 'storage/options');
  60. }
  61. export function exportIntercept(url: string) {
  62. successIntercept(url).as('export');
  63. }