repeat_spec.js 14 KB

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