1
0

x-if.spec.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { beVisible, haveText, html, notBeVisible, test } from '../../utils'
  2. test('x-if',
  3. html`
  4. <div x-data="{ show: false }">
  5. <button @click="show = ! show">Toggle</button>
  6. <template x-if="show">
  7. <h1>Toggle Me</h1>
  8. </template>
  9. </div>
  10. `,
  11. ({ get }) => {
  12. get('h1').should(notBeVisible())
  13. get('button').click()
  14. get('h1').should(beVisible())
  15. get('button').click()
  16. get('h1').should(notBeVisible())
  17. }
  18. )
  19. test('x-if inside x-for allows nested directives',
  20. html`
  21. <div x-data="{items: [{id: 1, label: '1'}]}">
  22. <template x-for="item in items" :key="item.id">
  23. <div>
  24. <template x-if="item.label">
  25. <span x-text="item.label"></span>
  26. </template>
  27. </div>
  28. </template>
  29. </div>
  30. `,
  31. ({ get }) => {
  32. get('span').should(haveText('1'))
  33. }
  34. )
  35. test('x-if initializes after being added to the DOM to allow x-ref to work',
  36. html`
  37. <div x-data="{}">
  38. <template x-if="true">
  39. <ul x-ref="listbox" data-foo="bar">
  40. <li x-text="$refs.listbox.dataset.foo"></li>
  41. </ul>
  42. </template>
  43. </div>
  44. `,
  45. ({ get }) => {
  46. get('li').should(haveText('bar'))
  47. }
  48. )