Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

96 líneas
2.4 KiB

  1. /// <reference types="cypress" />
  2. import JSDOMDummyCypress from './jsdom-compat'
  3. type ExpectedSearchValue = Record<string, string> | string
  4. type RetrieveSubmitterFn = (wrapper: any) => any
  5. type HTMLSubmitterElement = HTMLButtonElement | HTMLInputElement
  6. type TestFn = (form: HTMLFormElement, submitter: HTMLSubmitterElement, after: ExpectedSearchValue) => unknown
  7. export const setup = (template: string) => {
  8. if (typeof cy !== 'undefined') {
  9. return () => {
  10. cy.intercept({ url: '/' }, { body: template });
  11. cy.intercept({ url: '/?*' }, { body: template }).as('submitted');
  12. }
  13. }
  14. return () => {
  15. window.document.open(undefined, undefined, undefined, true)
  16. window.document.write(template)
  17. window.document.close()
  18. }
  19. }
  20. export const test = (retrieveSubmitterFn: RetrieveSubmitterFn, testFn: TestFn, expectedValue?: ExpectedSearchValue) => {
  21. let form: HTMLFormElement
  22. let submitter: HTMLButtonElement | HTMLInputElement
  23. let r: any
  24. if (typeof cy !== 'undefined') {
  25. cy
  26. .visit('/')
  27. .get('form')
  28. .then((formResult: any) => {
  29. [form] = Array.from(formResult);
  30. })
  31. r = retrieveSubmitterFn(cy)
  32. .then((submitterQueryEl: any) => {
  33. [submitter] = Array.from(submitterQueryEl as any[])
  34. })
  35. if (typeof expectedValue !== 'undefined') {
  36. r.click()
  37. cy
  38. .wait('@submitted')
  39. .location('search')
  40. .then((search: any) => {
  41. testFn(form, submitter, search)
  42. })
  43. } else {
  44. cy
  45. .location('search')
  46. .then((search: any) => {
  47. testFn(form, submitter, search)
  48. })
  49. }
  50. } else {
  51. r = retrieveSubmitterFn(new JSDOMDummyCypress())
  52. .then((submitterQueryEl: any) => {
  53. [submitter] = Array.from(submitterQueryEl as any[]);
  54. [form] = Array.from(window.document.getElementsByTagName('form'))
  55. testFn(form, submitter, expectedValue)
  56. })
  57. if (typeof expectedValue !== 'undefined') {
  58. r.click()
  59. }
  60. }
  61. }
  62. export const makeSearchParams = (beforeValues: Record<string, unknown> | string) => {
  63. switch (typeof (beforeValues as unknown)) {
  64. case 'string':
  65. return new URLSearchParams(beforeValues as string)
  66. case 'object':
  67. return Object
  68. .entries(beforeValues)
  69. .filter(([k]) => k.trim().length > 0)
  70. .reduce(
  71. (beforeSearchParams, [key, value]) => {
  72. const theValue = !Array.isArray(value) ? [value] : value
  73. theValue.forEach(v => {
  74. beforeSearchParams.append(key, v)
  75. })
  76. return beforeSearchParams
  77. },
  78. new URLSearchParams()
  79. )
  80. default:
  81. break
  82. }
  83. throw new TypeError('Invalid parameter.')
  84. }