repeat_spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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('repeating object with filter', function () {
  103. var vm = new Vue({
  104. el: el,
  105. data: {
  106. items: {
  107. a: { msg: 'aaa' },
  108. b: { msg: 'bbb' }
  109. }
  110. },
  111. template: '<div v-repeat="items | filterBy \'aaa\'">{{msg}}</div>'
  112. })
  113. expect(el.innerHTML).toBe('<div>aaa</div><!--v-repeat-->')
  114. })
  115. it('v-component', function () {
  116. var vm = new Vue({
  117. el: el,
  118. data: {
  119. items: [{a:1}, {a:2}, {a:3}]
  120. },
  121. template: '<div v-repeat="items" v-component="test"></div>',
  122. components: {
  123. test: {
  124. template: '<p>{{$index}} {{a}}</p>',
  125. replace: true
  126. }
  127. }
  128. })
  129. expect(el.innerHTML).toBe('<p>0 1</p><p>1 2</p><p>2 3</p><!--v-repeat-->')
  130. })
  131. it('nested repeats', function () {
  132. var vm = new Vue({
  133. el: el,
  134. data: {
  135. items: [
  136. { items: [{a:1}, {a:2}], a: 1 },
  137. { items: [{a:3}, {a:4}], a: 2 }
  138. ]
  139. },
  140. template: '<div v-repeat="items">' +
  141. '<p v-repeat="items">{{$index}} {{a}} {{$parent.$index}} {{$parent.a}}</p>' +
  142. '</div>'
  143. })
  144. expect(el.innerHTML).toBe(
  145. '<div><p>0 1 0 1</p><p>1 2 0 1</p><!--v-repeat--></div>' +
  146. '<div><p>0 3 1 2</p><p>1 4 1 2</p><!--v-repeat--></div>' +
  147. '<!--v-repeat-->'
  148. )
  149. })
  150. it('dynamic component type based on instance data', function () {
  151. var vm = new Vue({
  152. el: el,
  153. template: '<div v-repeat="list" v-component="view-{{type}}"></div>',
  154. data: {
  155. list: [
  156. { type: 'a' },
  157. { type: 'b' },
  158. { type: 'c' }
  159. ]
  160. },
  161. components: {
  162. 'view-a': {
  163. template: 'AAA'
  164. },
  165. 'view-b': {
  166. template: 'BBB'
  167. },
  168. 'view-c': {
  169. template: 'CCC'
  170. }
  171. }
  172. })
  173. expect(el.innerHTML).toBe('<div>AAA</div><div>BBB</div><div>CCC</div><!--v-repeat-->')
  174. // #458 meta properties
  175. vm = new Vue({
  176. el: el,
  177. template: '<div v-repeat="list" v-component="view-{{$value}}"></div>',
  178. data: {
  179. list: ['a', 'b', 'c']
  180. },
  181. components: {
  182. 'view-a': {
  183. template: 'AAA'
  184. },
  185. 'view-b': {
  186. template: 'BBB'
  187. },
  188. 'view-c': {
  189. template: 'CCC'
  190. }
  191. }
  192. })
  193. expect(el.innerHTML).toBe('<div>AAA</div><div>BBB</div><div>CCC</div><!--v-repeat-->')
  194. })
  195. it('block repeat', function () {
  196. var vm = new Vue({
  197. el: el,
  198. template: '<template v-repeat="list"><p>{{a}}</p><p>{{a + 1}}</p></template>',
  199. data: {
  200. list: [
  201. { a: 1 },
  202. { a: 2 },
  203. { a: 3 }
  204. ]
  205. }
  206. })
  207. var markup = vm.list.map(function (item) {
  208. return '<!--v-start--><p>' + item.a + '</p><p>' + (item.a + 1) + '</p><!--v-end-->'
  209. }).join('')
  210. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  211. })
  212. it('array filters', function (done) {
  213. var vm = new Vue({
  214. el: el,
  215. template: '<div v-repeat="list | filterBy filterKey | orderBy sortKey -1">{{id}}</div>',
  216. data: {
  217. filterKey: 'hi!',
  218. sortKey: 'id',
  219. list: [
  220. { id: 1, id2: 4, msg: 'hi!' },
  221. { id: 2, id2: 3, msg: 'na' },
  222. { id: 3, id2: 2, msg: 'hi!' },
  223. { id: 4, id2: 1, msg: 'na' }
  224. ]
  225. }
  226. })
  227. assertMarkup()
  228. go(
  229. function () {
  230. vm.filterKey = 'na'
  231. }, assertMarkup
  232. )
  233. .then(
  234. function () {
  235. vm.sortKey = 'id2'
  236. }, assertMarkup
  237. )
  238. .then(
  239. function () {
  240. vm.list[0].id2 = 0
  241. }, assertMarkup
  242. )
  243. .then(
  244. function () {
  245. vm.list.push({ id: 0, id2: 4, msg: 'na' })
  246. }, assertMarkup
  247. )
  248. .then(
  249. function () {
  250. vm.list = [
  251. { id: 33, id2: 4, msg: 'hi!' },
  252. { id: 44, id2: 3, msg: 'na' }
  253. ]
  254. }, assertMarkup
  255. )
  256. .run(done)
  257. function assertMarkup () {
  258. var markup = vm.list
  259. .filter(function (item) {
  260. return item.msg === vm.filterKey
  261. })
  262. .sort(function (a, b) {
  263. return a[vm.sortKey] > b[vm.sortKey] ? -1 : 1
  264. })
  265. .map(function (item) {
  266. return '<div>' + item.id + '</div>'
  267. }).join('')
  268. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  269. }
  270. })
  271. it('track by id', function (done) {
  272. assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}</div>', function () {
  273. assertTrackBy('<div v-repeat="item:list" track-by="id">{{item.msg}}</div>', done)
  274. })
  275. function assertTrackBy (template, next) {
  276. var vm = new Vue({
  277. el: el,
  278. template: template,
  279. data: {
  280. list: [
  281. { id: 1, msg: 'hi' },
  282. { id: 2, msg: 'ha' },
  283. { id: 3, msg: 'ho' }
  284. ]
  285. }
  286. })
  287. assertMarkup()
  288. var oldVms = vm._children.slice()
  289. // swap the data with different objects, but with
  290. // the same ID!
  291. vm.list = [
  292. { id: 1, msg: 'wa' },
  293. { id: 2, msg: 'wo' }
  294. ]
  295. _.nextTick(function () {
  296. assertMarkup()
  297. // should reuse old vms!
  298. var i = 2
  299. while (i--) {
  300. expect(vm._children[i]).toBe(oldVms[i])
  301. }
  302. next()
  303. })
  304. function assertMarkup () {
  305. var markup = vm.list.map(function (item) {
  306. return '<div>' + item.msg + '</div>'
  307. }).join('')
  308. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  309. }
  310. }
  311. })
  312. it('warn duplicate objects', function () {
  313. var obj = {}
  314. var vm = new Vue({
  315. el: el,
  316. template: '<div v-repeat="items"></div>',
  317. data: {
  318. items: [obj, obj]
  319. }
  320. })
  321. expect(_.warn).toHaveBeenCalled()
  322. })
  323. it('warn duplicate trackby id', function () {
  324. var vm = new Vue({
  325. el: el,
  326. template: '<div v-repeat="items" trackby="id"></div>',
  327. data: {
  328. items: [{id:1}, {id:1}]
  329. }
  330. })
  331. expect(_.warn).toHaveBeenCalled()
  332. })
  333. it('warn v-if', function () {
  334. var vm = new Vue({
  335. el: el,
  336. template: '<div v-repeat="items" v-if="aaa"></div>',
  337. data: {
  338. items: []
  339. }
  340. })
  341. expect(_.warn).toHaveBeenCalled()
  342. })
  343. it('repeat number', function () {
  344. var vm = new Vue({
  345. el: el,
  346. template: '<div v-repeat="3">{{$index}} {{$value}}</div>'
  347. })
  348. expect(el.innerHTML).toBe('<div>0 0</div><div>1 1</div><div>2 2</div><!--v-repeat-->')
  349. })
  350. it('repeat string', function () {
  351. var vm = new Vue({
  352. el: el,
  353. template: '<div v-repeat="\'vue\'">{{$index}} {{$value}}</div>'
  354. })
  355. expect(el.innerHTML).toBe('<div>0 v</div><div>1 u</div><div>2 e</div><!--v-repeat-->')
  356. })
  357. it('teardown', function () {
  358. var vm = new Vue({
  359. el: el,
  360. template: '<div v-repeat="items">{{a}}</div>',
  361. data: {
  362. items: [{a:1}, {a:2}]
  363. }
  364. })
  365. vm._directives[0].unbind()
  366. expect(vm._children.length).toBe(0)
  367. })
  368. it('with transition', function (done) {
  369. // === IMPORTANT ===
  370. // PhantomJS always returns false when calling
  371. // Element.contains() on a comment node. This causes
  372. // transitions to be skipped. Monkey patching here
  373. // isn't ideal but does the job...
  374. var inDoc = _.inDoc
  375. _.inDoc = function () {
  376. return true
  377. }
  378. var vm = new Vue({
  379. el: el,
  380. template: '<div v-repeat="items" v-transition="test">{{a}}</div>',
  381. data: {
  382. items: [{a:1}, {a:2}, {a:3}]
  383. },
  384. transitions: {
  385. test: {
  386. leave: function (el, done) {
  387. setTimeout(done, 1)
  388. }
  389. }
  390. }
  391. })
  392. vm.items.splice(1, 1, {a:4})
  393. setTimeout(function () {
  394. expect(el.innerHTML).toBe('<div>1</div><div>4</div><div>3</div><!--v-repeat-->')
  395. // clean up
  396. _.inDoc = inDoc
  397. done()
  398. }, 30)
  399. })
  400. })
  401. }
  402. /**
  403. * Simple helper for chained async asssertions
  404. *
  405. * @param {Function} fn - the data manipulation function
  406. * @param {Function} cb - the assertion fn to be called on nextTick
  407. */
  408. function go (fn, cb) {
  409. return {
  410. stack: [{fn:fn, cb:cb}],
  411. then: function (fn, cb) {
  412. this.stack.push({fn:fn, cb:cb})
  413. return this
  414. },
  415. run: function (done) {
  416. var self = this
  417. var step = this.stack.shift()
  418. if (!step) return done()
  419. step.fn()
  420. _.nextTick(function () {
  421. step.cb()
  422. self.run(done)
  423. })
  424. }
  425. }
  426. }
  427. /**
  428. * Assert mutation and markup correctness for v-repeat on
  429. * an Array of Objects
  430. */
  431. function assertMutations (vm, el, done) {
  432. assertMarkup()
  433. var poppedItem
  434. go(
  435. function () {
  436. vm.items.push({a:3})
  437. },
  438. assertMarkup
  439. )
  440. .then(
  441. function () {
  442. vm.items.shift()
  443. },
  444. assertMarkup
  445. )
  446. .then(
  447. function () {
  448. vm.items.reverse()
  449. },
  450. assertMarkup
  451. )
  452. .then(
  453. function () {
  454. poppedItem = vm.items.pop()
  455. },
  456. assertMarkup
  457. )
  458. .then(
  459. function () {
  460. vm.items.unshift(poppedItem)
  461. },
  462. assertMarkup
  463. )
  464. .then(
  465. function () {
  466. vm.items.sort(function (a, b) {
  467. return a.a > b.a ? 1 : -1
  468. })
  469. },
  470. assertMarkup
  471. )
  472. .then(
  473. function () {
  474. vm.items.splice(1, 1, {a:5})
  475. },
  476. assertMarkup
  477. )
  478. // test swapping the array
  479. .then(
  480. function () {
  481. vm.items = [{a:0}, {a:1}, {a:2}]
  482. },
  483. assertMarkup
  484. )
  485. .run(done)
  486. function assertMarkup () {
  487. var markup = vm.items.map(function (item, i) {
  488. return '<div>' + i + ' ' + item.a + '</div>'
  489. }).join('')
  490. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  491. }
  492. }
  493. /**
  494. * Assert mutation and markup correctness for v-repeat on
  495. * an Array of primitive values
  496. */
  497. function assertPrimitiveMutations (vm, el, done) {
  498. assertMarkup()
  499. go(
  500. function () {
  501. // check duplicate
  502. vm.items.push(2, 2, 3)
  503. },
  504. assertMarkup
  505. )
  506. .then(
  507. function () {
  508. vm.items.shift()
  509. },
  510. assertMarkup
  511. )
  512. .then(
  513. function () {
  514. vm.items.reverse()
  515. },
  516. assertMarkup
  517. )
  518. .then(
  519. function () {
  520. vm.items.pop()
  521. },
  522. assertMarkup
  523. )
  524. .then(
  525. function () {
  526. vm.items.unshift(3)
  527. },
  528. assertMarkup
  529. )
  530. .then(
  531. function () {
  532. vm.items.sort(function (a, b) {
  533. return a > b ? 1 : -1
  534. })
  535. },
  536. assertMarkup
  537. )
  538. .then(
  539. function () {
  540. vm.items.splice(1, 1, 5)
  541. },
  542. assertMarkup
  543. )
  544. // test swapping the array
  545. .then(
  546. function () {
  547. vm.items = [1, 2, 2]
  548. },
  549. assertMarkup
  550. )
  551. .run(done)
  552. function assertMarkup () {
  553. var markup = vm.items.map(function (item, i) {
  554. return '<div>' + i + ' ' + item + '</div>'
  555. }).join('')
  556. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  557. }
  558. }
  559. /**
  560. * Assert mutation and markup correctness for v-repeat on
  561. * an Object of Objects
  562. */
  563. function assertObjectMutations (vm, el, done) {
  564. assertMarkup()
  565. go(
  566. function () {
  567. vm.items.a = {a:3}
  568. },
  569. assertMarkup
  570. )
  571. .then(
  572. function () {
  573. vm.items = {
  574. c: {a:1},
  575. d: {a:2}
  576. }
  577. },
  578. assertMarkup
  579. )
  580. .then(
  581. function () {
  582. vm.items.$add('a', {a:3})
  583. },
  584. assertMarkup
  585. )
  586. .run(done)
  587. function assertMarkup () {
  588. var markup = Object.keys(vm.items).map(function (key, i) {
  589. return '<div>' + i + ' ' + key + ' ' + vm.items[key].a + '</div>'
  590. }).join('')
  591. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  592. }
  593. }
  594. /**
  595. * Assert mutation and markup correctness for v-repeat on
  596. * an Object of primitive values
  597. */
  598. function assertObjectPrimitiveMutations (vm, el, done) {
  599. assertMarkup()
  600. go(
  601. function () {
  602. vm.items.a = 3
  603. },
  604. assertMarkup
  605. )
  606. .then(
  607. function () {
  608. vm.items = {
  609. c: 1,
  610. d: 2
  611. }
  612. },
  613. assertMarkup
  614. )
  615. .then(
  616. function () {
  617. vm.items.$add('a', 3)
  618. },
  619. assertMarkup
  620. )
  621. .run(done)
  622. function assertMarkup () {
  623. var markup = Object.keys(vm.items).map(function (key, i) {
  624. return '<div>' + i + ' ' + key + ' ' + vm.items[key] + '</div>'
  625. }).join('')
  626. expect(el.innerHTML).toBe(markup + '<!--v-repeat-->')
  627. }
  628. }