builder.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {plaid} from "@/builder";
  2. describe('builder', () => {
  3. it('pushState with object will merge into url queries', () => {
  4. const b = plaid().
  5. eventFunc("hello").
  6. query("name", "felix").
  7. mergeQuery(true).
  8. url("/page1?hello=1&page=2#scroll=123_0")
  9. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&hello=1&name=felix&page=2');
  10. const [pushedData, title, url] = b.buildPushStateArgs()
  11. expect(url).toEqual('/page1?hello=1&name=felix&page=2#scroll=123_0');
  12. expect(pushedData).toEqual({ query: { hello: '1', name: 'felix', page: '2' }, url: '/page1?hello=1&name=felix&page=2#scroll=123_0' });
  13. });
  14. it('pushState with string will replace url queries', () => {
  15. const b = plaid().
  16. eventFunc("hello").
  17. query("name", "felix").
  18. url("/page1?hello=1&page=2")
  19. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=felix');
  20. const [pushedData, title, url] = b.buildPushStateArgs()
  21. expect(url).toEqual('/page1?name=felix');
  22. expect(pushedData).toEqual({ query: { name: 'felix' }, url: '/page1?name=felix' });
  23. });
  24. it('pushState with clearMergeQuery will delete provided keys then mergeQuery', () => {
  25. const b = plaid().
  26. url("/page1?active_filter_tab=missing_value&missing_value=1&approved.gte=1642003200&order_by=CreatedAt_ASC").
  27. stringQuery("missing_value=2&channel=2").
  28. query("name", "felix").
  29. clearMergeQuery(["missing_value", "channel", "approved"])
  30. const [pushedData, title, url] = b.buildPushStateArgs()
  31. expect(url).toEqual('/page1?active_filter_tab=missing_value&channel=2&missing_value=2&name=felix&order_by=CreatedAt_ASC');
  32. expect(pushedData).toEqual({ query: { active_filter_tab: 'missing_value', channel: '2', name: "felix", missing_value: '2', order_by: 'CreatedAt_ASC'}, url: '/page1?active_filter_tab=missing_value&channel=2&missing_value=2&name=felix&order_by=CreatedAt_ASC' });
  33. });
  34. it('pushState with stringQuery empty will delete provided keys', () => {
  35. const b = plaid().
  36. url("/page1?active_filter_tab=missing_value&missing_value=1&approved.gte=1642003200&order_by=CreatedAt_ASC").
  37. clearMergeQuery(["missing_value", "channel", "approved"])
  38. const [pushedData, title, url] = b.buildPushStateArgs()
  39. expect(url).toEqual('/page1?active_filter_tab=missing_value&order_by=CreatedAt_ASC');
  40. });
  41. it('add operator will add to current query values', () => {
  42. const b = plaid().
  43. eventFunc("hello").
  44. query("selectedIds", { value: '5', add: true }).
  45. mergeQuery(true).
  46. url("/page1?selectedIds=1,2,3&page=2")
  47. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&page=2&selectedIds=1,2,3,5');
  48. const [pushedData, title, url] = b.buildPushStateArgs()
  49. expect(url).toEqual('/page1?page=2&selectedIds=1,2,3,5');
  50. expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3', '5'] }, url: '/page1?page=2&selectedIds=1,2,3,5' });
  51. });
  52. it('remove operator will add to current query values', () => {
  53. const b = plaid().
  54. eventFunc("hello").
  55. query("selectedIds", { value: '5', remove: true }).
  56. mergeQuery(true).
  57. url("/page1?selectedIds=1,2,3,5&page=2")
  58. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&page=2&selectedIds=1,2,3');
  59. const [pushedData, title, url] = b.buildPushStateArgs()
  60. expect(url).toEqual('/page1?page=2&selectedIds=1,2,3');
  61. expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3'] }, url: '/page1?page=2&selectedIds=1,2,3' });
  62. });
  63. it('array with comma', () => {
  64. const b = plaid().
  65. eventFunc("hello").
  66. query("names", ['Hello, Felix', 'How are you']).
  67. mergeQuery(true).
  68. url("/page1?selectedIds=1,2,3,5&page=2")
  69. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5');
  70. const [pushedData, title, url] = b.buildPushStateArgs()
  71. expect(url).toEqual('/page1?names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5');
  72. expect(pushedData).toEqual({ query: { page: '2', selectedIds: ['1', '2', '3', '5'], names: ['Hello, Felix', 'How are you'] }, url: '/page1?names=Hello%2C%20Felix,How%20are%20you&page=2&selectedIds=1,2,3,5' });
  73. });
  74. it('first time add', () => {
  75. const b = plaid().
  76. eventFunc("hello").
  77. query("name", { value: '1', add: true }).
  78. mergeQuery(true).
  79. url("/page1")
  80. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1');
  81. const [pushedData, title, url] = b.buildPushStateArgs()
  82. expect(url).toEqual('/page1?name=1');
  83. expect(pushedData).toEqual({ query: { name: ['1'] }, url: '/page1?name=1' });
  84. });
  85. it('add operator with value array', () => {
  86. const b = plaid().
  87. eventFunc("hello").
  88. query("name", { value: ['1', '2'], add: true }).
  89. mergeQuery(true).
  90. url("/page1")
  91. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1,2');
  92. const [pushedData, title, url] = b.buildPushStateArgs()
  93. expect(url).toEqual('/page1?name=1,2');
  94. expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
  95. });
  96. it('remove operator with value array', () => {
  97. const b = plaid().
  98. eventFunc("hello").
  99. query("name", { value: ['1', '2', '5', '8'], remove: true }).
  100. mergeQuery(true).
  101. url("/page1?name=1,2,3,4,5,6,7,8,9")
  102. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=3,4,6,7,9');
  103. const [pushedData, title, url] = b.buildPushStateArgs()
  104. expect(url).toEqual('/page1?name=3,4,6,7,9');
  105. expect(pushedData).toEqual({ query: { name: ['3', '4', '6', '7', '9'] }, url: '/page1?name=3,4,6,7,9' });
  106. });
  107. it('remove operator with url and value array', () => {
  108. const b = plaid().
  109. eventFunc("hello").
  110. location({ url: '/page2?name=1,2,3,4,5,6,7,8,9', query: { name: { value: ['1', '2', '5', '8'], remove: true } }, mergeQuery: true }).
  111. url("/page1?name=1,2,3,4,5,6,7,8,9")
  112. expect(b.buildFetchURL()).toEqual('/page2?__execute_event__=hello&name=3,4,6,7,9');
  113. const [pushedData, title, url] = b.buildPushStateArgs()
  114. expect(url).toEqual('/page2?name=3,4,6,7,9');
  115. expect(pushedData).toEqual({ query: { name: ['3', '4', '6', '7', '9'] }, url: '/page2?name=3,4,6,7,9' });
  116. });
  117. it('with url', () => {
  118. const b = plaid().
  119. eventFunc("hello").
  120. location({ url: '/page2?name=2,3' }).
  121. url("/page1?name=1,2")
  122. expect(b.buildFetchURL()).toEqual('/page2?__execute_event__=hello&name=2,3');
  123. const [pushedData, title, url] = b.buildPushStateArgs()
  124. expect(url).toEqual('/page2?name=2,3');
  125. expect(pushedData).toEqual({ query: { name: ['2', '3'] }, url: '/page2?name=2,3' });
  126. });
  127. it('location is not set', () => {
  128. const b = plaid().
  129. eventFunc("hello").
  130. url("/page1?name=1,2")
  131. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=hello&name=1,2');
  132. const [pushedData, title, url] = b.buildPushStateArgs()
  133. expect(url).toEqual('/page1?name=1,2');
  134. expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
  135. });
  136. it('without eventFunc', () => {
  137. const b = plaid().
  138. url("/page1?name=1,2")
  139. expect(b.buildFetchURL()).toEqual('/page1?__execute_event__=__reload__&name=1,2');
  140. const [pushedData, title, url] = b.buildPushStateArgs()
  141. expect(url).toEqual('/page1?name=1,2');
  142. expect(pushedData).toEqual({ query: { name: ['1', '2'] }, url: '/page1?name=1,2' });
  143. });
  144. });