utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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) {
  7. it(name, () => {
  8. injectHtmlAndBootAlpine(cy, template, callback)
  9. })
  10. }
  11. test.only = (name, template, callback) => {
  12. it.only(name, () => {
  13. injectHtmlAndBootAlpine(cy, template, callback)
  14. })
  15. }
  16. test.retry = (count) => (name, template, callback) => {
  17. it(name, {
  18. retries: {
  19. // During "cypress run"
  20. runMode: count - 1,
  21. // During "cypress open"
  22. openMode: count - 1,
  23. }
  24. }, () => {
  25. injectHtmlAndBootAlpine(cy, template, callback)
  26. })
  27. }
  28. test.csp = (name, template, callback) => {
  29. it(name, () => {
  30. injectHtmlAndBootAlpine(cy, template, callback, __dirname+'/spec-csp.html')
  31. })
  32. }
  33. function injectHtmlAndBootAlpine(cy, templateAndPotentiallyScripts, callback, page) {
  34. let [template, scripts] = Array.isArray(templateAndPotentiallyScripts)
  35. ? templateAndPotentiallyScripts
  36. : [templateAndPotentiallyScripts]
  37. cy.visit(page || __dirname+'/spec.html')
  38. cy.get('#root').then(([el]) => {
  39. el.innerHTML = template
  40. el.evalScripts(scripts)
  41. cy.get('[alpine-is-ready]', { timeout: 5000 }).should('be.visible');
  42. // We can't just simply reload a page from a test, because we need to
  43. // re-inject all the templates and such. This is a helper to allow
  44. // a test-subject method to perform a redirect all on their own.
  45. let reload = () => {
  46. cy.reload()
  47. cy.get('#root').then(([el]) => {
  48. el.innerHTML = template
  49. el.evalScripts(scripts)
  50. cy.get('[alpine-is-ready]', { timeout: 5000 }).should('be.visible');
  51. })
  52. }
  53. callback(cy, reload)
  54. })
  55. }
  56. export let haveData = (key, value) => ([ el ]) => expect(root(el)._x_dataStack[0][key]).to.equal(value)
  57. export let haveFocus = () => el => expect(el).to.have.focus
  58. export let notHaveFocus = () => el => expect(el).not.to.be.focused
  59. export let haveAttribute = (name, value) => el => expect(el).to.have.attr(name, value)
  60. export let notHaveAttribute = (name, value) => el => expect(el).not.to.have.attr(name, value)
  61. export let haveText = text => el => expect(el).to.have.text(text)
  62. export let notHaveText = text => el => expect(el).not.to.have.text(text)
  63. export let beChecked = () => el => expect(el).to.be.checked
  64. export let notBeChecked = () => el => expect(el).not.to.be.checked
  65. export let beVisible = () => el => expect(el).to.be.visible
  66. export let notBeVisible = () => el => expect(el).not.to.be.visible
  67. export let beHidden = () => el => expect(el).to.be.hidden
  68. export let haveClasses = classes => el => classes.forEach(aClass => expect(el).to.have.class(aClass))
  69. export let notHaveClasses = classes => el => classes.forEach(aClass => expect(el).not.to.have.class(aClass))
  70. export let haveValue = value => el => expect(el).to.have.value(value)
  71. export let haveLength = length => el => expect(el).to.have.length(length)
  72. export function root(el) {
  73. if (el._x_dataStack) return el
  74. if (! el.parentElement) return
  75. return closestRoot(el.parentElement)
  76. }