persist.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { beVisible, haveText, html, notBeVisible, test } from '../../utils'
  2. test('can persist number',
  3. [html`
  4. <div x-data="{ count: $persist(1) }">
  5. <button @click="count++">Inc</button>
  6. <span x-text="count"></span>
  7. </div>
  8. `],
  9. ({ get }, reload) => {
  10. get('span').should(haveText('1'))
  11. get('button').click()
  12. get('span').should(haveText('2'))
  13. reload()
  14. get('span').should(haveText('2'))
  15. },
  16. )
  17. test('can persist string',
  18. [html`
  19. <div x-data="{ message: $persist('foo') }">
  20. <input x-model="message">
  21. <span x-text="message"></span>
  22. </div>
  23. `],
  24. ({ get }, reload) => {
  25. get('span').should(haveText('foo'))
  26. get('input').clear().type('bar')
  27. get('span').should(haveText('bar'))
  28. reload()
  29. get('span').should(haveText('bar'))
  30. },
  31. )
  32. test('can persist array',
  33. [html`
  34. <div x-data="{ things: $persist(['foo', 'bar']) }">
  35. <button @click="things.push('baz')"></button>
  36. <span x-text="things.join('-')"></span>
  37. </div>
  38. `],
  39. ({ get }, reload) => {
  40. get('span').should(haveText('foo-bar'))
  41. get('button').click()
  42. get('span').should(haveText('foo-bar-baz'))
  43. reload()
  44. get('span').should(haveText('foo-bar-baz'))
  45. },
  46. )
  47. test('can persist object',
  48. [html`
  49. <div x-data="{ something: $persist({foo: 'bar'}) }">
  50. <button id="one" @click="something.foo = 'baz'"></button>
  51. <button id="two" @click="something = {foo: 'bob'}"></button>
  52. <span x-text="something.foo"></span>
  53. </div>
  54. `],
  55. ({ get }, reload) => {
  56. get('span').should(haveText('bar'))
  57. get('button#one').click()
  58. get('span').should(haveText('baz'))
  59. reload()
  60. get('span').should(haveText('baz'))
  61. get('button#two').click()
  62. get('span').should(haveText('bob'))
  63. reload()
  64. get('span').should(haveText('bob'))
  65. },
  66. )
  67. test('can persist boolean',
  68. [html`
  69. <div x-data="{ show: $persist(false) }">
  70. <button @click="show = true"></button>
  71. <template x-if="show">
  72. <span>Foo</span>
  73. </template>
  74. </div>
  75. `],
  76. ({ get }, reload) => {
  77. get('span').should(notBeVisible())
  78. get('button').click()
  79. get('span').should(beVisible())
  80. reload()
  81. get('span').should(beVisible())
  82. },
  83. )
  84. test('can persist multiple components using the same property',
  85. [html`
  86. <div x-data="{ duplicate: $persist('foo') }">
  87. <button @click="duplicate = 'bar'"></button>
  88. <span id="one" x-text="duplicate"></span>
  89. </div>
  90. <div x-data="{ duplicate: $persist('foo') }">
  91. <span id="two" x-text="duplicate"></span>
  92. </div>
  93. `],
  94. ({ get }, reload) => {
  95. get('span#one').should(haveText('foo'))
  96. get('span#two').should(haveText('foo'))
  97. get('button').click()
  98. get('span#one').should(haveText('bar'))
  99. reload()
  100. get('span#one').should(haveText('bar'))
  101. get('span#two').should(haveText('bar'))
  102. },
  103. )