for_spec.js 27 KB

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