repeat_spec.js 23 KB

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