history.spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { haveText, html, test } from '../../utils'
  2. // Skipping these tests because the plugin has been moved to livewire/livewire until it's stablhese tests because the plugin has been moved to livewire/livewire until it's stable...
  3. describe.skip('History tests', function () {
  4. test('value is reflected in query string upon changing',
  5. [html`
  6. <div x-data="{ count: $queryString(1) }">
  7. <button @click="count++">Inc</button>
  8. <h1 @click="count--">Dec</h1>
  9. <span x-text="count"></span>
  10. </div>
  11. `],
  12. ({ get, url, go }) => {
  13. get('span').should(haveText('1'))
  14. url().should('not.include', '?count=1')
  15. get('button').click()
  16. get('span').should(haveText('2'))
  17. url().should('include', '?count=2')
  18. get('button').click()
  19. get('span').should(haveText('3'))
  20. url().should('include', '?count=3')
  21. get('h1').click()
  22. get('h1').click()
  23. get('span').should(haveText('1'))
  24. url().should('not.include', '?count=1')
  25. },
  26. )
  27. test('can configure always making the query string value present',
  28. [html`
  29. <div x-data="{ count: $queryString(1).alwaysShow() }">
  30. <button @click="count++">Inc</button>
  31. <h1 @click="count--">Dec</h1>
  32. <span x-text="count"></span>
  33. </div>
  34. `],
  35. ({ get, url, go }) => {
  36. get('span').should(haveText('1'))
  37. url().should('include', '?count=1')
  38. get('button').click()
  39. get('span').should(haveText('2'))
  40. url().should('include', '?count=2')
  41. get('h1').click()
  42. get('span').should(haveText('1'))
  43. url().should('include', '?count=1')
  44. },
  45. )
  46. test('value is persisted across requests',
  47. [html`
  48. <div x-data="{ count: $queryString(1) }">
  49. <button @click="count++">Inc</button>
  50. <span x-text="count"></span>
  51. </div>
  52. `],
  53. ({ get, url, go }, reload) => {
  54. get('span').should(haveText('1'))
  55. url().should('not.include', '?count=1')
  56. get('button').click()
  57. get('span').should(haveText('2'))
  58. url().should('include', '?count=2')
  59. reload()
  60. url().should('include', '?count=2')
  61. get('span').should(haveText('2'))
  62. },
  63. )
  64. test('can provide an alias',
  65. [html`
  66. <div x-data="{ count: $queryString(1).as('tnuoc') }">
  67. <button @click="count++">Inc</button>
  68. <span x-text="count"></span>
  69. </div>
  70. `],
  71. ({ get, url, go }) => {
  72. get('span').should(haveText('1'))
  73. url().should('not.include', '?tnuoc=1')
  74. get('button').click()
  75. get('span').should(haveText('2'))
  76. url().should('include', '?tnuoc=2')
  77. },
  78. )
  79. test('can use pushState',
  80. [html`
  81. <div x-data="{ count: $queryString(1).usePush() }">
  82. <button @click="count++">Inc</button>
  83. <span x-text="count"></span>
  84. </div>
  85. `],
  86. ({ get, url, go }) => {
  87. get('span').should(haveText('1'))
  88. url().should('not.include', '?count=1')
  89. get('button').click()
  90. get('span').should(haveText('2'))
  91. url().should('include', '?count=2')
  92. go('back')
  93. get('span').should(haveText('1'))
  94. url().should('not.include', '?count=1')
  95. go('forward')
  96. get('span').should(haveText('2'))
  97. url().should('include', '?count=2')
  98. },
  99. )
  100. test('can go back and forth with multiple components',
  101. [html`
  102. <div x-data="{ foo: $queryString(1).usePush() }" id="foo">
  103. <button @click="foo++">Inc</button>
  104. <span x-text="foo"></span>
  105. </div>
  106. <div x-data="{ bar: $queryString(1).usePush() }" id="bar">
  107. <button @click="bar++">Inc</button>
  108. <span x-text="bar"></span>
  109. </div>
  110. `],
  111. ({ get, url, go }) => {
  112. get('#foo span').should(haveText('1'))
  113. url().should('not.include', 'foo=1')
  114. get('#foo button').click()
  115. get('#foo span').should(haveText('2'))
  116. url().should('include', 'foo=2')
  117. get('#bar span').should(haveText('1'))
  118. url().should('not.include', 'bar=1')
  119. get('#bar button').click()
  120. get('#bar span').should(haveText('2'))
  121. url().should('include', 'bar=2')
  122. go('back')
  123. get('#bar span').should(haveText('1'))
  124. url().should('not.include', 'bar=1')
  125. get('#foo span').should(haveText('2'))
  126. url().should('include', 'foo=2')
  127. go('back')
  128. get('#bar span').should(haveText('1'))
  129. url().should('not.include', 'bar=1')
  130. get('#foo span').should(haveText('1'))
  131. url().should('not.include', 'foo=1')
  132. },
  133. )
  134. test('supports arrays',
  135. [html`
  136. <div x-data="{ items: $queryString(['foo']) }">
  137. <button @click="items.push('bar')">Inc</button>
  138. <span x-text="JSON.stringify(items)"></span>
  139. </div>
  140. `],
  141. ({ get, url, go }, reload) => {
  142. get('span').should(haveText('["foo"]'))
  143. url().should('not.include', '?items')
  144. get('button').click()
  145. get('span').should(haveText('["foo","bar"]'))
  146. url().should('include', '?items[0]=foo&items[1]=bar')
  147. reload()
  148. url().should('include', '?items[0]=foo&items[1]=bar')
  149. get('span').should(haveText('["foo","bar"]'))
  150. },
  151. )
  152. test('supports deep arrays',
  153. [html`
  154. <div x-data="{ items: $queryString(['foo', ['bar', 'baz']]) }">
  155. <button @click="items[1].push('bob')">Inc</button>
  156. <span x-text="JSON.stringify(items)"></span>
  157. </div>
  158. `],
  159. ({ get, url, go }, reload) => {
  160. get('span').should(haveText('["foo",["bar","baz"]]'))
  161. url().should('not.include', '?items')
  162. get('button').click()
  163. get('span').should(haveText('["foo",["bar","baz","bob"]]'))
  164. url().should('include', '?items[0]=foo&items[1][0]=bar&items[1][1]=baz&items[1][2]=bob')
  165. reload()
  166. url().should('include', '?items[0]=foo&items[1][0]=bar&items[1][1]=baz&items[1][2]=bob')
  167. get('span').should(haveText('["foo",["bar","baz","bob"]]'))
  168. },
  169. )
  170. test('supports objects',
  171. [html`
  172. <div x-data="{ items: $queryString({ foo: 'bar' }) }">
  173. <button @click="items.bob = 'lob'">Inc</button>
  174. <span x-text="JSON.stringify(items)"></span>
  175. </div>
  176. `],
  177. ({ get, url, go }, reload) => {
  178. get('span').should(haveText('{"foo":"bar"}'))
  179. url().should('not.include', '?items')
  180. get('button').click()
  181. get('span').should(haveText('{"foo":"bar","bob":"lob"}'))
  182. url().should('include', '?items[foo]=bar&items[bob]=lob')
  183. reload()
  184. url().should('include', '?items[foo]=bar&items[bob]=lob')
  185. get('span').should(haveText('{"foo":"bar","bob":"lob"}'))
  186. },
  187. )
  188. test('encodes values according to RFC 1738 (plus signs for spaces)',
  189. [html`
  190. <div x-data="{ foo: $queryString('hey&there').alwaysShow(), bar: $queryString('hey there').alwaysShow() }">
  191. <span x-text="JSON.stringify(foo)+JSON.stringify(bar)"></span>
  192. </div>
  193. `],
  194. ({ get, url, go }, reload) => {
  195. url().should('include', '?foo=hey%26there&bar=hey+there')
  196. get('span').should(haveText('"hey&there""hey there"'))
  197. reload()
  198. url().should('include', '?foo=hey%26there&bar=hey+there')
  199. get('span').should(haveText('"hey&there""hey there"'))
  200. },
  201. )
  202. })