sort.spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { haveText, html, test } from '../../utils'
  2. test('basic drag sorting works',
  3. [html`
  4. <div x-data>
  5. <ul x-sort>
  6. <li id="1">foo</li>
  7. <li id="2">bar</li>
  8. <li id="3">baz</li>
  9. </ul>
  10. </div>
  11. `],
  12. async ({ get }) => {
  13. get('ul li').eq(0).should(haveText('foo'))
  14. get('ul li').eq(1).should(haveText('bar'))
  15. get('ul li').eq(2).should(haveText('baz'))
  16. await get('#1').drag('#3')
  17. get('ul li').eq(0).should(haveText('bar'))
  18. get('ul li').eq(1).should(haveText('baz'))
  19. get('ul li').eq(2).should(haveText('foo'))
  20. await get('#3').drag('#1')
  21. get('ul li').eq(0).should(haveText('bar'))
  22. get('ul li').eq(1).should(haveText('foo'))
  23. get('ul li').eq(2).should(haveText('baz'))
  24. },
  25. )
  26. test('can use a custom handle',
  27. [html`
  28. <div x-data>
  29. <ul x-sort>
  30. <li id="1"><span x-sort:handle>handle</span> - foo</li>
  31. <li id="2"><span x-sort:handle>handle</span> - bar</li>
  32. </ul>
  33. </div>
  34. `],
  35. async ({ get }) => {
  36. get('ul li').eq(0).should(haveText('handle - foo'))
  37. get('ul li').eq(1).should(haveText('handle - bar'))
  38. await get('#1 span').drag('#2')
  39. get('ul li').eq(0).should(haveText('handle - bar'))
  40. get('ul li').eq(1).should(haveText('handle - foo'))
  41. },
  42. )
  43. test('can move items between groups',
  44. [html`
  45. <div x-data>
  46. <ul x-sort.group.one>
  47. <li id="1">foo</li>
  48. <li id="2">bar</li>
  49. </ul>
  50. <ol x-sort.group.one>
  51. <li id="3">oof</li>
  52. <li id="4">rab</li>
  53. </ol>
  54. </div>
  55. `],
  56. async ({ get }) => {
  57. get('ul li').eq(0).should(haveText('foo'))
  58. get('ul li').eq(1).should(haveText('bar'))
  59. get('ol li').eq(0).should(haveText('oof'))
  60. get('ol li').eq(1).should(haveText('rab'))
  61. await get('#1').drag('#4')
  62. get('ul li').eq(0).should(haveText('bar'))
  63. get('ol li').eq(0).should(haveText('oof'))
  64. get('ol li').eq(1).should(haveText('foo'))
  65. get('ol li').eq(2).should(haveText('rab'))
  66. },
  67. )
  68. test('sort handle method',
  69. [html`
  70. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  71. <ul x-sort="handle">
  72. <li x-sort:key="1" id="1">foo</li>
  73. <li x-sort:key="2" id="2">bar</li>
  74. <li x-sort:key="3" id="3">baz</li>
  75. </ul>
  76. <h1 x-ref="outlet"></h1>
  77. </div>
  78. `],
  79. async ({ get }) => {
  80. await get('#1').drag('#3')
  81. get('h1').should(haveText('1-2'))
  82. await get('#3').drag('#1')
  83. get('h1').should(haveText('3-2'))
  84. },
  85. )
  86. test('can access key and position in handler',
  87. [html`
  88. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  89. <ul x-sort="handle($position, $key)">
  90. <li x-sort:key="1" id="1">foo</li>
  91. <li x-sort:key="2" id="2">bar</li>
  92. <li x-sort:key="3" id="3">baz</li>
  93. </ul>
  94. <h1 x-ref="outlet"></h1>
  95. </div>
  96. `],
  97. async ({ get }) => {
  98. await get('#1').drag('#3')
  99. get('h1').should(haveText('2-1'))
  100. await get('#3').drag('#1')
  101. get('h1').should(haveText('2-3'))
  102. },
  103. )
  104. test('can use custom sortablejs configuration',
  105. [html`
  106. <div x-data>
  107. <ul x-sort x-sort:config="{ filter: '[data-ignore]' }">
  108. <li id="1" data-ignore>foo</li>
  109. <li id="2">bar</li>
  110. <li id="3">baz</li>
  111. </ul>
  112. </div>
  113. `],
  114. async ({ get }) => {
  115. get('ul li').eq(0).should(haveText('foo'))
  116. get('ul li').eq(1).should(haveText('bar'))
  117. get('ul li').eq(2).should(haveText('baz'))
  118. await get('#1').drag('#3')
  119. get('ul li').eq(0).should(haveText('foo'))
  120. get('ul li').eq(1).should(haveText('bar'))
  121. get('ul li').eq(2).should(haveText('baz'))
  122. await get('#3').drag('#1')
  123. get('ul li').eq(0).should(haveText('baz'))
  124. get('ul li').eq(1).should(haveText('foo'))
  125. get('ul li').eq(2).should(haveText('bar'))
  126. },
  127. )
  128. test('works with Livewire morphing',
  129. [html`
  130. <div x-data>
  131. <ul x-sort>
  132. <!-- [if BLOCK]><![endif] -->
  133. <li id="1">foo</li>
  134. <li id="2">bar</li>
  135. <li id="3">baz</li>
  136. <!-- [if ENDBLOCK]><![endif] -->
  137. </ul>
  138. </div>
  139. `],
  140. async ({ get }) => {
  141. await get('#1').drag('#3')
  142. // This is the easiest way I can think of to assert the order of HTML comments doesn't change...
  143. get('ul').should('have.html', `\n <!-- [if BLOCK]><![endif] -->\n \n <li x-sort:item="" id="2" style="">bar</li>\n <li x-sort:item="" id="3" style="">baz</li>\n \n <li x-sort:item="" id="1" draggable="false" class="" style="opacity: 1;">foo</li><!-- [if ENDBLOCK]><![endif] -->`)
  144. },
  145. )