history.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { haveText, html, test } from '../../utils'
  2. test('can go back and forth',
  3. [html`
  4. <div x-data="{ count: $queryString(1) }">
  5. <button @click="count++">Inc</button>
  6. <span x-text="count"></span>
  7. </div>
  8. `],
  9. ({ get, url, go }) => {
  10. get('span').should(haveText('1'))
  11. url().should('include', '?count=1')
  12. get('button').click()
  13. get('span').should(haveText('2'))
  14. url().should('include', '?count=2')
  15. go('back')
  16. get('span').should(haveText('1'))
  17. url().should('include', '?count=1')
  18. go('forward')
  19. get('span').should(haveText('2'))
  20. url().should('include', '?count=2')
  21. },
  22. )
  23. test('property is set from the query string on load',
  24. [html`
  25. <div x-data="{ count: $queryString(1) }">
  26. <button @click="count++">Inc</button>
  27. <span x-text="count"></span>
  28. </div>
  29. `],
  30. ({ get, url }, reload) => {
  31. get('span').should(haveText('1'))
  32. url().should('include', '?count=1')
  33. get('button').click()
  34. get('span').should(haveText('2'))
  35. url().should('include', '?count=2')
  36. reload()
  37. get('span').should(haveText('2'))
  38. },
  39. )
  40. test('can use a query string key alias',
  41. [html`
  42. <div x-data="{ count: $queryString(1).as('foo') }">
  43. <button @click="count++">Inc</button>
  44. <span x-text="count"></span>
  45. </div>
  46. `],
  47. ({ get, url }, reload) => {
  48. get('span').should(haveText('1'))
  49. url().should('include', '?foo=1')
  50. get('button').click()
  51. get('span').should(haveText('2'))
  52. url().should('include', '?foo=2')
  53. reload()
  54. get('span').should(haveText('2'))
  55. },
  56. )
  57. test('can go back and forth with multiple components',
  58. [html`
  59. <div x-data="{ foo: $queryString(1) }" id="foo">
  60. <button @click="foo++">Inc</button>
  61. <span x-text="foo"></span>
  62. </div>
  63. <div x-data="{ bar: $queryString(1) }" id="bar">
  64. <button @click="bar++">Inc</button>
  65. <span x-text="bar"></span>
  66. </div>
  67. `],
  68. ({ get, url, go }) => {
  69. get('#foo span').should(haveText('1'))
  70. url().should('include', 'foo=1')
  71. get('#foo button').click()
  72. get('#foo span').should(haveText('2'))
  73. url().should('include', 'foo=2')
  74. get('#bar span').should(haveText('1'))
  75. url().should('include', 'bar=1')
  76. get('#bar button').click()
  77. get('#bar span').should(haveText('2'))
  78. url().should('include', 'bar=2')
  79. go('back')
  80. get('#bar span').should(haveText('1'))
  81. url().should('include', 'bar=1')
  82. get('#foo span').should(haveText('2'))
  83. url().should('include', 'foo=2')
  84. go('back')
  85. get('#bar span').should(haveText('1'))
  86. url().should('include', 'bar=1')
  87. get('#foo span').should(haveText('1'))
  88. url().should('include', 'foo=1')
  89. },
  90. )