persist.spec.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { haveText, html, test } from '../../utils'
  2. test('can perist 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 perist 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 perist 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. )