repeat_spec.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-repeat', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. it('objects', function (done) {
  11. var vm = new Vue({
  12. el: el,
  13. data: {
  14. items: [{a:1}, {a:2}]
  15. },
  16. template: '<div v-repeat="items">{{$index}} {{a}}</div>'
  17. })
  18. assertMutations(vm, el, done)
  19. })
  20. it('primitive values', function (done) {
  21. var vm = new Vue({
  22. el: el,
  23. data: {
  24. items: [2, 1, 2]
  25. },
  26. template: '<div v-repeat="items">{{$index}} {{$value}}</div>'
  27. })
  28. assertPrimitiveMutations(vm, el, done)
  29. })
  30. it('objects with identifier', function (done) {
  31. var vm = new Vue({
  32. el: el,
  33. data: {
  34. items: [{a:1}, {a:2}]
  35. },
  36. template: '<div v-repeat="item:items">{{$index}} {{item.a}}</div>'
  37. })
  38. assertMutations(vm, el, done)
  39. })
  40. it('primitive with identifier', function (done) {
  41. var vm = new Vue({
  42. el: el,
  43. data: {
  44. items: [2, 1, 2]
  45. },
  46. template: '<div v-repeat="item:items">{{$index}} {{item}}</div>'
  47. })
  48. assertPrimitiveMutations(vm, el, done)
  49. })
  50. it('repeating an object of objects', function (done) {
  51. var vm = new Vue({
  52. el: el,
  53. data: {
  54. items: {
  55. a: {a:1},
  56. b: {a:2}
  57. }
  58. },
  59. template: '<div v-repeat="items">{{$index}} {{$key}} {{a}}</div>'
  60. })
  61. assertObjectMutations(vm, el, done)
  62. })
  63. it('repeating an object of primitives', function (done) {
  64. var vm = new Vue({
  65. el: el,
  66. data: {
  67. items: {
  68. a: 1,
  69. b: 2
  70. }
  71. },
  72. template: '<div v-repeat="items">{{$index}} {{$key}} {{$value}}</div>'
  73. })
  74. assertObjectPrimitiveMutations(vm, el, done)
  75. })
  76. it('repeating an object of objects with identifier', function (done) {
  77. var vm = new Vue({
  78. el: el,
  79. data: {
  80. items: {
  81. a: {a:1},
  82. b: {a:2}
  83. }
  84. },
  85. template: '<div v-repeat="item:items">{{$index}} {{$key}} {{item.a}}</div>'
  86. })
  87. assertObjectMutations(vm, el, done)
  88. })
  89. it('repeating an object of primitives with identifier', function (done) {
  90. var vm = new Vue({
  91. el: el,
  92. data: {
  93. items: {
  94. a: 1,
  95. b: 2
  96. }
  97. },
  98. template: '<div v-repeat="item:items">{{$index}} {{$key}} {{item}}</div>'
  99. })
  100. assertObjectPrimitiveMutations(vm, el, done)
  101. })
  102. it('array of arrays', function () {
  103. var vm = new Vue({
  104. el: el,
  105. data: {
  106. items: [[1,1], [2,2], [3,3]]
  107. },
  108. template: '<div v-repeat="items">{{$index}} {{$value}}</div>'
  109. })
  110. var markup = vm.items.map(function (item, i) {
  111. return '<div>' + i + ' ' + item.toString() + '</div>'
  112. }).join('') + '<!--v-repeat-->'
  113. expect(el.innerHTML).toBe(markup)
  114. })
  115. it('repeating object with filter', function () {
  116. var vm = new Vue({
  117. el: el,
  118. data: {
  119. items: {
  120. a: { msg: 'aaa' },
  121. b: { msg: 'bbb' }
  122. }
  123. },
  124. template: '<div v-repeat="items | filterBy \'aaa\'">{{msg}}</div>'
  125. })
  126. expect(el.innerHTML).toBe('<div>aaa</div><!--v-repeat-->')
  127. })
  128. it('v-component', function (done) {
  129. var vm = new Vue({
  130. el: el,
  131. data: {
  132. items: [{a:1}, {a:2}]
  133. },
  134. template: '<p v-repeat="items" v-component="test"></p>',
  135. components: {
  136. test: {
  137. template: '<div>{{$index}} {{a}}</div>',
  138. replace: true
  139. }
  140. }
  141. })
  142. assertMutations(vm, el, done)
  143. })
  144. it('v-component with inline-template', function (done) {
  145. var vm = new Vue({
  146. el: el,
  147. data: {
  148. items: [{a:1}, {a:2}]
  149. },
  150. template:
  151. '<div v-repeat="items" v-component="test" inline-template>' +
  152. '{{$index}} {{a}}' +
  153. '</div>',
  154. components: {
  155. test: {}
  156. }
  157. })
  158. assertMutations(vm, el, done)
  159. })
  160. it('v-component with inline-template on <template>', function (done) {
  161. var vm = new Vue({
  162. el: el,
  163. data: {
  164. items: [{a:1}, {a:2}]
  165. },
  166. template:
  167. '<template v-repeat="items" v-component="test" inline-template>' +
  168. '<div>{{$index}} {{a}}</div>' +
  169. '</template>',
  170. components: {
  171. test: {}
  172. }
  173. })
  174. assertMutations(vm, el, done, true)
  175. })
  176. it('v-component with primitive values', function (done) {
  177. var vm = new Vue({
  178. el: el,
  179. data: {
  180. items: [2, 1, 2]
  181. },
  182. template: '<p v-repeat="items" v-component="test"></p>',
  183. components: {
  184. test: {
  185. template: '<div>{{$index}} {{$value}}</div>',
  186. replace: true
  187. }
  188. }
  189. })
  190. assertPrimitiveMutations(vm, el, done)
  191. })
  192. it('v-component with object of objects', function (done) {
  193. var vm = new Vue({
  194. el: el,
  195. data: {
  196. items: {
  197. a: {a:1},
  198. b: {a:2}
  199. }
  200. },
  201. template: '<p v-repeat="items" v-component="test"></p>',
  202. components: {
  203. test: {
  204. template: '<div>{{$index}} {{$key}} {{a}}</div>',
  205. replace: true
  206. }
  207. }
  208. })
  209. assertObjectMutations(vm, el, done)
  210. })
  211. it('custom element component', function () {
  212. var vm = new Vue({
  213. el: el,
  214. data: {
  215. items: [{a:1}, {a:2}, {a:3}]
  216. },
  217. template: '<test-component v-repeat="items"></test-component>',
  218. components: {
  219. 'test-component': {
  220. template: '{{$index}} {{a}}'
  221. }
  222. }
  223. })
  224. expect(el.innerHTML).toBe(
  225. '<test-component>0 1</test-component>' +
  226. '<test-component>1 2</test-component>' +
  227. '<test-component>2 3</test-component>' +
  228. '<!--v-repeat-->'
  229. )
  230. })
  231. it('custom element component with replace:true', function () {
  232. var vm = new Vue({
  233. el: el,
  234. data: {
  235. items: [{a:1}, {a:2}, {a:3}]
  236. },
  237. template: '<test-component v-repeat="items"></test-component>',
  238. components: {
  239. 'test-component': {
  240. template: '<p>{{$index}} {{a}}</p>',
  241. replace: true
  242. }
  243. }
  244. })
  245. expect(el.innerHTML).toBe('<p>0 1</p><p>1 2</p><p>2 3</p><!--v-repeat-->')
  246. })
  247. it('nested repeats', function () {
  248. var vm = new Vue({
  249. el: el,
  250. data: {
  251. items: [
  252. { items: [{a:1}, {a:2}], a: 1 },
  253. { items: [{a:3}, {a:4}], a: 2 }
  254. ]
  255. },
  256. template: '<div v-repeat="items">' +
  257. '<p v-repeat="items">{{$index}} {{a}} {{$parent.$index}} {{$parent.a}}</p>' +
  258. '</div>'
  259. })
  260. expect(el.innerHTML).toBe(
  261. '<div><p>0 1 0 1</p><p>1 2 0 1</p><!--v-repeat--></div>' +
  262. '<div><p>0 3 1 2</p><p>1 4 1 2</p><!--v-repeat--></div>' +
  263. '<!--v-repeat-->'
  264. )
  265. })
  266. it('nested repeats on object', function(){
  267. var vm = new Vue({
  268. el: el,
  269. data: {
  270. listHash: {
  271. listA: [{a: 1},{a: 2}],
  272. listB: [{a: 1},{a: 2}]
  273. }
  274. },
  275. template: '<div v-repeat="listHash">{{$key}}' +
  276. '<p v-repeat="$value">{{a}}</p>' +
  277. '</div>'
  278. })
  279. function output(key){
  280. var key1 = key === 'listA' ? 'listB' : 'listA'
  281. return '<div>'+ key +'<p>1</p><p>2</p><!--v-repeat--></div>' +
  282. '<div>'+ key1 +'<p>1</p><p>2</p><!--v-repeat--></div>' +
  283. '<!--v-repeat-->'
  284. }
  285. expect(el.innerHTML === output('listA') || el.innerHTML === output('listB')).toBeTruthy()
  286. })
  287. it('dynamic component type based on instance data', function () {
  288. var vm = new Vue({
  289. el: el,
  290. template: '<div v-repeat="list" v-component="view-{{type}}"></div>',
  291. data: {
  292. list: [
  293. { type: 'a' },
  294. { type: 'b' },
  295. { type: 'c' }
  296. ]
  297. },
  298. components: {
  299. 'view-a': {
  300. template: 'AAA'
  301. },
  302. 'view-b': {
  303. template: 'BBB'
  304. },
  305. 'view-c': {
  306. template: 'CCC'
  307. }
  308. }
  309. })
  310. expect(el.innerHTML).toBe('<div>AAA</div><div>BBB</div><div>CCC</div><!--v-repeat-->')
  311. // #458 meta properties
  312. vm = new Vue({
  313. el: el,
  314. template: '<div v-repeat="list" v-component="view-{{$value}}"></div>',
  315. data: {
  316. list: ['a', 'b', 'c']
  317. },
  318. components: {
  319. 'view-a': {
  320. template: 'AAA'
  321. },
  322. 'view-b': {
  323. template: 'BBB'
  324. },
  325. 'view-c': {
  326. template: 'CCC'
  327. }
  328. }
  329. })
  330. expect(el.innerHTML).toBe('<div>AAA</div><div>BBB</div><div>CCC</div><!--v-repeat-->')
  331. })
  332. it('block repeat', function (done) {
  333. var vm = new Vue({
  334. el: el,
  335. template: '<template v-repeat="list"><p>{{a}}</p><p>{{a + 1}}</p></template>',
  336. data: {
  337. list: [
  338. { a: 1 },
  339. { a: 2 },
  340. { a: 3 }
  341. ]
  342. }
  343. })
  344. assertMarkup()
  345. vm.list.reverse()
  346. _.nextTick(function () {
  347. assertMarkup()
  348. done()
  349. })
  350. function assertMarkup () {
  351. var markup = vm.list.map(function (item) {
  352. return '<!--v-start--><p>' + item.a + '</p><p>' + (item.a + 1) + '</p><!--v-end-->'
  353. }).join('')
  354. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  355. }
  356. })
  357. // added for #799
  358. it('block repeat with diff', function (done) {
  359. var vm = new Vue({
  360. el: el,
  361. template: '<template v-repeat="list" v-component="test"></template>',
  362. data: {
  363. list: [
  364. { a: 1 },
  365. { a: 2 },
  366. { a: 3 }
  367. ]
  368. },
  369. components: {
  370. test: {
  371. template: '<p>{{a}}</p><p>{{a + 1}}</p>'
  372. }
  373. }
  374. })
  375. assertMarkup()
  376. vm.list.reverse()
  377. _.nextTick(function () {
  378. assertMarkup()
  379. done()
  380. })
  381. function assertMarkup () {
  382. var markup = vm.list.map(function (item) {
  383. return '<!--v-start--><p>' + item.a + '</p><p>' + (item.a + 1) + '</p><!--v-end-->'
  384. }).join('')
  385. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  386. }
  387. })
  388. it('component + parent directive + transclusion', function (done) {
  389. var vm = new Vue({
  390. el: el,
  391. template: '<div v-repeat="list" v-component="test" v-class="cls">{{msg}}</div>',
  392. data: {
  393. cls: 'parent',
  394. msg: 'hi',
  395. list: [{a:1},{a:2},{a:3}]
  396. },
  397. components: {
  398. test: {
  399. replace: true,
  400. template: '<div class="child">{{a}} <content></content></div>'
  401. }
  402. }
  403. })
  404. var markup = vm.list.map(function (item) {
  405. return '<div class="child parent">' + item.a + ' hi</div>'
  406. }).join('')
  407. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  408. vm.msg = 'ho'
  409. markup = vm.list.map(function (item) {
  410. return '<div class="child parent">' + item.a + ' ho</div>'
  411. }).join('')
  412. _.nextTick(function () {
  413. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  414. done()
  415. })
  416. })
  417. it('array filters', function (done) {
  418. var vm = new Vue({
  419. el: el,
  420. template: '<div v-repeat="list | filterBy filterKey | orderBy sortKey -1">{{id}}</div>',
  421. data: {
  422. filterKey: 'hi!',
  423. sortKey: 'id',
  424. list: [
  425. { id: 1, id2: 4, msg: 'hi!' },
  426. { id: 2, id2: 3, msg: 'na' },
  427. { id: 3, id2: 2, msg: 'hi!' },
  428. { id: 4, id2: 1, msg: 'na' }
  429. ]
  430. }
  431. })
  432. assertMarkup()
  433. go(
  434. function () {
  435. vm.filterKey = 'na'
  436. }, assertMarkup
  437. )
  438. .then(
  439. function () {
  440. vm.sortKey = 'id2'
  441. }, assertMarkup
  442. )
  443. .then(
  444. function () {
  445. vm.list[0].id2 = 0
  446. }, assertMarkup
  447. )
  448. .then(
  449. function () {
  450. vm.list.push({ id: 0, id2: 4, msg: 'na' })
  451. }, assertMarkup
  452. )
  453. .then(
  454. function () {
  455. vm.list = [
  456. { id: 33, id2: 4, msg: 'hi!' },
  457. { id: 44, id2: 3, msg: 'na' }
  458. ]
  459. }, assertMarkup
  460. )
  461. .run(done)
  462. function assertMarkup () {
  463. var markup = vm.list
  464. .filter(function (item) {
  465. return item.msg === vm.filterKey
  466. })
  467. .sort(function (a, b) {
  468. return a[vm.sortKey] > b[vm.sortKey] ? -1 : 1
  469. })
  470. .map(function (item) {
  471. return '<div>' + item.id + '</div>'
  472. }).join('')
  473. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  474. }
  475. })
  476. it('orderBy supporting $key for object repeaters', function (done) {
  477. var vm = new Vue({
  478. el: el,
  479. template: '<div v-repeat="obj | orderBy sortKey">{{$value}}</div>',
  480. data: {
  481. sortKey: '$key',
  482. obj: {
  483. c: 1,
  484. a: 3,
  485. b: 2
  486. }
  487. }
  488. })
  489. expect(el.innerHTML).toBe('<div>3</div><div>2</div><div>1</div><!--v-repeat-->')
  490. vm.sortKey = '$value'
  491. _.nextTick(function () {
  492. expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div><!--v-repeat-->')
  493. done()
  494. })
  495. })
  496. it('orderBy supporting $value for primitive arrays', function () {
  497. var vm = new Vue({
  498. el: el,
  499. template: '<div v-repeat="list | orderBy \'$value\'">{{$value}}</div>',
  500. data: {
  501. list: [3, 2, 1]
  502. }
  503. })
  504. expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div><!--v-repeat-->')
  505. })
  506. it('track by id', function (done) {
  507. assertTrackBy('<div v-repeat="list" v-component="test" track-by="id"></div>', '{{msg}}', function () {
  508. assertTrackBy('<div v-repeat="item:list" v-component="test" track-by="id"></div>', '{{item.msg}}', done)
  509. })
  510. function assertTrackBy (template, componentTemplate, next) {
  511. var vm = new Vue({
  512. el: el,
  513. template: template,
  514. data: {
  515. list: [
  516. { id: 1, msg: 'hi' },
  517. { id: 2, msg: 'ha' },
  518. { id: 3, msg: 'ho' }
  519. ]
  520. },
  521. components: {
  522. test: {
  523. template: componentTemplate
  524. }
  525. }
  526. })
  527. assertMarkup()
  528. var oldVms = vm._children.slice()
  529. // swap the data with different objects, but with
  530. // the same ID!
  531. vm.list = [
  532. { id: 1, msg: 'wa' },
  533. { id: 2, msg: 'wo' }
  534. ]
  535. _.nextTick(function () {
  536. assertMarkup()
  537. // should reuse old vms!
  538. var i = 2
  539. while (i--) {
  540. expect(vm._children[i]).toBe(oldVms[i])
  541. }
  542. next()
  543. })
  544. function assertMarkup () {
  545. var markup = vm.list.map(function (item) {
  546. return '<div>' + item.msg + '</div>'
  547. }).join('')
  548. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  549. }
  550. }
  551. })
  552. it('warn duplicate objects', function () {
  553. var obj = {}
  554. var vm = new Vue({
  555. el: el,
  556. template: '<div v-repeat="items" v-component="test"></div>',
  557. data: {
  558. items: [obj, obj]
  559. },
  560. components: {
  561. test: {}
  562. }
  563. })
  564. expect(hasWarned(_, 'Duplicate objects')).toBe(true)
  565. })
  566. it('warn duplicate trackby id', function () {
  567. var vm = new Vue({
  568. el: el,
  569. template: '<div v-repeat="items" v-component="test" track-by="id"></div>',
  570. data: {
  571. items: [{id:1}, {id:1}]
  572. },
  573. components: {
  574. test: {}
  575. }
  576. })
  577. expect(hasWarned(_, 'Duplicate track-by key')).toBe(true)
  578. })
  579. it('warn v-if', function () {
  580. var vm = new Vue({
  581. el: el,
  582. template: '<div v-repeat="items" v-if="aaa"></div>',
  583. data: {
  584. items: []
  585. }
  586. })
  587. expect(hasWarned(_, 'Don\'t use v-if')).toBe(true)
  588. })
  589. it('repeat number', function () {
  590. var vm = new Vue({
  591. el: el,
  592. template: '<div v-repeat="3">{{$index}} {{$value}}</div>'
  593. })
  594. expect(el.innerHTML).toBe('<div>0 0</div><div>1 1</div><div>2 2</div><!--v-repeat-->')
  595. })
  596. it('repeat string', function () {
  597. var vm = new Vue({
  598. el: el,
  599. template: '<div v-repeat="\'vue\'">{{$index}} {{$value}}</div>'
  600. })
  601. expect(el.innerHTML).toBe('<div>0 v</div><div>1 u</div><div>2 e</div><!--v-repeat-->')
  602. })
  603. it('teardown', function () {
  604. var vm = new Vue({
  605. el: el,
  606. template: '<div v-repeat="items" v-component="test"></div>',
  607. data: {
  608. items: [{a:1}, {a:2}]
  609. },
  610. components: {
  611. test: {}
  612. }
  613. })
  614. vm._directives[0].unbind()
  615. expect(vm._children.length).toBe(0)
  616. })
  617. it('with transition', function (done) {
  618. document.body.appendChild(el)
  619. var vm = new Vue({
  620. el: el,
  621. template: '<div v-repeat="items" v-transition="test">{{a}}</div>',
  622. data: {
  623. items: [{a:1}, {a:2}, {a:3}]
  624. },
  625. transitions: {
  626. test: {
  627. leave: function (el, done) {
  628. setTimeout(done, 1)
  629. }
  630. }
  631. }
  632. })
  633. vm.items.splice(1, 1, {a:4})
  634. setTimeout(function () {
  635. expect(el.innerHTML).toBe('<div>1</div><div>4</div><div>3</div><!--v-repeat-->')
  636. document.body.removeChild(el)
  637. done()
  638. }, 30)
  639. })
  640. it('sync $value changes back to original array/object', function (done) {
  641. var vm = new Vue({
  642. el: el,
  643. template:
  644. '<div v-repeat="items">{{$value}}</div>' +
  645. '<div v-repeat="obj">{{$value}}</div>',
  646. data: {
  647. items: ['a', 'b'],
  648. obj: { foo: 'a', bar: 'b' }
  649. }
  650. })
  651. vm._children[0].$value = 'c'
  652. var key = vm._children[2].$key
  653. vm._children[2].$value = 'd'
  654. _.nextTick(function () {
  655. expect(vm.items[0]).toBe('c')
  656. expect(vm.obj[key]).toBe('d')
  657. done()
  658. })
  659. })
  660. it('nested track by', function (done) {
  661. assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}<div v-repeat="list" track-by="id">{{msg}}</div></div>', function () {
  662. assertTrackBy('<div v-transition v-repeat="list" track-by="id">{{msg}}<div v-transition v-repeat="list" track-by="id">{{msg}}</div></div>', done)
  663. })
  664. function assertTrackBy(template, next) {
  665. var vm = new Vue({
  666. el: el,
  667. data: {
  668. list: [
  669. { id: 1, msg: 'hi', list: [
  670. { id: 1, msg: 'hi foo' }
  671. ] },
  672. { id: 2, msg: 'ha', list: [] },
  673. { id: 3, msg: 'ho', list: [] }
  674. ]
  675. },
  676. template: template
  677. })
  678. assertMarkup()
  679. var oldVms = vm._children.slice()
  680. vm.list = [
  681. { id: 1, msg: 'wa', list: [
  682. { id: 1, msg: 'hi foo' },
  683. { id: 2, msg: 'hi bar' }
  684. ] },
  685. { id: 2, msg: 'wo', list: [] }
  686. ]
  687. _.nextTick(function () {
  688. assertMarkup()
  689. // should reuse old vms!
  690. var i = 2
  691. while (i--) {
  692. expect(vm._children[i]).toBe(oldVms[i])
  693. }
  694. expect(vm._children[0]._children[0]).toBe(oldVms[0]._children[0])
  695. next()
  696. })
  697. function assertMarkup () {
  698. var markup = vm.list.map(function (item) {
  699. var sublist = item.list.map(function (item) {
  700. return '<div>' + item.msg + '</div>'
  701. }).join('') + '<!--v-repeat-->'
  702. return '<div>' + item.msg + sublist + '</div>'
  703. }).join('') + '<!--v-repeat-->'
  704. expect(el.innerHTML).toBe(markup)
  705. }
  706. }
  707. })
  708. })
  709. }
  710. /**
  711. * Simple helper for chained async asssertions
  712. *
  713. * @param {Function} fn - the data manipulation function
  714. * @param {Function} cb - the assertion fn to be called on nextTick
  715. */
  716. function go (fn, cb) {
  717. return {
  718. stack: [{fn:fn, cb:cb}],
  719. then: function (fn, cb) {
  720. this.stack.push({fn:fn, cb:cb})
  721. return this
  722. },
  723. run: function (done) {
  724. var self = this
  725. var step = this.stack.shift()
  726. if (!step) return done()
  727. step.fn()
  728. _.nextTick(function () {
  729. step.cb()
  730. self.run(done)
  731. })
  732. }
  733. }
  734. }
  735. /**
  736. * Assert mutation and markup correctness for v-repeat on
  737. * an Array of Objects
  738. */
  739. function assertMutations (vm, el, done, isBlock) {
  740. assertMarkup()
  741. var poppedItem
  742. go(
  743. function () {
  744. vm.items.push({a:3})
  745. },
  746. assertMarkup
  747. )
  748. .then(
  749. function () {
  750. vm.items.shift()
  751. },
  752. assertMarkup
  753. )
  754. .then(
  755. function () {
  756. vm.items.reverse()
  757. },
  758. assertMarkup
  759. )
  760. .then(
  761. function () {
  762. poppedItem = vm.items.pop()
  763. },
  764. assertMarkup
  765. )
  766. .then(
  767. function () {
  768. vm.items.unshift(poppedItem)
  769. },
  770. assertMarkup
  771. )
  772. .then(
  773. function () {
  774. vm.items.sort(function (a, b) {
  775. return a.a > b.a ? 1 : -1
  776. })
  777. },
  778. assertMarkup
  779. )
  780. .then(
  781. function () {
  782. vm.items.splice(1, 1, {a:5})
  783. },
  784. assertMarkup
  785. )
  786. // test swapping the array
  787. .then(
  788. function () {
  789. vm.items = [{a:0}, {a:1}, {a:2}]
  790. },
  791. assertMarkup
  792. )
  793. .run(done)
  794. function assertMarkup () {
  795. var markup = vm.items.map(function (item, i) {
  796. var el = '<div>' + i + ' ' + item.a + '</div>'
  797. if (isBlock) el = '<!--v-start-->' + el + '<!--v-end-->'
  798. return el
  799. }).join('')
  800. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  801. }
  802. }
  803. /**
  804. * Assert mutation and markup correctness for v-repeat on
  805. * an Array of primitive values
  806. */
  807. function assertPrimitiveMutations (vm, el, done) {
  808. assertMarkup()
  809. go(
  810. function () {
  811. // check duplicate
  812. vm.items.push(2, 2, 3)
  813. },
  814. assertMarkup
  815. )
  816. .then(
  817. function () {
  818. vm.items.shift()
  819. },
  820. assertMarkup
  821. )
  822. .then(
  823. function () {
  824. vm.items.reverse()
  825. },
  826. assertMarkup
  827. )
  828. .then(
  829. function () {
  830. vm.items.pop()
  831. },
  832. assertMarkup
  833. )
  834. .then(
  835. function () {
  836. vm.items.unshift(3)
  837. },
  838. assertMarkup
  839. )
  840. .then(
  841. function () {
  842. vm.items.sort(function (a, b) {
  843. return a > b ? 1 : -1
  844. })
  845. },
  846. assertMarkup
  847. )
  848. .then(
  849. function () {
  850. vm.items.splice(1, 1, 5)
  851. },
  852. assertMarkup
  853. )
  854. // test swapping the array
  855. .then(
  856. function () {
  857. vm.items = [1, 2, 2]
  858. },
  859. assertMarkup
  860. )
  861. .run(done)
  862. function assertMarkup () {
  863. var markup = vm.items.map(function (item, i) {
  864. return '<div>' + i + ' ' + item + '</div>'
  865. }).join('')
  866. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  867. }
  868. }
  869. /**
  870. * Assert mutation and markup correctness for v-repeat on
  871. * an Object of Objects
  872. */
  873. function assertObjectMutations (vm, el, done) {
  874. assertMarkup()
  875. go(
  876. function () {
  877. vm.items.a = {a:3}
  878. },
  879. assertMarkup
  880. )
  881. .then(
  882. function () {
  883. vm.items = {
  884. c: {a:1},
  885. d: {a:2}
  886. }
  887. },
  888. assertMarkup
  889. )
  890. .then(
  891. function () {
  892. vm.items.$add('a', {a:3})
  893. },
  894. assertMarkup
  895. )
  896. .run(done)
  897. function assertMarkup () {
  898. var markup = Object.keys(vm.items).map(function (key, i) {
  899. return '<div>' + i + ' ' + key + ' ' + vm.items[key].a + '</div>'
  900. }).join('')
  901. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  902. }
  903. }
  904. /**
  905. * Assert mutation and markup correctness for v-repeat on
  906. * an Object of primitive values
  907. */
  908. function assertObjectPrimitiveMutations (vm, el, done) {
  909. assertMarkup()
  910. go(
  911. function () {
  912. vm.items.a = 3
  913. },
  914. assertMarkup
  915. )
  916. .then(
  917. function () {
  918. vm.items = {
  919. c: 1,
  920. d: 2
  921. }
  922. },
  923. assertMarkup
  924. )
  925. .then(
  926. function () {
  927. vm.items.$add('a', 3)
  928. },
  929. assertMarkup
  930. )
  931. .run(done)
  932. function assertMarkup () {
  933. var markup = Object.keys(vm.items).map(function (key, i) {
  934. return '<div>' + i + ' ' + key + ' ' + vm.items[key] + '</div>'
  935. }).join('')
  936. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  937. }
  938. }