setup.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 dictionaryIntercept() {
  20. normalIntercept('/dictionary/getDictionary', 'dictonary/department');
  21. }
  22. export function successIntercept(url: string) {
  23. return normalIntercept(
  24. url,
  25. 'success',
  26. {delay: 100},
  27. );
  28. }
  29. export function intercept(
  30. url: string,
  31. response: (e: {
  32. url: string,
  33. reply: (params: StaticResponse) => void,
  34. search: URLSearchParams,
  35. }) => void,
  36. ) {
  37. cy.intercept(
  38. parseUrl(url),
  39. function(res) {
  40. function reply(params: StaticResponse) {
  41. return res.reply({delay: 100, ...params});
  42. }
  43. const url = new URL(res.url);
  44. const search = new URLSearchParams(url.search);
  45. response({url: res.url, reply, search});
  46. },
  47. );
  48. }
  49. export function loginSetup() {
  50. cy.visit('/');
  51. cy.get('input[name="accountName"]').type('admin');
  52. cy.get('input[name="accountPassword"]').type('tld123');
  53. cy.getTestId('login_form').submit();
  54. }
  55. export function loginIntercept() {
  56. normalIntercept('/user/login', 'login').as('loginIntercept');
  57. }
  58. export function menuIntercept() {
  59. normalIntercept('/menu/getUserMenu', 'menu/basic')
  60. .as('menuIntercept');
  61. }
  62. export function optionsIntercept() {
  63. normalIntercept('/role/roleBefore', 'role/options');
  64. normalIntercept('/department/getDepartBefor', 'department/options');
  65. normalIntercept('/storage/getStorageAll', 'storage/options');
  66. }
  67. export function exportIntercept(url: string) {
  68. successIntercept(url).as('export');
  69. }