x-model.spec.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { haveData, haveText, haveValue, html, test } from '../../utils'
  2. test('The name of the test',
  3. html`<h1 x-data x-text="'HEY'"></h1>`,
  4. ({ get }) => get('h1').should(haveText('HEY'))
  5. )
  6. test('x-model has value binding when initialized',
  7. html`
  8. <div x-data="{ foo: 'bar' }">
  9. <input x-model="foo"></input>
  10. </div>
  11. `,
  12. ({ get }) => { get('input').should(haveValue('bar')) }
  13. )
  14. test('x-model updates value when updated via input event',
  15. html`
  16. <div x-data="{ foo: 'bar' }">
  17. <input x-model="foo"></input>
  18. <span x-text="foo"></span>
  19. </div>
  20. `,
  21. ({ get }) => {
  22. get('span').should(haveText('bar'))
  23. get('input').type('baz')
  24. get('span').should(haveText('barbaz'))
  25. }
  26. )
  27. test('x-model has value binding when updated',
  28. html`
  29. <div x-data="{ foo: 'bar' }">
  30. <input x-model="foo"></input>
  31. <button x-on:click="foo = 'baz'">click me</button>
  32. </div>
  33. `,
  34. ({ get }) => {
  35. get('input').should(haveValue('bar'))
  36. get('button').click()
  37. get('input').should(haveValue('baz'))
  38. }
  39. )
  40. test('x-model casts value to number if number modifier is present',
  41. html`
  42. <div x-data="{ foo: null }">
  43. <input type="number" x-model.number="foo"></input>
  44. </div>
  45. `,
  46. ({ get }) => {
  47. get('input').type('123')
  48. get('div').should(haveData('foo', 123))
  49. }
  50. )
  51. test('x-model with number modifier returns: null if empty, original value if casting fails, numeric value if casting passes',
  52. html`
  53. <div x-data="{ foo: 0, bar: '' }">
  54. <input type="number" x-model.number="foo"></input>
  55. <input x-model.number="bar"></input>
  56. </div>
  57. `,
  58. ({ get }) => {
  59. get('input:nth-of-type(1)').clear()
  60. get('div').should(haveData('foo', null))
  61. get('input:nth-of-type(1)').clear().type('-')
  62. get('div').should(haveData('foo', null))
  63. get('input:nth-of-type(1)').clear().type('-123')
  64. get('div').should(haveData('foo', -123))
  65. get('input:nth-of-type(2)').type(123).clear()
  66. get('div').should(haveData('bar', null))
  67. get('input:nth-of-type(2)').clear().type('-')
  68. get('div').should(haveData('bar', '-'))
  69. get('input:nth-of-type(2)').clear().type('-123')
  70. get('div').should(haveData('bar', -123))
  71. }
  72. )
  73. test('x-model trims value if trim modifier is present',
  74. html`
  75. <div x-data="{ foo: '' }">
  76. <input x-model.trim="foo"></input>
  77. <span x-text="foo"></span>
  78. </div>
  79. `,
  80. ({ get }) => {
  81. get('input').type('bar ')
  82. get('div').should(haveData('foo', 'bar'))
  83. }
  84. )