sort.spec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ({ 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. // Unfortunately, github actions doesn't like "async/await" here
  17. // so we need to use .then() throughout this entire test...
  18. get('#1').drag('#3').then(() => {
  19. get('ul li').eq(0).should(haveText('bar'))
  20. get('ul li').eq(1).should(haveText('baz'))
  21. get('ul li').eq(2).should(haveText('foo'))
  22. get('#3').drag('#1').then(() => {
  23. get('ul li').eq(0).should(haveText('bar'))
  24. get('ul li').eq(1).should(haveText('foo'))
  25. get('ul li').eq(2).should(haveText('baz'))
  26. })
  27. })
  28. },
  29. )
  30. test('can use a custom handle',
  31. [html`
  32. <div x-data>
  33. <ul x-sort>
  34. <li id="1"><span x-sort:handle>handle</span> - foo</li>
  35. <li id="2"><span x-sort:handle>handle</span> - bar</li>
  36. </ul>
  37. </div>
  38. `],
  39. ({ get }) => {
  40. get('ul li').eq(0).should(haveText('handle - foo'))
  41. get('ul li').eq(1).should(haveText('handle - bar'))
  42. get('#1 span').drag('#2').then(() => {
  43. get('ul li').eq(0).should(haveText('handle - bar'))
  44. get('ul li').eq(1).should(haveText('handle - foo'))
  45. })
  46. },
  47. )
  48. // Skipping this because it passes locally but not in CI...
  49. test.skip('can move items between groups',
  50. [html`
  51. <div x-data>
  52. <ul x-sort.group.one>
  53. <li id="1">foo</li>
  54. <li id="2">bar</li>
  55. </ul>
  56. <ol x-sort.group.one>
  57. <li id="3">oof</li>
  58. <li id="4">rab</li>
  59. </ol>
  60. </div>
  61. `],
  62. ({ get }) => {
  63. get('ul li').eq(0).should(haveText('foo'))
  64. get('ul li').eq(1).should(haveText('bar'))
  65. get('ol li').eq(0).should(haveText('oof'))
  66. get('ol li').eq(1).should(haveText('rab'))
  67. get('#1').drag('#4').then(() => {
  68. get('ul li').eq(0).should(haveText('bar'))
  69. get('ol li').eq(0).should(haveText('oof'))
  70. get('ol li').eq(1).should(haveText('foo'))
  71. get('ol li').eq(2).should(haveText('rab'))
  72. })
  73. },
  74. )
  75. test('sort handle method',
  76. [html`
  77. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  78. <ul x-sort="handle">
  79. <li x-sort:key="1" id="1">foo</li>
  80. <li x-sort:key="2" id="2">bar</li>
  81. <li x-sort:key="3" id="3">baz</li>
  82. </ul>
  83. <h1 x-ref="outlet"></h1>
  84. </div>
  85. `],
  86. ({ get }) => {
  87. get('#1').drag('#3').then(() => {
  88. get('h1').should(haveText('1-2'))
  89. get('#3').drag('#1').then(() => {
  90. get('h1').should(haveText('3-2'))
  91. })
  92. })
  93. },
  94. )
  95. test('can access key and position in handler',
  96. [html`
  97. <div x-data="{ handle(key, position) { $refs.outlet.textContent = key+'-'+position } }">
  98. <ul x-sort="handle($position, $key)">
  99. <li x-sort:key="1" id="1">foo</li>
  100. <li x-sort:key="2" id="2">bar</li>
  101. <li x-sort:key="3" id="3">baz</li>
  102. </ul>
  103. <h1 x-ref="outlet"></h1>
  104. </div>
  105. `],
  106. ({ get }) => {
  107. get('#1').drag('#3').then(() => {
  108. get('h1').should(haveText('2-1'))
  109. get('#3').drag('#1').then(() => {
  110. get('h1').should(haveText('2-3'))
  111. })
  112. })
  113. },
  114. )
  115. test('can use custom sortablejs configuration',
  116. [html`
  117. <div x-data>
  118. <ul x-sort x-sort:config="{ filter: '[data-ignore]' }">
  119. <li id="1" data-ignore>foo</li>
  120. <li id="2">bar</li>
  121. <li id="3">baz</li>
  122. </ul>
  123. </div>
  124. `],
  125. ({ get }) => {
  126. get('ul li').eq(0).should(haveText('foo'))
  127. get('ul li').eq(1).should(haveText('bar'))
  128. get('ul li').eq(2).should(haveText('baz'))
  129. get('#1').drag('#3').then(() => {
  130. get('ul li').eq(0).should(haveText('foo'))
  131. get('ul li').eq(1).should(haveText('bar'))
  132. get('ul li').eq(2).should(haveText('baz'))
  133. get('#3').drag('#1').then(() => {
  134. get('ul li').eq(0).should(haveText('baz'))
  135. get('ul li').eq(1).should(haveText('foo'))
  136. get('ul li').eq(2).should(haveText('bar'))
  137. })
  138. })
  139. },
  140. )
  141. test('works with Livewire morphing',
  142. [html`
  143. <div x-data>
  144. <ul x-sort>
  145. <!-- [if BLOCK]><![endif] -->
  146. <li id="1">foo</li>
  147. <li id="2">bar</li>
  148. <li id="3">baz</li>
  149. <!-- [if ENDBLOCK]><![endif] -->
  150. </ul>
  151. </div>
  152. `],
  153. ({ get }) => {
  154. get('#1').drag('#3').then(() => {
  155. // This is the easiest way I can think of to assert the order of HTML comments doesn't change...
  156. 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] -->`)
  157. })
  158. },
  159. )