x-for.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import { beVisible, haveLength, haveText, html, notBeVisible, test } from '../../utils'
  2. test('renders loops with x-for',
  3. html`
  4. <div x-data="{ items: ['foo'] }">
  5. <button x-on:click="items = ['foo', 'bar']">click me</button>
  6. <template x-for="item in items">
  7. <span x-text="item"></span>
  8. </template>
  9. </div>
  10. `,
  11. ({ get }) => {
  12. get('span:nth-of-type(1)').should(haveText('foo'))
  13. get('span:nth-of-type(2)').should(notBeVisible())
  14. get('button').click()
  15. get('span:nth-of-type(1)').should(haveText('foo'))
  16. get('span:nth-of-type(2)').should(haveText('bar'))
  17. }
  18. )
  19. test('can destructure arrays',
  20. html`
  21. <div x-data="{ items: [[1, 'foo'], [2, 'bar']] }">
  22. <template x-for="[id, label] in items">
  23. <div x-bind:id="id">
  24. <span x-text="id"></span>
  25. <h1 x-text="label"></h1>
  26. </div>
  27. </template>
  28. </div>
  29. `,
  30. ({ get }) => {
  31. get('#1 span').should(haveText('1'))
  32. get('#1 h1').should(haveText('foo'))
  33. get('#2 span').should(haveText('2'))
  34. get('#2 h1').should(haveText('bar'))
  35. }
  36. )
  37. test('removes all elements when array is empty and previously had one item',
  38. html`
  39. <div x-data="{ items: ['foo'] }">
  40. <button x-on:click="items = []">click me</button>
  41. <template x-for="item in items">
  42. <span x-text="item"></span>
  43. </template>
  44. </div>
  45. `,
  46. ({ get }) => {
  47. get('span').should(beVisible())
  48. get('button').click()
  49. get('span').should(notBeVisible())
  50. }
  51. )
  52. test('removes all elements when array is empty and previously had multiple items',
  53. html`
  54. <div x-data="{ items: ['foo', 'bar', 'world'] }">
  55. <button x-on:click="items = []">click me</button>
  56. <template x-for="item in items">
  57. <span x-text="item"></span>
  58. </template>
  59. </div>
  60. `,
  61. ({ get }) => {
  62. get('span:nth-of-type(1)').should(beVisible())
  63. get('span:nth-of-type(2)').should(beVisible())
  64. get('span:nth-of-type(3)').should(beVisible())
  65. get('button').click()
  66. get('span:nth-of-type(1)').should(notBeVisible())
  67. get('span:nth-of-type(2)').should(notBeVisible())
  68. get('span:nth-of-type(3)').should(notBeVisible())
  69. }
  70. )
  71. test('elements inside of loop are reactive',
  72. html`
  73. <div x-data="{ items: ['first'], foo: 'bar' }">
  74. <button x-on:click="foo = 'baz'">click me</button>
  75. <template x-for="item in items">
  76. <span>
  77. <h1 x-text="item"></h1>
  78. <h2 x-text="foo"></h2>
  79. </span>
  80. </template>
  81. </div>
  82. `,
  83. ({ get }) => {
  84. get('span').should(beVisible())
  85. get('h1').should(haveText('first'))
  86. get('h2').should(haveText('bar'))
  87. get('button').click()
  88. get('span').should(beVisible())
  89. get('h1').should(haveText('first'))
  90. get('h2').should(haveText('baz'))
  91. }
  92. )
  93. test('components inside of loop are reactive',
  94. html`
  95. <div x-data="{ items: ['first'] }">
  96. <template x-for="item in items">
  97. <div x-data="{foo: 'bar'}" class="child">
  98. <span x-text="foo"></span>
  99. <button x-on:click="foo = 'bob'">click me</button>
  100. </div>
  101. </template>
  102. </div>
  103. `,
  104. ({ get }) => {
  105. get('span').should(haveText('bar'))
  106. get('button').click()
  107. get('span').should(haveText('bob'))
  108. }
  109. )
  110. test('components inside a plain element of loop are reactive',
  111. html`
  112. <div x-data="{ items: ['first'] }">
  113. <template x-for="item in items">
  114. <ul>
  115. <div x-data="{foo: 'bar'}" class="child">
  116. <span x-text="foo"></span>
  117. <button x-on:click="foo = 'bob'">click me</button>
  118. </div>
  119. </ul>
  120. </template>
  121. </div>
  122. `,
  123. ({ get }) => {
  124. get('span').should(haveText('bar'))
  125. get('button').click()
  126. get('span').should(haveText('bob'))
  127. }
  128. )
  129. test('adding key attribute moves dom nodes properly',
  130. html`
  131. <div x-data="{ items: ['foo', 'bar'] }">
  132. <button x-on:click="items = ['bob', 'bar', 'foo', 'baz']" id="reorder">click me</button>
  133. <button x-on:click="$el.parentElement.querySelectorAll('span').forEach((el, index) => el.og_loop_index = index)" id="assign">click me</button>
  134. <template x-for="item in items" :key="item">
  135. <span x-text="item"></span>
  136. </template>
  137. </div>
  138. `,
  139. ({ get }) => {
  140. let haveOgIndex = index => el => expect(el[0].og_loop_index).to.equal(index)
  141. get('#assign').click()
  142. get('span:nth-of-type(1)').should(haveOgIndex(0))
  143. get('span:nth-of-type(2)').should(haveOgIndex(1))
  144. get('#reorder').click()
  145. get('span:nth-of-type(1)').should(haveOgIndex(undefined))
  146. get('span:nth-of-type(2)').should(haveOgIndex(1))
  147. get('span:nth-of-type(3)').should(haveOgIndex(0))
  148. get('span:nth-of-type(4)').should(haveOgIndex(undefined))
  149. }
  150. )
  151. test('can key by index',
  152. html`
  153. <div x-data="{ items: ['foo', 'bar'] }">
  154. <button x-on:click="items = ['bar', 'foo', 'baz']" id="reorder">click me</button>
  155. <button x-on:click="$el.parentElement.querySelectorAll('span').forEach((el, index) => el.og_loop_index = index)" id="assign">click me</button>
  156. <template x-for="(item, index) in items" :key="index">
  157. <span x-text="item"></span>
  158. </template>
  159. </div>
  160. `,
  161. ({ get }) => {
  162. let haveOgIndex = index => el => expect(el[0].og_loop_index).to.equal(index)
  163. get('#assign').click()
  164. get('span:nth-of-type(1)').should(haveOgIndex(0))
  165. get('span:nth-of-type(2)').should(haveOgIndex(1))
  166. get('#reorder').click()
  167. get('span:nth-of-type(1)').should(haveOgIndex(0))
  168. get('span:nth-of-type(2)').should(haveOgIndex(1))
  169. get('span:nth-of-type(3)').should(haveOgIndex(undefined))
  170. }
  171. )
  172. test('can use index inside of loop',
  173. html`
  174. <div x-data="{ items: ['foo'] }">
  175. <template x-for="(item, index) in items">
  176. <div>
  177. <h1 x-text="items.indexOf(item)"></h1>
  178. <h2 x-text="index"></h2>
  179. </div>
  180. </template>
  181. </div>
  182. `,
  183. ({ get }) => {
  184. get('h1').should(haveText(0))
  185. get('h2').should(haveText(0))
  186. }
  187. )
  188. test('can use third iterator param (collection) inside of loop',
  189. html`
  190. <div x-data="{ items: ['foo'] }">
  191. <template x-for="(item, index, things) in items">
  192. <div>
  193. <h1 x-text="items"></h1>
  194. <h2 x-text="things"></h2>
  195. </div>
  196. </template>
  197. </div>
  198. `,
  199. ({ get }) => {
  200. get('h1').should(haveText('foo'))
  201. get('h2').should(haveText('foo'))
  202. }
  203. )
  204. test('listeners in loop get fresh iteration data even though they are only registered initially',
  205. html`
  206. <div x-data="{ items: ['foo'], output: '' }">
  207. <button x-on:click="items = ['bar']">click me</button>
  208. <template x-for="(item, index) in items">
  209. <span x-text="item" x-on:click="output = item"></span>
  210. </template>
  211. <h1 x-text="output"></h1>
  212. </div>
  213. `,
  214. ({ get }) => {
  215. get('h1').should(haveText(''))
  216. get('span').click()
  217. get('h1').should(haveText('foo'))
  218. get('button').click()
  219. get('span').click()
  220. get('h1').should(haveText('bar'))
  221. }
  222. )
  223. test('nested x-for',
  224. html`
  225. <div x-data="{ foos: [ {bars: ['bob', 'lob']} ] }">
  226. <button x-on:click="foos = [ {bars: ['bob', 'lob']}, {bars: ['law']} ]">click me</button>
  227. <template x-for="foo in foos">
  228. <h1>
  229. <template x-for="bar in foo.bars">
  230. <h2 x-text="bar"></h2>
  231. </template>
  232. </h1>
  233. </template>
  234. </div>
  235. `,
  236. ({ get }) => {
  237. get('h1:nth-of-type(1) h2:nth-of-type(1)').should(beVisible())
  238. get('h1:nth-of-type(1) h2:nth-of-type(2)').should(beVisible())
  239. get('h1:nth-of-type(2) h2:nth-of-type(1)').should(notBeVisible())
  240. get('button').click()
  241. get('h1:nth-of-type(1) h2:nth-of-type(1)').should(beVisible())
  242. get('h1:nth-of-type(1) h2:nth-of-type(2)').should(beVisible())
  243. get('h1:nth-of-type(2) h2:nth-of-type(1)').should(beVisible())
  244. }
  245. )
  246. test('x-for updates the right elements when new item are inserted at the beginning of the list',
  247. html`
  248. <div x-data="{ items: [{name: 'one', key: '1'}, {name: 'two', key: '2'}] }">
  249. <button x-on:click="items = [{name: 'zero', key: '0'}, {name: 'one', key: '1'}, {name: 'two', key: '2'}]">click me</button>
  250. <template x-for="item in items" :key="item.key">
  251. <span x-text="item.name"></span>
  252. </template>
  253. </div>
  254. `,
  255. ({ get }) => {
  256. get('span:nth-of-type(1)').should(haveText('one'))
  257. get('span:nth-of-type(2)').should(haveText('two'))
  258. get('button').click()
  259. get('span:nth-of-type(1)').should(haveText('zero'))
  260. get('span:nth-of-type(2)').should(haveText('one'))
  261. get('span:nth-of-type(3)').should(haveText('two'))
  262. }
  263. )
  264. test('nested x-for access outer loop variable',
  265. html`
  266. <div x-data="{ foos: [ {name: 'foo', bars: ['bob', 'lob']}, {name: 'baz', bars: ['bab', 'lab']} ] }">
  267. <template x-for="foo in foos">
  268. <h1>
  269. <template x-for="bar in foo.bars">
  270. <h2 x-text="foo.name+': '+bar"></h2>
  271. </template>
  272. </h1>
  273. </template>
  274. </div>
  275. `,
  276. ({ get }) => {
  277. get('h1:nth-of-type(1) h2:nth-of-type(1)').should(haveText('foo: bob'))
  278. get('h1:nth-of-type(1) h2:nth-of-type(2)').should(haveText('foo: lob'))
  279. get('h1:nth-of-type(2) h2:nth-of-type(1)').should(haveText('baz: bab'))
  280. get('h1:nth-of-type(2) h2:nth-of-type(2)').should(haveText('baz: lab'))
  281. }
  282. )
  283. test('sibling x-for do not interact with each other',
  284. html`
  285. <div x-data="{ foos: [1], bars: [1, 2] }">
  286. <template x-for="foo in foos">
  287. <h1 x-text="foo"></h1>
  288. </template>
  289. <template x-for="bar in bars">
  290. <h2 x-text="bar"></h2>
  291. </template>
  292. <button @click="foos = [1, 2];bars = [1, 2, 3]">Change</button>
  293. </div>
  294. `,
  295. ({ get }) => {
  296. get('h1:nth-of-type(1)').should(haveText('1'))
  297. get('h2:nth-of-type(1)').should(haveText('1'))
  298. get('h2:nth-of-type(2)').should(haveText('2'))
  299. get('button').click()
  300. get('h1:nth-of-type(1)').should(haveText('1'))
  301. get('h1:nth-of-type(2)').should(haveText('2'))
  302. get('h2:nth-of-type(1)').should(haveText('1'))
  303. get('h2:nth-of-type(2)').should(haveText('2'))
  304. get('h2:nth-of-type(3)').should(haveText('3'))
  305. }
  306. )
  307. test('x-for over range using i in x syntax',
  308. html`
  309. <div x-data>
  310. <template x-for="i in 10">
  311. <span x-text="i"></span>
  312. </template>
  313. </div>
  314. `,
  315. ({ get }) => get('span').should(haveLength('10'))
  316. )
  317. test('x-for over range using i in property syntax',
  318. html`
  319. <div x-data="{ count: 10 }">
  320. <template x-for="i in count">
  321. <span x-text="i"></span>
  322. </template>
  323. </div>
  324. `,
  325. ({ get }) => get('span').should(haveLength('10'))
  326. )
  327. // @flaky
  328. test.retry(2)('x-for with an array of numbers',
  329. `
  330. <div x-data="{ items: [] }">
  331. <template x-for="i in items">
  332. <span x-text="i"></span>
  333. </template>
  334. <button @click="items.push(2)" id="first">click me</button>
  335. <button @click="items.push(3)" id="second">click me</button>
  336. </div>
  337. `,
  338. ({ get }) => {
  339. get('span').should(haveLength('0'))
  340. get('#first').click()
  341. get('span').should(haveLength('1'))
  342. get('#second').click()
  343. get('span').should(haveLength('2'))
  344. }
  345. )