spec.html 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <html>
  2. <!-- Using a <blockquote> because it's an obscure tag -->
  3. <!-- which allows us to use document.querySelector() -->
  4. <!-- with generic tags freely inside Cypress tests. -->
  5. <blockquote id="root">
  6. <!-- This is where our test subjects will be injected. -->
  7. </blockquote>
  8. <script src="/../../packages/history/dist/cdn.js"></script>
  9. <script src="/../../packages/morph/dist/cdn.js"></script>
  10. <script src="/../../packages/persist/dist/cdn.js"></script>
  11. <script src="/../../packages/trap/dist/cdn.js"></script>
  12. <script src="/../../packages/intersect/dist/cdn.js"></script>
  13. <script>
  14. let root = document.querySelector('#root')
  15. // We aren't loading Alpine directly because we are expecting
  16. // Cypress to inject HTML into "#root", THEN we'll call
  17. // this function from Cypress to boot everything up.
  18. root.evalScripts = (extraJavaScript) => {
  19. // Load bespoke JavaScript.
  20. if (extraJavaScript) {
  21. let script = document.createElement('script')
  22. script.src = `data:text/javascript;base64,${btoa(`
  23. document.addEventListener('alpine:init', () => {
  24. ${extraJavaScript}
  25. })
  26. `)}`
  27. root.after(script)
  28. }
  29. // Load all injected script tags.
  30. root.querySelectorAll('script').forEach(el => {
  31. eval(el.innerHTML)
  32. })
  33. // Load Alpine.
  34. let script = document.createElement('script')
  35. script.src = '/../../packages/alpinejs/dist/cdn.js'
  36. root.after(script)
  37. document.addEventListener('alpine:initialized', () => {
  38. let readyEl = document.createElement('blockquote')
  39. readyEl.setAttribute('alpine-is-ready', true)
  40. readyEl.style.width = '1px'
  41. readyEl.style.height = '1px'
  42. document.querySelector('blockquote').after(readyEl)
  43. })
  44. }
  45. </script>
  46. </html>