persist.spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. window().then((win) => {
  169. win.sessionStorage.clear()
  170. });
  171. },
  172. )
  173. test('can persist to custom storage using an alias',
  174. [html`
  175. <div x-data="{ message: $persist('foo').as('mymessage').using(sessionStorage) }">
  176. <input x-model="message">
  177. <span x-text="message"></span>
  178. </div>
  179. `],
  180. ({ get, window }, reload) => {
  181. get('span').should(haveText('foo'))
  182. get('input').clear().type('bar')
  183. get('span').should(haveText('bar'))
  184. window().its('sessionStorage.mymessage').should(beEqualTo(JSON.stringify('bar')))
  185. window().then((win) => {
  186. win.sessionStorage.clear()
  187. });
  188. },
  189. )
  190. test('can persist using global Alpine.$persist within Alpine.store',
  191. [html`
  192. <div x-data>
  193. <input x-model="$store.name.firstName">
  194. <span x-text="$store.name.firstName"></span>
  195. </div>
  196. `, `
  197. Alpine.store('name', {
  198. firstName: Alpine.$persist('Daniel').as('dev-name')
  199. })
  200. `],
  201. ({ get, window }, reload) => {
  202. get('span').should(haveText('Daniel'))
  203. get('input').clear().type('Malcolm')
  204. get('span').should(haveText('Malcolm'))
  205. reload()
  206. get('span').should(haveText('Malcolm'))
  207. },
  208. )
  209. test('multiple aliases work when using global Alpine.$persist',
  210. [html`
  211. <div x-data>
  212. <input x-model="$store.name.firstName">
  213. <span x-text="$store.name.firstName"></span>
  214. <p x-text="$store.name.lastName"></p>
  215. </div>
  216. `, `
  217. Alpine.store('name', {
  218. firstName: Alpine.$persist('John').as('first-name'),
  219. lastName: Alpine.$persist('Doe').as('name-name')
  220. })
  221. `],
  222. ({ get, window }, reload) => {
  223. get('span').should(haveText('John'))
  224. get('p').should(haveText('Doe'))
  225. get('input').clear().type('Joe')
  226. get('span').should(haveText('Joe'))
  227. get('p').should(haveText('Doe'))
  228. reload()
  229. get('span').should(haveText('Joe'))
  230. get('p').should(haveText('Doe'))
  231. },
  232. )