sort.spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { haveText, html, test } from '../../utils'
  2. // Skipping this because it passes locally but not in CI...
  3. test.skip('basic drag sorting works',
  4. [html`
  5. <div x-data>
  6. <ul x-sort>
  7. <li id="1">foo</li>
  8. <li id="2">bar</li>
  9. <li id="3">baz</li>
  10. </ul>
  11. </div>
  12. `],
  13. ({ get }) => {
  14. get('ul li').eq(0).should(haveText('foo'))
  15. get('ul li').eq(1).should(haveText('bar'))
  16. get('ul li').eq(2).should(haveText('baz'))
  17. // Unfortunately, github actions doesn't like "async/await" here
  18. // so we need to use .then() throughout this entire test...
  19. get('#1').drag('#3').then(() => {
  20. get('ul li').eq(0).should(haveText('bar'))
  21. get('ul li').eq(1).should(haveText('baz'))
  22. get('ul li').eq(2).should(haveText('foo'))
  23. get('#3').drag('#1').then(() => {
  24. get('ul li').eq(0).should(haveText('bar'))
  25. get('ul li').eq(1).should(haveText('foo'))
  26. get('ul li').eq(2).should(haveText('baz'))
  27. })
  28. })
  29. },
  30. )
  31. test('can use a custom handle',
  32. [html`
  33. <div x-data>
  34. <ul x-sort>
  35. <li id="1"><span x-sort:handle>handle</span> - foo</li>
  36. <li id="2"><span x-sort:handle>handle</span> - bar</li>
  37. </ul>
  38. </div>
  39. `],
  40. ({ get }) => {
  41. get('ul li').eq(0).should(haveText('handle - foo'))
  42. get('ul li').eq(1).should(haveText('handle - bar'))
  43. get('#1 span').drag('#2').then(() => {
  44. get('ul li').eq(0).should(haveText('handle - bar'))
  45. get('ul li').eq(1).should(haveText('handle - foo'))
  46. })
  47. },
  48. )
  49. // Skipping this because it passes locally but not in CI...
  50. test.skip('can move items between groups',
  51. [html`
  52. <div x-data>
  53. <ul x-sort x-sort:group="one">
  54. <li id="1">foo</li>
  55. <li id="2">bar</li>
  56. </ul>
  57. <ol x-sort x-sort:group="one">
  58. <li id="3">oof</li>
  59. <li id="4">rab</li>
  60. </ol>
  61. </div>
  62. `],
  63. ({ get }) => {
  64. get('ul li').eq(0).should(haveText('foo'))
  65. get('ul li').eq(1).should(haveText('bar'))
  66. get('ol li').eq(0).should(haveText('oof'))
  67. get('ol li').eq(1).should(haveText('rab'))
  68. get('#1').drag('#4').then(() => {
  69. get('ul li').eq(0).should(haveText('bar'))
  70. get('ol li').eq(0).should(haveText('oof'))
  71. get('ol li').eq(1).should(haveText('foo'))
  72. get('ol li').eq(2).should(haveText('rab'))
  73. })
  74. },
  75. )
  76. test('sort handle method',
  77. [html`
  78. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  79. <ul x-sort="handle">
  80. <li x-sort:key="1" id="1">foo</li>
  81. <li x-sort:key="2" id="2">bar</li>
  82. <li x-sort:key="3" id="3">baz</li>
  83. </ul>
  84. <h1 x-ref="outlet"></h1>
  85. </div>
  86. `],
  87. ({ get }) => {
  88. get('#1').drag('#3').then(() => {
  89. get('h1').should(haveText('1-2'))
  90. get('#3').drag('#1').then(() => {
  91. get('h1').should(haveText('3-2'))
  92. })
  93. })
  94. },
  95. )
  96. test('item is also supported for the key in the sort handle method',
  97. [html`
  98. <div x-data="{ handle(item, position) { $refs.outlet.textContent = item+'-'+position } }">
  99. <ul x-sort="handle">
  100. <li x-sort:item="1" id="1">foo</li>
  101. <li x-sort:item="2" id="2">bar</li>
  102. <li x-sort:item="3" id="3">baz</li>
  103. </ul>
  104. <h1 x-ref="outlet"></h1>
  105. </div>
  106. `],
  107. ({ get }) => {
  108. get('#1').drag('#3').then(() => {
  109. get('h1').should(haveText('1-2'))
  110. get('#3').drag('#1').then(() => {
  111. get('h1').should(haveText('3-2'))
  112. })
  113. })
  114. },
  115. )
  116. test('can access key and position in handler',
  117. [html`
  118. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  119. <ul x-sort="handle($position, $key)">
  120. <li x-sort:key="1" id="1">foo</li>
  121. <li x-sort:key="2" id="2">bar</li>
  122. <li x-sort:key="3" id="3">baz</li>
  123. </ul>
  124. <h1 x-ref="outlet"></h1>
  125. </div>
  126. `],
  127. ({ get }) => {
  128. get('#1').drag('#3').then(() => {
  129. get('h1').should(haveText('2-1'))
  130. get('#3').drag('#1').then(() => {
  131. get('h1').should(haveText('2-3'))
  132. })
  133. })
  134. },
  135. )
  136. test('can access $item instead of $key',
  137. [html`
  138. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  139. <ul x-sort="handle($position, $item)">
  140. <li x-sort:key="1" id="1">foo</li>
  141. <li x-sort:key="2" id="2">bar</li>
  142. <li x-sort:key="3" id="3">baz</li>
  143. </ul>
  144. <h1 x-ref="outlet"></h1>
  145. </div>
  146. `],
  147. ({ get }) => {
  148. get('#1').drag('#3').then(() => {
  149. get('h1').should(haveText('2-1'))
  150. get('#3').drag('#1').then(() => {
  151. get('h1').should(haveText('2-3'))
  152. })
  153. })
  154. },
  155. )
  156. test('can use custom sortablejs configuration',
  157. [html`
  158. <div x-data>
  159. <ul x-sort x-sort:config="{ filter: '[data-ignore]' }">
  160. <li id="1" data-ignore>foo</li>
  161. <li id="2">bar</li>
  162. <li id="3">baz</li>
  163. </ul>
  164. </div>
  165. `],
  166. ({ get }) => {
  167. get('ul li').eq(0).should(haveText('foo'))
  168. get('ul li').eq(1).should(haveText('bar'))
  169. get('ul li').eq(2).should(haveText('baz'))
  170. get('#1').drag('#3').then(() => {
  171. get('ul li').eq(0).should(haveText('foo'))
  172. get('ul li').eq(1).should(haveText('bar'))
  173. get('ul li').eq(2).should(haveText('baz'))
  174. get('#3').drag('#1').then(() => {
  175. get('ul li').eq(0).should(haveText('baz'))
  176. get('ul li').eq(1).should(haveText('foo'))
  177. get('ul li').eq(2).should(haveText('bar'))
  178. })
  179. })
  180. },
  181. )
  182. test('works with Livewire morphing',
  183. [html`
  184. <div x-data>
  185. <ul x-sort>
  186. <!-- [if BLOCK]><![endif] -->
  187. <li id="1">foo</li>
  188. <li id="2">bar</li>
  189. <li id="3">baz</li>
  190. <!-- [if ENDBLOCK]><![endif] -->
  191. </ul>
  192. </div>
  193. `],
  194. ({ get }) => {
  195. get('#1').drag('#3').then(() => {
  196. // This is the easiest way I can think of to assert the order of HTML comments doesn't change...
  197. get('ul').should('have.html', `\n <!-- [if BLOCK]><![endif] -->\n \n <li id="2" style="">bar</li>\n <li id="3" style="">baz</li>\n \n <li id="1" draggable="false" class="" style="opacity: 1;">foo</li><!-- [if ENDBLOCK]><![endif] -->`)
  198. })
  199. },
  200. )
  201. test('x-sort:item can be used as a filter',
  202. [html`
  203. <div x-data>
  204. <ul x-sort>
  205. <li x-sort:item id="1">foo</li>
  206. <li id="2">bar</li>
  207. <li x-sort:item id="3">baz</li>
  208. </ul>
  209. </div>
  210. `],
  211. ({ get }) => {
  212. get('ul li').eq(0).should(haveText('foo'))
  213. get('ul li').eq(1).should(haveText('bar'))
  214. get('ul li').eq(2).should(haveText('baz'))
  215. // Unfortunately, github actions doesn't like "async/await" here
  216. // so we need to use .then() throughout this entire test...
  217. get('#1').drag('#3').then(() => {
  218. get('ul li').eq(0).should(haveText('bar'))
  219. get('ul li').eq(1).should(haveText('baz'))
  220. get('ul li').eq(2).should(haveText('foo'))
  221. get('#2').drag('#1').then(() => {
  222. get('ul li').eq(0).should(haveText('bar'))
  223. get('ul li').eq(1).should(haveText('baz'))
  224. get('ul li').eq(2).should(haveText('foo'))
  225. })
  226. })
  227. },
  228. )