commands.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import '@testing-library/cypress/add-commands'
  3. import 'cypress-real-events/support'
  4. import '@frsource/cypress-plugin-visual-regression-diff'
  5. import { configure } from '@testing-library/cypress'
  6. import { mount } from 'cypress/vue'
  7. configure({ testIdAttribute: 'data-test-id' })
  8. Cypress.Commands.add('mount', mount)
  9. /**
  10. * Simulates a paste event.
  11. * Modified from https://gist.github.com/nickytonline/bcdef8ef00211b0faf7c7c0e7777aaf6
  12. *
  13. * @param subject A jQuery context representing a DOM element.
  14. * @param pasteOptions Set of options for a simulated paste event.
  15. * @param pasteOptions.pastePayload Simulated data that is on the clipboard.
  16. * @param pasteOptions.pasteFormat The format of the simulated paste payload. Default value is 'text'.
  17. * @param pasteOptions.files A list of assisiated file, if any
  18. *
  19. * @returns The subject parameter.
  20. *
  21. * @example
  22. * cy.get('body').paste({
  23. * pasteType: 'application/json',
  24. * pastePayload: {hello: 'yolo'},
  25. * });
  26. */
  27. Cypress.Commands.add(
  28. 'paste',
  29. { prevSubject: true },
  30. function onPaste(subject, pasteOptions) {
  31. const { pastePayload = '', pasteType = 'text', files = [] } = pasteOptions
  32. const data =
  33. pasteType === 'application/json'
  34. ? JSON.stringify(pastePayload)
  35. : pastePayload
  36. // https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
  37. const clipboardData = new DataTransfer()
  38. clipboardData.setData(pasteType, data)
  39. files.forEach((file) => {
  40. clipboardData.items.add(file)
  41. })
  42. // https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event
  43. const pasteEvent = new ClipboardEvent('paste', {
  44. bubbles: true,
  45. cancelable: true,
  46. dataType: pasteType,
  47. data,
  48. clipboardData,
  49. })
  50. subject[0].dispatchEvent(pasteEvent)
  51. return subject
  52. },
  53. )
  54. Cypress.Commands.add(
  55. 'selectText',
  56. { prevSubject: true },
  57. (subject, direction, size) => {
  58. return cy
  59. .wrap(subject)
  60. .realPress([
  61. 'Shift',
  62. ...new Array(size).fill(
  63. direction === 'right' ? 'ArrowRight' : 'ArrowLeft',
  64. ),
  65. ])
  66. },
  67. )