repeat_spec.js 24 KB

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