setup.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 loginSetup() {
  47. cy.visit('/');
  48. cy.get('input[name="accountName"]').type('admin');
  49. cy.get('input[name="accountPassword"]').type('tld123');
  50. cy.getTestId('login_form').submit();
  51. }
  52. export function loginIntercept() {
  53. normalIntercept('/user/login', 'login').as('loginIntercept');
  54. }
  55. export function menuIntercept() {
  56. normalIntercept('/menu/getUserMenu', 'menu/basic')
  57. .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. }