1
0

persist.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { beEqualTo, 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. )
  104. test('can persist using an alias',
  105. [html`
  106. <div x-data="{ show: $persist(false) }">
  107. <template x-if="show">
  108. <span id="one">Foo</span>
  109. </template>
  110. </div>
  111. <div x-data="{ show: $persist(false).as('foo') }">
  112. <button id="test" @click="show = true"></button>
  113. <template x-if="show">
  114. <span id="two">Foo</span>
  115. </template>
  116. </div>
  117. `],
  118. ({ get }, reload) => {
  119. get('span#one').should(notBeVisible())
  120. get('span#two').should(notBeVisible())
  121. get('button').click()
  122. get('span#one').should(notBeVisible())
  123. get('span#two').should(beVisible())
  124. reload()
  125. get('span#one').should(notBeVisible())
  126. get('span#two').should(beVisible())
  127. },
  128. )
  129. test('aliases do not affect other $persist calls',
  130. [html`
  131. <div x-data="{ show: $persist(false).as('foo') }">
  132. <button id="test" @click="show = true"></button>
  133. <template x-if="show">
  134. <span id="two">Foo</span>
  135. </template>
  136. </div>
  137. <div x-data="{ open: $persist(false) }">
  138. <template x-if="open">
  139. <span id="one">Foo</span>
  140. </template>
  141. </div>
  142. `],
  143. ({ get }, reload) => {
  144. get('span#one').should(notBeVisible())
  145. get('span#two').should(notBeVisible())
  146. get('button').click()
  147. get('span#one').should(notBeVisible())
  148. get('span#two').should(beVisible())
  149. reload()
  150. get('span#one').should(notBeVisible())
  151. get('span#two').should(beVisible())
  152. },
  153. )
  154. test('can persist to custom storage',
  155. [html`
  156. <div x-data="{ message: $persist('foo').using(sessionStorage) }">
  157. <input x-model="message">
  158. <span x-text="message"></span>
  159. </div>
  160. `],
  161. ({ get, window }, reload) => {
  162. get('span').should(haveText('foo'))
  163. get('input').clear().type('bar')
  164. get('span').should(haveText('bar'))
  165. reload()
  166. get('span').should(haveText('bar'))
  167. window().its('sessionStorage._x_message').should(beEqualTo(JSON.stringify('bar')))
  168. },
  169. )
  170. test('can persist to custom storage using an alias',
  171. [html`
  172. <div x-data="{ message: $persist('foo').as('mymessage').using(sessionStorage) }">
  173. <input x-model="message">
  174. <span x-text="message"></span>
  175. </div>
  176. `],
  177. ({ get, window }, reload) => {
  178. get('span').should(haveText('foo'))
  179. get('input').clear().type('bar')
  180. get('span').should(haveText('bar'))
  181. window().its('sessionStorage.mymessage').should(beEqualTo(JSON.stringify('bar')))
  182. },
  183. )