for_spec.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. var _ = require('src/util')
  2. var Vue = require('src')
  3. describe('v-for', function () {
  4. var el
  5. beforeEach(function () {
  6. el = document.createElement('div')
  7. })
  8. it('objects', function (done) {
  9. var vm = new Vue({
  10. el: el,
  11. data: {
  12. items: [{a: 1}, {a: 2}]
  13. },
  14. template: '<div v-for="item in items">{{$index}} {{item.a}}</div>'
  15. })
  16. assertMutations(vm, el, done)
  17. })
  18. it('primitives', function (done) {
  19. var vm = new Vue({
  20. el: el,
  21. data: {
  22. items: [1, 2, 3]
  23. },
  24. template: '<div v-for="item in items">{{$index}} {{item}}</div>'
  25. })
  26. assertPrimitiveMutations(vm, el, done)
  27. })
  28. it('object of objects', function (done) {
  29. var vm = new Vue({
  30. el: el,
  31. data: {
  32. items: {
  33. a: {a: 1},
  34. b: {a: 2}
  35. }
  36. },
  37. template: '<div v-for="item in items">{{$index}} {{$key}} {{item.a}}</div>'
  38. })
  39. assertObjectMutations(vm, el, done)
  40. })
  41. it('object of primitives', function (done) {
  42. var vm = new Vue({
  43. el: el,
  44. data: {
  45. items: {
  46. a: 1,
  47. b: 2
  48. }
  49. },
  50. template: '<div v-for="item in items">{{$index}} {{$key}} {{item}}</div>'
  51. })
  52. assertObjectPrimitiveMutations(vm, el, done)
  53. })
  54. it('array of arrays', function () {
  55. var vm = new Vue({
  56. el: el,
  57. data: {
  58. items: [[1, 1], [2, 2], [3, 3]]
  59. },
  60. template: '<div v-for="item in items">{{$index}} {{item}}</div>'
  61. })
  62. var markup = vm.items.map(function (item, i) {
  63. return '<div>' + i + ' ' + item.toString() + '</div>'
  64. }).join('')
  65. expect(el.innerHTML).toBe(markup)
  66. })
  67. it('repeating object with filter', function () {
  68. new Vue({
  69. el: el,
  70. data: {
  71. items: {
  72. a: { msg: 'aaa' },
  73. b: { msg: 'bbb' }
  74. }
  75. },
  76. template: '<div v-for="item in items | filterBy \'aaa\'">{{item.msg}}</div>'
  77. })
  78. expect(el.innerHTML).toBe('<div>aaa</div>')
  79. })
  80. it('filter converting array to object', function () {
  81. new Vue({
  82. el: el,
  83. data: {
  84. items: [
  85. { msg: 'aaa' },
  86. { msg: 'bbb' }
  87. ]
  88. },
  89. template: '<div v-for="item in items | test">{{item.msg}} {{$key}}</div>',
  90. filters: {
  91. test: function (val) {
  92. return {
  93. a: val[0],
  94. b: val[1]
  95. }
  96. }
  97. }
  98. })
  99. expect(el.innerHTML).toBe('<div>aaa a</div><div>bbb b</div>')
  100. })
  101. it('component', function (done) {
  102. var vm = new Vue({
  103. el: el,
  104. data: {
  105. items: [{a: 1}, {a: 2}]
  106. },
  107. template: '<test v-for="item in items" :index="$index" :item="item"></test>',
  108. components: {
  109. test: {
  110. props: ['index', 'item'],
  111. template: '<div>{{index}} {{item.a}}</div>',
  112. replace: true
  113. }
  114. }
  115. })
  116. assertMutations(vm, el, done)
  117. })
  118. it('is component', function (done) {
  119. var vm = new Vue({
  120. el: el,
  121. data: {
  122. items: [{a: 1}, {a: 2}]
  123. },
  124. template: '<p v-for="item in items" is="test" :index="$index" :item="item"></p>',
  125. components: {
  126. test: {
  127. props: ['index', 'item'],
  128. template: '<div>{{index}} {{item.a}}</div>',
  129. replace: true
  130. }
  131. }
  132. })
  133. assertMutations(vm, el, done)
  134. })
  135. it('component with inline-template', function (done) {
  136. var vm = new Vue({
  137. el: el,
  138. data: {
  139. items: [{a: 1}, {a: 2}]
  140. },
  141. template:
  142. '<test v-for="item in items" :index="$index" :item="item" inline-template>' +
  143. '{{index}} {{item.a}}' +
  144. '</test>',
  145. components: {
  146. test: {
  147. props: ['index', 'item']
  148. }
  149. }
  150. })
  151. assertMutations(vm, el, done)
  152. })
  153. it('component with primitive values', function (done) {
  154. var vm = new Vue({
  155. el: el,
  156. data: {
  157. items: [1, 2, 3]
  158. },
  159. template: '<test v-for="item in items" :index="$index" :value="item"></test>',
  160. components: {
  161. test: {
  162. props: ['index', 'value'],
  163. template: '<div>{{index}} {{value}}</div>',
  164. replace: true
  165. }
  166. }
  167. })
  168. assertPrimitiveMutations(vm, el, done)
  169. })
  170. it('component with object of objects', function (done) {
  171. var vm = new Vue({
  172. el: el,
  173. data: {
  174. items: {
  175. a: {a: 1},
  176. b: {a: 2}
  177. }
  178. },
  179. template: '<test v-for="item in items" :key="$key" :index="$index" :value="item"></test>',
  180. components: {
  181. test: {
  182. props: ['key', 'index', 'value'],
  183. template: '<div>{{index}} {{key}} {{value.a}}</div>',
  184. replace: true
  185. }
  186. }
  187. })
  188. assertObjectMutations(vm, el, done)
  189. })
  190. it('nested loops', function () {
  191. new Vue({
  192. el: el,
  193. data: {
  194. items: [
  195. { items: [{a: 1}, {a: 2}], a: 1 },
  196. { items: [{a: 3}, {a: 4}], a: 2 }
  197. ]
  198. },
  199. template: '<div v-for="item in items">' +
  200. '<p v-for="subItem in item.items">{{$index}} {{subItem.a}} {{$parent.$index}} {{item.a}}</p>' +
  201. '</div>'
  202. })
  203. expect(el.innerHTML).toBe(
  204. '<div><p>0 1 0 1</p><p>1 2 0 1</p></div>' +
  205. '<div><p>0 3 1 2</p><p>1 4 1 2</p></div>'
  206. )
  207. })
  208. it('nested loops on object', function () {
  209. new Vue({
  210. el: el,
  211. data: {
  212. listHash: {
  213. listA: [{a: 1}, {a: 2}],
  214. listB: [{a: 1}, {a: 2}]
  215. }
  216. },
  217. template:
  218. '<div v-for="list in listHash">' +
  219. '{{$key}}' +
  220. '<p v-for="item in list">{{item.a}}</p>' +
  221. '</div>'
  222. })
  223. function output (key) {
  224. var key1 = key === 'listA' ? 'listB' : 'listA'
  225. return '<div>' + key + '<p>1</p><p>2</p></div>' +
  226. '<div>' + key1 + '<p>1</p><p>2</p></div>'
  227. }
  228. expect(el.innerHTML === output('listA') || el.innerHTML === output('listB')).toBeTruthy()
  229. })
  230. it('dynamic component type based on instance data', function () {
  231. new Vue({
  232. el: el,
  233. template: '<component v-for="item in list" :is="\'view-\' + item.type"></component>',
  234. data: {
  235. list: [
  236. { type: 'a' },
  237. { type: 'b' },
  238. { type: 'c' }
  239. ]
  240. },
  241. components: {
  242. 'view-a': {
  243. template: 'AAA'
  244. },
  245. 'view-b': {
  246. template: 'BBB'
  247. },
  248. 'view-c': {
  249. template: 'CCC'
  250. }
  251. }
  252. })
  253. expect(el.innerHTML).toBe('<component>AAA</component><component>BBB</component><component>CCC</component>')
  254. // primitive
  255. el = document.createElement('div')
  256. new Vue({
  257. el: el,
  258. template: '<component v-for="type in list" :is="\'view-\' + type"></component>',
  259. data: {
  260. list: ['a', 'b', 'c']
  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('<component>AAA</component><component>BBB</component><component>CCC</component>')
  275. })
  276. it('fragment loop', function (done) {
  277. var vm = new Vue({
  278. el: el,
  279. template: '<template v-for="item in list"><p>{{item.a}}</p><p>{{item.a + 1}}</p></template>',
  280. data: {
  281. list: [
  282. { a: 1 },
  283. { a: 2 },
  284. { a: 3 }
  285. ]
  286. }
  287. })
  288. assertMarkup()
  289. vm.list.reverse()
  290. _.nextTick(function () {
  291. assertMarkup()
  292. vm.list.splice(1, 1)
  293. _.nextTick(function () {
  294. assertMarkup()
  295. vm.list.splice(1, 0, { a: 2 })
  296. _.nextTick(function () {
  297. assertMarkup()
  298. done()
  299. })
  300. })
  301. })
  302. function assertMarkup () {
  303. var markup = vm.list.map(function (item) {
  304. return '<p>' + item.a + '</p><p>' + (item.a + 1) + '</p>'
  305. }).join('')
  306. expect(el.innerHTML).toBe(markup)
  307. }
  308. })
  309. it('fragment loop with component', function (done) {
  310. var vm = new Vue({
  311. el: el,
  312. template: '<template v-for="item in list"><test :a="item.a"></test></template>',
  313. data: {
  314. list: [
  315. { a: 1 },
  316. { a: 2 },
  317. { a: 3 }
  318. ]
  319. },
  320. components: {
  321. test: {
  322. props: ['a'],
  323. template: '{{a}}'
  324. }
  325. }
  326. })
  327. assertMarkup()
  328. vm.list.reverse()
  329. _.nextTick(function () {
  330. assertMarkup()
  331. vm.list.splice(1, 1)
  332. _.nextTick(function () {
  333. assertMarkup()
  334. vm.list.splice(1, 0, { a: 2 })
  335. _.nextTick(function () {
  336. assertMarkup()
  337. done()
  338. })
  339. })
  340. })
  341. function assertMarkup () {
  342. var markup = vm.list.map(function (item) {
  343. return '<test>' + item.a + '</test>'
  344. }).join('')
  345. expect(el.innerHTML).toBe(markup)
  346. }
  347. })
  348. it('array filters', function (done) {
  349. var vm = new Vue({
  350. el: el,
  351. template: '<div v-for="item in list | filterBy filterKey | orderBy sortKey -1 | limitBy 2">{{item.id}}</div>',
  352. data: {
  353. filterKey: 'hi!',
  354. sortKey: 'id',
  355. list: [
  356. { id: 1, id2: 4, msg: 'hi!' },
  357. { id: 2, id2: 3, msg: 'na' },
  358. { id: 3, id2: 2, msg: 'hi!' },
  359. { id: 4, id2: 1, msg: 'na' }
  360. ]
  361. }
  362. })
  363. assertMarkup()
  364. go(
  365. function () {
  366. vm.filterKey = 'na'
  367. }, assertMarkup
  368. )
  369. .then(
  370. function () {
  371. vm.sortKey = 'id2'
  372. }, assertMarkup
  373. )
  374. .then(
  375. function () {
  376. vm.list[0].id2 = 0
  377. }, assertMarkup
  378. )
  379. .then(
  380. function () {
  381. vm.list.push({ id: 0, id2: 4, msg: 'na' })
  382. }, assertMarkup
  383. )
  384. .then(
  385. function () {
  386. vm.list = [
  387. { id: 33, id2: 4, msg: 'hi!' },
  388. { id: 44, id2: 3, msg: 'na' }
  389. ]
  390. }, assertMarkup
  391. )
  392. .run(done)
  393. function assertMarkup () {
  394. var markup = vm.list
  395. .filter(function (item) {
  396. return item.msg === vm.filterKey
  397. })
  398. .sort(function (a, b) {
  399. return a[vm.sortKey] > b[vm.sortKey] ? -1 : 1
  400. })
  401. .map(function (item) {
  402. return '<div>' + item.id + '</div>'
  403. })
  404. .slice(0, 2)
  405. .join('')
  406. expect(el.innerHTML).toBe(markup)
  407. }
  408. })
  409. it('orderBy supporting $key for object repeaters', function (done) {
  410. var vm = new Vue({
  411. el: el,
  412. template: '<div v-for="val in obj | orderBy sortKey">{{val}}</div>',
  413. data: {
  414. sortKey: '$key',
  415. obj: {
  416. c: 1,
  417. a: 3,
  418. b: 2
  419. }
  420. }
  421. })
  422. expect(el.innerHTML).toBe('<div>3</div><div>2</div><div>1</div>')
  423. vm.sortKey = 'val'
  424. _.nextTick(function () {
  425. expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
  426. done()
  427. })
  428. })
  429. it('orderBy supporting alias for primitive arrays', function () {
  430. new Vue({
  431. el: el,
  432. template: '<div v-for="val in list | orderBy \'val\'">{{val}}</div>',
  433. data: {
  434. list: [3, 2, 1]
  435. }
  436. })
  437. expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
  438. })
  439. it('track by id', function (done) {
  440. var vm = new Vue({
  441. el: el,
  442. template: '<test v-for="item in list" :item="item" track-by="id"></test>',
  443. data: {
  444. list: [
  445. { id: 1, msg: 'hi' },
  446. { id: 2, msg: 'ha' },
  447. { id: 3, msg: 'ho' }
  448. ]
  449. },
  450. components: {
  451. test: {
  452. props: ['item'],
  453. template: '{{item.msg}}'
  454. }
  455. }
  456. })
  457. assertMarkup()
  458. var oldVms = vm.$children.slice()
  459. // swap the data with different objects, but with
  460. // the same ID!
  461. vm.list = [
  462. { id: 1, msg: 'wa' },
  463. { id: 2, msg: 'wo' }
  464. ]
  465. _.nextTick(function () {
  466. assertMarkup()
  467. // should reuse old vms!
  468. var i = 2
  469. while (i--) {
  470. expect(vm.$children[i]).toBe(oldVms[i])
  471. }
  472. done()
  473. })
  474. function assertMarkup () {
  475. var markup = vm.list.map(function (item) {
  476. return '<test>' + item.msg + '</test>'
  477. }).join('')
  478. expect(el.innerHTML).toBe(markup)
  479. }
  480. })
  481. it('track by $index', function (done) {
  482. var vm = new Vue({
  483. el: el,
  484. data: {
  485. items: [{a: 1}, {a: 2}]
  486. },
  487. template: '<div v-for="item in items" track-by="$index">{{$index}} {{item.a}}</div>'
  488. })
  489. assertMarkup()
  490. var el1 = el.children[0]
  491. var el2 = el.children[1]
  492. vm.items = [{a: 3}, {a: 2}, {a: 1}]
  493. _.nextTick(function () {
  494. assertMarkup()
  495. // should mutate the DOM in-place
  496. expect(el.children[0]).toBe(el1)
  497. expect(el.children[1]).toBe(el2)
  498. done()
  499. })
  500. function assertMarkup () {
  501. expect(el.innerHTML).toBe(vm.items.map(function (item, i) {
  502. return '<div>' + i + ' ' + item.a + '</div>'
  503. }).join(''))
  504. }
  505. })
  506. it('primitive values track by $index', function (done) {
  507. var vm = new Vue({
  508. el: el,
  509. data: {
  510. items: [1, 2, 3]
  511. },
  512. template: '<div v-for="item in items" track-by="$index">{{$index}} {{item}}</div>'
  513. })
  514. assertPrimitiveMutationsWithDuplicates(vm, el, done)
  515. })
  516. it('warn missing alias', function () {
  517. new Vue({
  518. el: el,
  519. template: '<div v-for="items"></div>'
  520. })
  521. expect('Alias is required in v-for').toHaveBeenWarned()
  522. })
  523. it('warn duplicate objects', function () {
  524. var obj = {}
  525. new Vue({
  526. el: el,
  527. template: '<div v-for="item in items"></div>',
  528. data: {
  529. items: [obj, obj]
  530. }
  531. })
  532. expect('Duplicate value').toHaveBeenWarned()
  533. })
  534. it('warn duplicate objects on diff', function (done) {
  535. var obj = {}
  536. var vm = new Vue({
  537. el: el,
  538. template: '<div v-for="item in items"></div>',
  539. data: {
  540. items: [obj]
  541. }
  542. })
  543. expect(getWarnCount()).toBe(0)
  544. vm.items.push(obj)
  545. _.nextTick(function () {
  546. expect('Duplicate value').toHaveBeenWarned()
  547. done()
  548. })
  549. })
  550. it('warn duplicate trackby id', function () {
  551. new Vue({
  552. el: el,
  553. template: '<div v-for="item in items" track-by="id"></div>',
  554. data: {
  555. items: [{id: 1}, {id: 1}]
  556. }
  557. })
  558. expect('Duplicate value').toHaveBeenWarned()
  559. })
  560. it('key val syntax with object', function (done) {
  561. var vm = new Vue({
  562. el: el,
  563. template: '<div v-for="(key,val) in items">{{$index}} {{key}} {{val.a}}</div>',
  564. data: {
  565. items: {
  566. a: {a: 1},
  567. b: {a: 2}
  568. }
  569. }
  570. })
  571. assertObjectMutations(vm, el, done)
  572. })
  573. it('key val syntax with array', function (done) {
  574. var vm = new Vue({
  575. el: el,
  576. template: '<div v-for="(i, item) in items">{{i}} {{item.a}}</div>',
  577. data: {
  578. items: [{a: 1}, {a: 2}]
  579. }
  580. })
  581. assertMutations(vm, el, done)
  582. })
  583. it('key val syntax with nested v-for s', function () {
  584. new Vue({
  585. el: el,
  586. template: '<div v-for="(key,val) in items"><div v-for="(subkey,subval) in val">{{key}} {{subkey}} {{subval}}</div></div>',
  587. data: {
  588. items: {'a': {'b': 'c'}}
  589. }
  590. })
  591. expect(el.innerHTML).toBe('<div><div>a b c</div></div>')
  592. })
  593. it('repeat number', function () {
  594. new Vue({
  595. el: el,
  596. template: '<div v-for="n in 3">{{$index}} {{n}}</div>'
  597. })
  598. expect(el.innerHTML).toBe('<div>0 0</div><div>1 1</div><div>2 2</div>')
  599. })
  600. it('repeat string', function () {
  601. new Vue({
  602. el: el,
  603. template: '<div v-for="letter in \'vue\'">{{$index}} {{letter}}</div>'
  604. })
  605. expect(el.innerHTML).toBe('<div>0 v</div><div>1 u</div><div>2 e</div>')
  606. })
  607. it('teardown', function () {
  608. var vm = new Vue({
  609. el: el,
  610. template: '<div v-for="item in items"></div>',
  611. data: {
  612. items: [{a: 1}, {a: 2}]
  613. }
  614. })
  615. vm._directives[0].unbind()
  616. expect(vm.$children.length).toBe(0)
  617. })
  618. it('with transition', function (done) {
  619. document.body.appendChild(el)
  620. var vm = new Vue({
  621. el: el,
  622. template: '<div v-for="item in items" transition="test">{{item.a}}</div>',
  623. data: {
  624. items: [{a: 1}, {a: 2}, {a: 3}]
  625. },
  626. transitions: {
  627. test: {
  628. leave: function (el, done) {
  629. setTimeout(done, 0)
  630. }
  631. }
  632. }
  633. })
  634. vm.items.splice(1, 1, {a: 4})
  635. setTimeout(function () {
  636. expect(el.innerHTML).toBe(
  637. '<div class="test-transition">1</div>' +
  638. '<div class="test-transition">4</div>' +
  639. '<div class="test-transition">3</div>'
  640. )
  641. document.body.removeChild(el)
  642. done()
  643. }, 100)
  644. })
  645. it('v-model binding on alias', function () {
  646. var vm = new Vue({
  647. el: el,
  648. template:
  649. '<div v-for="val in items"><input v-model="val"></div>' +
  650. '<div v-for="val in obj"><input v-model="val"></div>',
  651. data: {
  652. items: ['a'],
  653. obj: { foo: 'a' }
  654. }
  655. })
  656. var a = getInput(1)
  657. a.value = 'b'
  658. trigger(a, 'input')
  659. expect(vm.items[0]).toBe('b')
  660. var b = getInput(2)
  661. b.value = 'bar'
  662. trigger(b, 'input')
  663. expect(vm.obj.foo).toBe('bar')
  664. function getInput (x) {
  665. return vm.$el.querySelector('div:nth-child(' + x + ') input')
  666. }
  667. })
  668. it('warn v-model on alias with filters', function () {
  669. var vm = new Vue({
  670. el: el,
  671. template:
  672. '<div v-for="item in items | orderBy \'item\'">' +
  673. '<input v-model="item">' +
  674. '</div>',
  675. data: {
  676. items: ['a', 'b']
  677. }
  678. })
  679. trigger(vm.$el.querySelector('input'), 'input')
  680. expect('It seems you are using two-way binding').toHaveBeenWarned()
  681. })
  682. it('nested track by', function (done) {
  683. var vm = new Vue({
  684. el: el,
  685. template:
  686. '<div v-for="item in list" track-by="id">' +
  687. '{{item.msg}}' +
  688. '<div v-for="subItem in item.list" track-by="id">' +
  689. '{{subItem.msg}}' +
  690. '</div>' +
  691. '</div>',
  692. data: {
  693. list: [
  694. { id: 1, msg: 'hi', list: [
  695. { id: 1, msg: 'hi foo' }
  696. ] },
  697. { id: 2, msg: 'ha', list: [] },
  698. { id: 3, msg: 'ho', list: [] }
  699. ]
  700. }
  701. })
  702. assertMarkup()
  703. var oldNodes = el.children
  704. var oldInnerNodes = el.children[0].children
  705. vm.list = [
  706. { id: 1, msg: 'wa', list: [
  707. { id: 1, msg: 'hi foo' },
  708. { id: 2, msg: 'hi bar' }
  709. ] },
  710. { id: 2, msg: 'wo', list: [] }
  711. ]
  712. _.nextTick(function () {
  713. assertMarkup()
  714. // should reuse old frags!
  715. var i = 2
  716. while (i--) {
  717. expect(el.children[i]).toBe(oldNodes[i])
  718. }
  719. expect(el.children[0].children[0]).toBe(oldInnerNodes[0])
  720. done()
  721. })
  722. function assertMarkup () {
  723. var markup = vm.list.map(function (item) {
  724. var sublist = item.list.map(function (item) {
  725. return '<div>' + item.msg + '</div>'
  726. }).join('')
  727. return '<div>' + item.msg + sublist + '</div>'
  728. }).join('')
  729. expect(el.innerHTML).toBe(markup)
  730. }
  731. })
  732. it('switch between object-converted & array mode', function (done) {
  733. var obj = {
  734. a: { msg: 'AA' },
  735. b: { msg: 'BB' }
  736. }
  737. var arr = [obj.b, obj.a]
  738. var vm = new Vue({
  739. el: el,
  740. template: '<div v-for="item in obj">{{item.msg}}</div>',
  741. data: {
  742. obj: obj
  743. }
  744. })
  745. expect(el.innerHTML).toBe(Object.keys(obj).map(function (key) {
  746. return '<div>' + obj[key].msg + '</div>'
  747. }).join(''))
  748. vm.obj = arr
  749. _.nextTick(function () {
  750. expect(el.innerHTML).toBe('<div>BB</div><div>AA</div>')
  751. // make sure it cleared the cache
  752. expect(vm._directives[0].cache.a).toBeNull()
  753. expect(vm._directives[0].cache.b).toBeNull()
  754. done()
  755. })
  756. })
  757. it('call attach/detach for contained components', function (done) {
  758. document.body.appendChild(el)
  759. var attachSpy = jasmine.createSpy('attach')
  760. var detachSpy = jasmine.createSpy('detach')
  761. var vm = new Vue({
  762. el: el,
  763. template: '<test v-for="item in items"></test>',
  764. data: {
  765. items: [1, 2]
  766. },
  767. components: {
  768. test: {
  769. attached: attachSpy,
  770. detached: detachSpy
  771. }
  772. }
  773. })
  774. expect(attachSpy.calls.count()).toBe(2)
  775. expect(detachSpy.calls.count()).toBe(0)
  776. vm.items.push(3)
  777. _.nextTick(function () {
  778. expect(attachSpy.calls.count()).toBe(3)
  779. expect(detachSpy.calls.count()).toBe(0)
  780. vm.items.pop()
  781. _.nextTick(function () {
  782. expect(attachSpy.calls.count()).toBe(3)
  783. expect(detachSpy.calls.count()).toBe(1)
  784. vm.items = []
  785. _.nextTick(function () {
  786. expect(attachSpy.calls.count()).toBe(3)
  787. expect(detachSpy.calls.count()).toBe(3)
  788. done()
  789. })
  790. })
  791. })
  792. })
  793. it('access parent\'s $refs', function () {
  794. var vm = new Vue({
  795. el: document.createElement('div'),
  796. template: '<c1 v-ref:c1><div v-for="n in 2">{{$refs.c1.d}}</div></c1>',
  797. components: {
  798. c1: {
  799. template: '<div><slot></slot></div>',
  800. data: function () {
  801. return {
  802. d: 1
  803. }
  804. }
  805. }
  806. }
  807. })
  808. expect(vm.$refs.c1 instanceof Vue).toBe(true)
  809. expect(vm.$refs.c1.$el.innerHTML).toContain('<div>1</div><div>1</div>')
  810. })
  811. it('access parent scope\'s $els', function (done) {
  812. var vm = new Vue({
  813. el: document.createElement('div'),
  814. template: '<div data-d=1 v-el:a><div v-for="n in 2">{{ready ? $els.a.getAttribute("data-d") : 0}}</div></div>',
  815. data: {
  816. ready: false
  817. }
  818. })
  819. expect(vm.$els.a.nodeType).toBe(1)
  820. expect(vm.$els.a.innerHTML).toContain('<div>0</div><div>0</div>')
  821. vm.ready = true
  822. vm.$nextTick(function () {
  823. expect(vm.$els.a.innerHTML).toContain('<div>1</div><div>1</div>')
  824. done()
  825. })
  826. })
  827. })
  828. /**
  829. * Simple helper for chained async asssertions
  830. *
  831. * @param {Function} fn - the data manipulation function
  832. * @param {Function} cb - the assertion fn to be called on nextTick
  833. */
  834. function go (fn, cb) {
  835. return {
  836. stack: [{fn: fn, cb: cb}],
  837. then: function (fn, cb) {
  838. this.stack.push({fn: fn, cb: cb})
  839. return this
  840. },
  841. run: function (done) {
  842. var self = this
  843. var step = this.stack.shift()
  844. if (!step) return done()
  845. step.fn()
  846. _.nextTick(function () {
  847. step.cb()
  848. self.run(done)
  849. })
  850. }
  851. }
  852. }
  853. /**
  854. * Assert mutation and markup correctness for v-for on
  855. * an Array of Objects
  856. */
  857. function assertMutations (vm, el, done) {
  858. assertMarkup()
  859. var poppedItem
  860. go(
  861. function () {
  862. vm.items.push({a: 3})
  863. },
  864. assertMarkup
  865. )
  866. .then(
  867. function () {
  868. vm.items.push(vm.items.shift())
  869. },
  870. assertMarkup
  871. )
  872. .then(
  873. function () {
  874. vm.items.reverse()
  875. },
  876. assertMarkup
  877. )
  878. .then(
  879. function () {
  880. poppedItem = vm.items.pop()
  881. },
  882. assertMarkup
  883. )
  884. .then(
  885. function () {
  886. vm.items.unshift(poppedItem)
  887. },
  888. assertMarkup
  889. )
  890. .then(
  891. function () {
  892. vm.items.sort(function (a, b) {
  893. return a.a > b.a ? 1 : -1
  894. })
  895. },
  896. assertMarkup
  897. )
  898. .then(
  899. function () {
  900. vm.items.splice(1, 1, {a: 5})
  901. },
  902. assertMarkup
  903. )
  904. // test swapping the array
  905. .then(
  906. function () {
  907. vm.items = [{a: 0}, {a: 1}, {a: 2}]
  908. },
  909. assertMarkup
  910. )
  911. .run(done)
  912. function assertMarkup () {
  913. var tag = el.children[0].tagName.toLowerCase()
  914. var markup = vm.items.map(function (item, i) {
  915. var el = '<' + tag + '>' + i + ' ' + item.a + '</' + tag + '>'
  916. return el
  917. }).join('')
  918. expect(el.innerHTML).toBe(markup)
  919. }
  920. }
  921. /**
  922. * Assert mutation and markup correctness for v-for on
  923. * an Array of primitive values
  924. */
  925. function assertPrimitiveMutations (vm, el, done) {
  926. assertMarkup()
  927. go(
  928. function () {
  929. vm.items.push(4)
  930. },
  931. assertMarkup
  932. )
  933. .then(
  934. function () {
  935. vm.items.shift()
  936. },
  937. assertMarkup
  938. )
  939. .then(
  940. function () {
  941. vm.items.reverse()
  942. },
  943. assertMarkup
  944. )
  945. .then(
  946. function () {
  947. vm.items.pop()
  948. },
  949. assertMarkup
  950. )
  951. .then(
  952. function () {
  953. vm.items.unshift(1)
  954. },
  955. assertMarkup
  956. )
  957. .then(
  958. function () {
  959. vm.items.sort(function (a, b) {
  960. return a > b ? 1 : -1
  961. })
  962. },
  963. assertMarkup
  964. )
  965. .then(
  966. function () {
  967. vm.items.splice(1, 1, 5)
  968. },
  969. assertMarkup
  970. )
  971. // test swapping the array
  972. .then(
  973. function () {
  974. vm.items = [1, 2, 3]
  975. },
  976. assertMarkup
  977. )
  978. .run(done)
  979. function assertMarkup () {
  980. var markup = vm.items.map(function (item, i) {
  981. return '<div>' + i + ' ' + item + '</div>'
  982. }).join('')
  983. expect(el.innerHTML).toBe(markup)
  984. }
  985. }
  986. /**
  987. * Assert mutation and markup correctness for v-for on
  988. * an Array of primitive values when using track-by="$index"
  989. */
  990. function assertPrimitiveMutationsWithDuplicates (vm, el, done) {
  991. assertMarkup()
  992. go(
  993. function () {
  994. vm.items.push(2, 2, 3)
  995. },
  996. assertMarkup
  997. )
  998. .then(
  999. function () {
  1000. vm.items.shift()
  1001. },
  1002. assertMarkup
  1003. )
  1004. .then(
  1005. function () {
  1006. vm.items.reverse()
  1007. },
  1008. assertMarkup
  1009. )
  1010. .then(
  1011. function () {
  1012. vm.items.pop()
  1013. },
  1014. assertMarkup
  1015. )
  1016. .then(
  1017. function () {
  1018. vm.items.unshift(3)
  1019. },
  1020. assertMarkup
  1021. )
  1022. .then(
  1023. function () {
  1024. vm.items.sort(function (a, b) {
  1025. return a > b ? 1 : -1
  1026. })
  1027. },
  1028. assertMarkup
  1029. )
  1030. .then(
  1031. function () {
  1032. vm.items.splice(1, 1, 5)
  1033. },
  1034. assertMarkup
  1035. )
  1036. // test swapping the array
  1037. .then(
  1038. function () {
  1039. vm.items = [1, 2, 2]
  1040. },
  1041. assertMarkup
  1042. )
  1043. .run(done)
  1044. function assertMarkup () {
  1045. var markup = vm.items.map(function (item, i) {
  1046. return '<div>' + i + ' ' + item + '</div>'
  1047. }).join('')
  1048. expect(el.innerHTML).toBe(markup)
  1049. }
  1050. }
  1051. /**
  1052. * Assert mutation and markup correctness for v-for on
  1053. * an Object of Objects
  1054. */
  1055. function assertObjectMutations (vm, el, done) {
  1056. assertMarkup()
  1057. go(
  1058. function () {
  1059. vm.items.a = {a: 3}
  1060. },
  1061. assertMarkup
  1062. )
  1063. .then(
  1064. function () {
  1065. vm.items = {
  1066. c: {a: 1},
  1067. d: {a: 2}
  1068. }
  1069. },
  1070. assertMarkup
  1071. )
  1072. .then(
  1073. function () {
  1074. _.set(vm.items, 'a', {a: 3})
  1075. },
  1076. assertMarkup
  1077. )
  1078. .run(done)
  1079. function assertMarkup () {
  1080. var markup = Object.keys(vm.items).map(function (key, i) {
  1081. return '<div>' + i + ' ' + key + ' ' + vm.items[key].a + '</div>'
  1082. }).join('')
  1083. expect(el.innerHTML).toBe(markup)
  1084. }
  1085. }
  1086. /**
  1087. * Assert mutation and markup correctness for v-for on
  1088. * an Object of primitive values
  1089. */
  1090. function assertObjectPrimitiveMutations (vm, el, done) {
  1091. assertMarkup()
  1092. go(
  1093. function () {
  1094. vm.items.a = 3
  1095. },
  1096. assertMarkup
  1097. )
  1098. .then(
  1099. function () {
  1100. vm.items = {
  1101. c: 1,
  1102. d: 2
  1103. }
  1104. },
  1105. assertMarkup
  1106. )
  1107. .then(
  1108. function () {
  1109. _.set(vm.items, 'a', 3)
  1110. },
  1111. assertMarkup
  1112. )
  1113. .run(done)
  1114. function assertMarkup () {
  1115. var markup = Object.keys(vm.items).map(function (key, i) {
  1116. return '<div>' + i + ' ' + key + ' ' + vm.items[key] + '</div>'
  1117. }).join('')
  1118. expect(el.innerHTML).toBe(markup)
  1119. }
  1120. }
  1121. /**
  1122. * Helper for triggering events
  1123. */
  1124. function trigger (target, event, process) {
  1125. var e = document.createEvent('HTMLEvents')
  1126. e.initEvent(event, true, true)
  1127. if (process) process(e)
  1128. target.dispatchEvent(e)
  1129. return e
  1130. }