x-modelable.spec.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { haveText, html, test } from '../../utils'
  2. test('can expose data for x-model binding',
  3. html`
  4. <div x-data="{ outer: 'foo' }">
  5. <div x-data="{ inner: 'bar' }" x-modelable="inner" x-model="outer">
  6. <h1 x-text="outer"></h1>
  7. <h2 x-text="inner"></h2>
  8. <button @click="inner = 'bob'" id="1">change inner</button>
  9. <button @click="outer = 'lob'" id="2">change outer</button>
  10. </div>
  11. </div>
  12. `,
  13. ({ get }) => {
  14. get('h1').should(haveText('foo'))
  15. get('h2').should(haveText('foo'))
  16. get('#1').click()
  17. get('h1').should(haveText('bob'))
  18. get('h2').should(haveText('bob'))
  19. get('#2').click()
  20. get('h1').should(haveText('lob'))
  21. get('h2').should(haveText('lob'))
  22. }
  23. )
  24. test('x-modelable works when inside x-bind and x-model is outside',
  25. html`
  26. <div x-data="{ outer: 'foo', thing: {
  27. ['x-modelable']: 'inner',
  28. } }">
  29. <div x-data="{ inner: 'bar' }" x-bind="thing" x-model="outer">
  30. <h1 x-text="outer"></h1>
  31. <h2 x-text="inner"></h2>
  32. <button @click="inner = 'bob'" id="1">change inner</button>
  33. <button @click="outer = 'lob'" id="2">change outer</button>
  34. </div>
  35. </div>
  36. `,
  37. ({ get }) => {
  38. get('h1').should(haveText('foo'))
  39. get('h2').should(haveText('foo'))
  40. get('#1').click()
  41. get('h1').should(haveText('bob'))
  42. get('h2').should(haveText('bob'))
  43. get('#2').click()
  44. get('h1').should(haveText('lob'))
  45. get('h2').should(haveText('lob'))
  46. }
  47. )
  48. test('x-modelable removes the event listener used by corresponding x-model',
  49. html`
  50. <div x-data="{ outer: 'foo' }">
  51. <div x-data="{ inner: 'bar' }" x-modelable="inner" x-model="outer">
  52. <h1 x-text="outer"></h1>
  53. <h2 x-text="inner"></h2>
  54. <button id="1" @click="$dispatch('input', 'baz')"></button>
  55. </div>
  56. </div>
  57. `,
  58. ({ get }) => {
  59. get('h1').should(haveText('foo'))
  60. get('h2').should(haveText('foo'))
  61. get('#1').click()
  62. get('h1').should(haveText('foo'))
  63. get('h2').should(haveText('foo'))
  64. }
  65. )