utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // This is an invisible template tag for enabling syntax highlighting
  2. // of any string in most editors.
  3. export function html(strings) {
  4. return strings.raw[0]
  5. }
  6. export let test = function (name, template, callback, handleExpectedErrors = false) {
  7. it(name, () => {
  8. injectHtmlAndBootAlpine(cy, template, callback, undefined, handleExpectedErrors)
  9. })
  10. }
  11. test.only = (name, template, callback, handleExpectedErrors = false) => {
  12. it.only(name, () => {
  13. injectHtmlAndBootAlpine(cy, template, callback, undefined, handleExpectedErrors)
  14. })
  15. }
  16. test.skip = (name, template, callback, handleExpectedErrors = false) => {
  17. it.skip(name, () => {
  18. injectHtmlAndBootAlpine(cy, template, callback, undefined, handleExpectedErrors)
  19. })
  20. }
  21. test.retry = (count) => (name, template, callback, handleExpectedErrors = false) => {
  22. it(name, {
  23. retries: {
  24. // During "cypress run"
  25. runMode: count - 1,
  26. // During "cypress open"
  27. openMode: count - 1,
  28. }
  29. }, () => {
  30. injectHtmlAndBootAlpine(cy, template, callback, undefined, handleExpectedErrors)
  31. })
  32. }
  33. test.csp = (name, template, callback, handleExpectedErrors = false) => {
  34. it(name, () => {
  35. injectHtmlAndBootAlpine(cy, template, callback, __dirname+'/spec-csp.html', handleExpectedErrors)
  36. })
  37. }
  38. function injectHtmlAndBootAlpine(cy, templateAndPotentiallyScripts, callback, page, handleExpectedErrors = false) {
  39. let [template, scripts] = Array.isArray(templateAndPotentiallyScripts)
  40. ? templateAndPotentiallyScripts
  41. : [templateAndPotentiallyScripts]
  42. cy.visit(page || __dirname+'/spec.html')
  43. if( handleExpectedErrors ) {
  44. cy.on( 'uncaught:exception', ( error ) => {
  45. if( error.el === undefined && error.expression === undefined ) {
  46. console.warn( 'Expected all errors originating from Alpine to have el and expression. Letting cypress fail the test.', error )
  47. return true
  48. }
  49. return false
  50. } );
  51. }
  52. cy.get('#root').then(([el]) => {
  53. el.innerHTML = template
  54. el.evalScripts(scripts)
  55. cy.get('[alpine-is-ready]', { timeout: 5000 }).should('be.visible');
  56. // We can't just simply reload a page from a test, because we need to
  57. // re-inject all the templates and such. This is a helper to allow
  58. // a test-subject method to perform a redirect all on their own.
  59. let reload = () => {
  60. cy.reload()
  61. cy.get('#root').then(([el]) => {
  62. el.innerHTML = template
  63. el.evalScripts(scripts)
  64. cy.get('[alpine-is-ready]', { timeout: 5000 }).should('be.visible');
  65. })
  66. }
  67. cy.window().then(window => {
  68. callback(cy, reload, window, window.document)
  69. })
  70. })
  71. }
  72. export let haveData = (key, value) => ([ el ]) => expect(root(el)._x_dataStack[0][key]).to.equal(value)
  73. export let haveFocus = () => el => expect(el).to.have.focus
  74. export let notHaveFocus = () => el => expect(el).not.to.be.focused
  75. export let haveAttribute = (name, value) => el => expect(el).to.have.attr(name, value)
  76. export let notHaveAttribute = (name, value) => el => expect(el).not.to.have.attr(name, value)
  77. export let haveText = text => el => expect(el).to.have.text(text)
  78. export let notHaveText = text => el => expect(el).not.to.have.text(text)
  79. export let haveHtml = html => el => expect(el).to.have.html(html)
  80. export let beChecked = () => el => expect(el).to.be.checked
  81. export let notBeChecked = () => el => expect(el).not.to.be.checked
  82. export let beVisible = () => el => expect(el).to.be.visible
  83. export let notBeVisible = () => el => expect(el).not.to.be.visible
  84. export let exist = () => el => expect(el).to.exist
  85. export let notExist = () => el => expect(el).not.to.exist
  86. export let beHidden = () => el => expect(el).to.be.hidden
  87. export let haveClasses = classes => el => classes.forEach(aClass => expect(el).to.have.class(aClass))
  88. export let notHaveClasses = classes => el => classes.forEach(aClass => expect(el).not.to.have.class(aClass))
  89. export let haveValue = value => el => expect(el).to.have.value(value)
  90. export let haveLength = length => el => expect(el).to.have.length(length)
  91. export let beEqualTo = value => el => expect(el).to.eq(value)
  92. export let notHaveComputedStyle = (name, value) => el => {
  93. const win = el[0].ownerDocument.defaultView
  94. expect(win.getComputedStyle(el[0]).getPropertyValue(name)).not.to.eq(value)
  95. }
  96. export let haveComputedStyle = (name, value) => el => {
  97. const win = el[0].ownerDocument.defaultView
  98. expect(win.getComputedStyle(el[0]).getPropertyValue(name)).to.eq(value)
  99. }
  100. export function root(el) {
  101. if (el._x_dataStack) return el
  102. if (! el.parentElement) return
  103. return closestRoot(el.parentElement)
  104. }