prop_spec.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. var Vue = require('src')
  2. describe('prop', function () {
  3. var el
  4. beforeEach(function () {
  5. el = document.createElement('div')
  6. })
  7. it('one way binding', function (done) {
  8. var vm = new Vue({
  9. el: el,
  10. data: {
  11. b: 'bar'
  12. },
  13. template: '<test v-bind:b="b" v-ref:child></test>',
  14. components: {
  15. test: {
  16. props: ['b'],
  17. template: '{{b}}'
  18. }
  19. }
  20. })
  21. expect(el.innerHTML).toBe('<test>bar</test>')
  22. vm.b = 'baz'
  23. Vue.nextTick(function () {
  24. expect(el.innerHTML).toBe('<test>baz</test>')
  25. vm.$refs.child.b = 'qux'
  26. expect(vm.b).toBe('baz')
  27. Vue.nextTick(function () {
  28. expect(el.innerHTML).toBe('<test>qux</test>')
  29. done()
  30. })
  31. })
  32. })
  33. it('with filters', function (done) {
  34. var vm = new Vue({
  35. el: el,
  36. template: '<test :name="a | test"></test>',
  37. data: {
  38. a: 123
  39. },
  40. filters: {
  41. test: function (v) {
  42. return v * 2
  43. }
  44. },
  45. components: {
  46. test: {
  47. props: ['name'],
  48. template: '{{name}}'
  49. }
  50. }
  51. })
  52. expect(el.textContent).toBe('246')
  53. vm.a = 234
  54. Vue.nextTick(function () {
  55. expect(el.textContent).toBe('468')
  56. done()
  57. })
  58. })
  59. it('two-way binding', function (done) {
  60. var vm = new Vue({
  61. el: el,
  62. data: {
  63. b: 'B',
  64. test: {
  65. a: 'A'
  66. }
  67. },
  68. template: '<test v-bind:testt.sync="test" :bb.sync="b" :a.sync=" test.a " v-ref:child></test>',
  69. components: {
  70. test: {
  71. props: ['testt', 'bb', 'a'],
  72. template: '{{testt.a}} {{bb}} {{a}}'
  73. }
  74. }
  75. })
  76. expect(el.firstChild.textContent).toBe('A B A')
  77. vm.test.a = 'AA'
  78. vm.b = 'BB'
  79. Vue.nextTick(function () {
  80. expect(el.firstChild.textContent).toBe('AA BB AA')
  81. vm.test = { a: 'foo' }
  82. Vue.nextTick(function () {
  83. expect(el.firstChild.textContent).toBe('foo BB foo')
  84. vm.$data = {
  85. b: 'bar',
  86. test: {
  87. a: 'fooA'
  88. }
  89. }
  90. Vue.nextTick(function () {
  91. expect(el.firstChild.textContent).toBe('fooA bar fooA')
  92. // test two-way
  93. vm.$refs.child.bb = 'B'
  94. vm.$refs.child.testt = { a: 'A' }
  95. Vue.nextTick(function () {
  96. expect(el.firstChild.textContent).toBe('A B A')
  97. expect(vm.test.a).toBe('A')
  98. expect(vm.test).toBe(vm.$refs.child.testt)
  99. expect(vm.b).toBe('B')
  100. vm.$refs.child.a = 'Oops'
  101. Vue.nextTick(function () {
  102. expect(el.firstChild.textContent).toBe('Oops B Oops')
  103. expect(vm.test.a).toBe('Oops')
  104. done()
  105. })
  106. })
  107. })
  108. })
  109. })
  110. })
  111. it('explicit one time binding', function (done) {
  112. var vm = new Vue({
  113. el: el,
  114. data: {
  115. b: 'foo'
  116. },
  117. template: '<test :b.once="b" v-ref:child></test>',
  118. components: {
  119. test: {
  120. props: ['b'],
  121. template: '{{b}}'
  122. }
  123. }
  124. })
  125. expect(el.innerHTML).toBe('<test>foo</test>')
  126. vm.b = 'bar'
  127. Vue.nextTick(function () {
  128. expect(el.innerHTML).toBe('<test>foo</test>')
  129. done()
  130. })
  131. })
  132. it('warn non-settable parent path', function (done) {
  133. var vm = new Vue({
  134. el: el,
  135. data: {
  136. b: 'foo'
  137. },
  138. template: '<test :b.sync=" b + \'bar\'" v-ref:child></test>',
  139. components: {
  140. test: {
  141. props: ['b'],
  142. template: '{{b}}'
  143. }
  144. }
  145. })
  146. expect('Cannot bind two-way prop with non-settable parent path').toHaveBeenWarned()
  147. expect(el.innerHTML).toBe('<test>foobar</test>')
  148. vm.b = 'baz'
  149. Vue.nextTick(function () {
  150. expect(el.innerHTML).toBe('<test>bazbar</test>')
  151. vm.$refs.child.b = 'qux'
  152. Vue.nextTick(function () {
  153. expect(vm.b).toBe('baz')
  154. expect(el.innerHTML).toBe('<test>qux</test>')
  155. done()
  156. })
  157. })
  158. })
  159. it('warn expect two-way', function () {
  160. new Vue({
  161. el: el,
  162. template: '<test :test="foo"></test>',
  163. data: {
  164. foo: 'bar'
  165. },
  166. components: {
  167. test: {
  168. props: {
  169. test: {
  170. twoWay: true
  171. }
  172. }
  173. }
  174. }
  175. })
  176. expect('expects a two-way binding type').toHaveBeenWarned()
  177. })
  178. it('warn $data as prop', function () {
  179. new Vue({
  180. el: el,
  181. template: '<test></test>',
  182. data: {
  183. foo: 'bar'
  184. },
  185. components: {
  186. test: {
  187. props: ['$data']
  188. }
  189. }
  190. })
  191. expect('Do not use $data as prop').toHaveBeenWarned()
  192. })
  193. it('warn invalid keys', function () {
  194. new Vue({
  195. el: el,
  196. template: '<test :a.b.c="test"></test>',
  197. components: {
  198. test: {
  199. props: ['a.b.c']
  200. }
  201. }
  202. })
  203. expect('Invalid prop key').toHaveBeenWarned()
  204. })
  205. it('warn props with no el option', function () {
  206. new Vue({
  207. props: ['a']
  208. })
  209. expect('Props will not be compiled if no `el`').toHaveBeenWarned()
  210. })
  211. it('warn object/array default values', function () {
  212. new Vue({
  213. el: el,
  214. props: {
  215. arr: {
  216. type: Array,
  217. default: []
  218. },
  219. obj: {
  220. type: Object,
  221. default: {}
  222. }
  223. }
  224. })
  225. expect('use a factory function to return the default value').toHaveBeenWarned()
  226. expect(getWarnCount()).toBe(2)
  227. })
  228. it('teardown', function (done) {
  229. var vm = new Vue({
  230. el: el,
  231. data: {
  232. a: 'A',
  233. b: 'B'
  234. },
  235. template: '<test :aa.sync="a" :bb="b"></test>',
  236. components: {
  237. test: {
  238. props: ['aa', 'bb'],
  239. template: '{{aa}} {{bb}}'
  240. }
  241. }
  242. })
  243. var child = vm.$children[0]
  244. expect(el.firstChild.textContent).toBe('A B')
  245. child.aa = 'AA'
  246. vm.b = 'BB'
  247. Vue.nextTick(function () {
  248. expect(el.firstChild.textContent).toBe('AA BB')
  249. expect(vm.a).toBe('AA')
  250. // unbind the two props
  251. child._directives[0].unbind()
  252. child._directives[1].unbind()
  253. child.aa = 'foo'
  254. vm.b = 'BBB'
  255. Vue.nextTick(function () {
  256. expect(el.firstChild.textContent).toBe('foo BB')
  257. expect(vm.a).toBe('AA')
  258. done()
  259. })
  260. })
  261. })
  262. it('block instance with replace:true', function () {
  263. new Vue({
  264. el: el,
  265. template: '<test :b="a" :c="d"></test>',
  266. data: {
  267. a: 'foo',
  268. d: 'bar'
  269. },
  270. components: {
  271. test: {
  272. props: ['b', 'c'],
  273. template: '<p>{{b}}</p><p>{{c}}</p>',
  274. replace: true
  275. }
  276. }
  277. })
  278. expect(el.innerHTML).toBe('<p>foo</p><p>bar</p>')
  279. })
  280. describe('assertions', function () {
  281. function makeInstance (value, type, validator, coerce, required) {
  282. return new Vue({
  283. el: document.createElement('div'),
  284. template: '<test :test="val"></test>',
  285. data: {
  286. val: value
  287. },
  288. components: {
  289. test: {
  290. props: {
  291. test: {
  292. type: type,
  293. validator: validator,
  294. coerce: coerce,
  295. required: required
  296. }
  297. }
  298. }
  299. }
  300. })
  301. }
  302. it('string', function () {
  303. makeInstance('hello', String)
  304. expect(getWarnCount()).toBe(0)
  305. makeInstance(123, String)
  306. expect('Expected String').toHaveBeenWarned()
  307. })
  308. it('number', function () {
  309. makeInstance(123, Number)
  310. expect(getWarnCount()).toBe(0)
  311. makeInstance('123', Number)
  312. expect('Expected Number').toHaveBeenWarned()
  313. })
  314. it('boolean', function () {
  315. makeInstance(true, Boolean)
  316. expect(getWarnCount()).toBe(0)
  317. makeInstance('123', Boolean)
  318. expect('Expected Boolean').toHaveBeenWarned()
  319. })
  320. it('function', function () {
  321. makeInstance(function () {}, Function)
  322. expect(getWarnCount()).toBe(0)
  323. makeInstance(123, Function)
  324. expect('Expected Function').toHaveBeenWarned()
  325. })
  326. it('object', function () {
  327. makeInstance({}, Object)
  328. expect(getWarnCount()).toBe(0)
  329. makeInstance([], Object)
  330. expect('Expected Object').toHaveBeenWarned()
  331. })
  332. it('array', function () {
  333. makeInstance([], Array)
  334. expect(getWarnCount()).toBe(0)
  335. makeInstance({}, Array)
  336. expect('Expected Array').toHaveBeenWarned()
  337. })
  338. it('custom constructor', function () {
  339. function Class () {}
  340. makeInstance(new Class(), Class)
  341. expect(getWarnCount()).toBe(0)
  342. makeInstance({}, Class)
  343. expect('Expected custom type').toHaveBeenWarned()
  344. })
  345. it('multiple types', function () {
  346. makeInstance([], [Array, Number, Boolean])
  347. expect(getWarnCount()).toBe(0)
  348. makeInstance({}, [Array, Number, Boolean])
  349. expect('Expected Array, Number, Boolean').toHaveBeenWarned()
  350. })
  351. it('custom validator', function () {
  352. makeInstance(123, null, function (v) {
  353. return v === 123
  354. })
  355. expect(getWarnCount()).toBe(0)
  356. makeInstance(123, null, function (v) {
  357. return v === 234
  358. })
  359. expect('custom validator check failed').toHaveBeenWarned()
  360. })
  361. it('type check + custom validator', function () {
  362. makeInstance(123, Number, function (v) {
  363. return v === 123
  364. })
  365. expect(getWarnCount()).toBe(0)
  366. makeInstance(123, Number, function (v) {
  367. return v === 234
  368. })
  369. expect('custom validator check failed').toHaveBeenWarned()
  370. makeInstance(123, String, function (v) {
  371. return v === 123
  372. })
  373. expect('Expected String').toHaveBeenWarned()
  374. })
  375. it('multiple types + custom validator', function () {
  376. makeInstance(123, [Number, String, Boolean], function (v) {
  377. return v === 123
  378. })
  379. expect(getWarnCount()).toBe(0)
  380. makeInstance(123, [Number, String, Boolean], function (v) {
  381. return v === 234
  382. })
  383. expect('custom validator check failed').toHaveBeenWarned()
  384. makeInstance(123, [String, Boolean], function (v) {
  385. return v === 123
  386. })
  387. expect('Expected String, Boolean').toHaveBeenWarned()
  388. })
  389. it('type check + coerce', function () {
  390. makeInstance(123, String, null, String)
  391. expect(getWarnCount()).toBe(0)
  392. makeInstance('123', Number, null, Number)
  393. expect(getWarnCount()).toBe(0)
  394. makeInstance('123', Boolean, null, function (val) {
  395. return val === '123'
  396. })
  397. expect(getWarnCount()).toBe(0)
  398. })
  399. it('multiple types + custom validator', function () {
  400. makeInstance(123, [String, Boolean, Number], null, String)
  401. expect(getWarnCount()).toBe(0)
  402. makeInstance('123', [String, Boolean, Number], null, Number)
  403. expect(getWarnCount()).toBe(0)
  404. makeInstance('123', [String, Boolean, Function], null, function (val) {
  405. return val === '123'
  406. })
  407. expect(getWarnCount()).toBe(0)
  408. })
  409. it('required', function () {
  410. new Vue({
  411. el: document.createElement('div'),
  412. template: '<test></test>',
  413. components: {
  414. test: {
  415. props: {
  416. prop: { required: true }
  417. }
  418. }
  419. }
  420. })
  421. expect('Missing required prop').toHaveBeenWarned()
  422. })
  423. it('optional with type + null/undefined', function () {
  424. makeInstance(undefined, String)
  425. expect(getWarnCount()).toBe(0)
  426. makeInstance(null, String)
  427. expect(getWarnCount()).toBe(0)
  428. })
  429. it('required with type + null/undefined', function () {
  430. makeInstance(undefined, String, null, null, true)
  431. expect(getWarnCount()).toBe(1)
  432. expect('Expected String').toHaveBeenWarned()
  433. makeInstance(null, Boolean, null, null, true)
  434. expect(getWarnCount()).toBe(2)
  435. expect('Expected Boolean').toHaveBeenWarned()
  436. })
  437. })
  438. it('alternative syntax', function () {
  439. new Vue({
  440. el: el,
  441. template: '<test :b="a" :c="d"></test>',
  442. data: {
  443. a: 'foo',
  444. d: 'bar'
  445. },
  446. components: {
  447. test: {
  448. props: {
  449. b: String,
  450. c: {
  451. type: Number
  452. },
  453. d: {
  454. required: true
  455. }
  456. },
  457. template: '<p>{{b}}</p><p>{{c}}</p>'
  458. }
  459. }
  460. })
  461. expect('Missing required prop').toHaveBeenWarned()
  462. expect('Expected Number').toHaveBeenWarned()
  463. expect(el.textContent).toBe('foo')
  464. })
  465. it('mixed syntax', function () {
  466. new Vue({
  467. el: el,
  468. template: '<test :b="a" :c="d"></test>',
  469. data: {
  470. a: 'foo',
  471. d: 'bar'
  472. },
  473. components: {
  474. test: {
  475. props: [
  476. 'b',
  477. {
  478. name: 'c',
  479. type: Number
  480. },
  481. {
  482. name: 'd',
  483. required: true
  484. }
  485. ],
  486. template: '<p>{{b}}</p><p>{{c}}</p>'
  487. }
  488. }
  489. })
  490. expect('Missing required prop').toHaveBeenWarned()
  491. expect('Expected Number').toHaveBeenWarned()
  492. expect(el.textContent).toBe('foo')
  493. })
  494. it('should respect default value of a Boolean prop', function () {
  495. var vm = new Vue({
  496. el: el,
  497. template: '<test></test>',
  498. components: {
  499. test: {
  500. props: {
  501. prop: {
  502. type: Boolean,
  503. default: true
  504. }
  505. },
  506. template: '{{prop}}'
  507. }
  508. }
  509. })
  510. expect(vm.$el.textContent).toBe('true')
  511. })
  512. it('should initialize with default value when not provided & has default data', function (done) {
  513. var vm = new Vue({
  514. el: el,
  515. template: '<test></test>',
  516. components: {
  517. test: {
  518. props: {
  519. prop: {
  520. type: String,
  521. default: 'hello'
  522. },
  523. prop2: {
  524. type: Object,
  525. default: function () {
  526. return { vm: this }
  527. }
  528. }
  529. },
  530. data: function () {
  531. return {
  532. other: 'world'
  533. }
  534. },
  535. template: '{{prop}} {{other}}'
  536. }
  537. }
  538. })
  539. expect(vm.$el.textContent).toBe('hello world')
  540. // object/array default value initializers should be
  541. // called with the correct `this` context
  542. var child = vm.$children[0]
  543. expect(child.prop2.vm).toBe(child)
  544. vm.$children[0].prop = 'bye'
  545. Vue.nextTick(function () {
  546. expect(vm.$el.textContent).toBe('bye world')
  547. done()
  548. })
  549. })
  550. it('should warn data fields already defined as a prop', function () {
  551. var Comp = Vue.extend({
  552. data: function () {
  553. return { a: 123 }
  554. },
  555. props: {
  556. a: null
  557. }
  558. })
  559. new Vue({
  560. el: el,
  561. template: '<comp a="1"></comp>',
  562. components: {
  563. comp: Comp
  564. }
  565. })
  566. expect('already defined as a prop').toHaveBeenWarned()
  567. })
  568. it('propsData options', function () {
  569. var vm = new Vue({
  570. el: el,
  571. props: {
  572. a: null
  573. },
  574. propsData: {
  575. a: 123
  576. }
  577. })
  578. expect(getWarnCount()).toBe(0)
  579. expect(vm.a).toBe(123)
  580. })
  581. it('should warn using propsData during extension', function () {
  582. Vue.extend({
  583. propsData: {
  584. a: 123
  585. }
  586. })
  587. expect('propsData can only be used as an instantiation option').toHaveBeenWarned()
  588. })
  589. it('should not warn for non-required, absent prop', function () {
  590. new Vue({
  591. el: el,
  592. template: '<test></test>',
  593. components: {
  594. test: {
  595. props: {
  596. prop: {
  597. type: String
  598. }
  599. }
  600. }
  601. }
  602. })
  603. expect(getWarnCount()).toBe(0)
  604. })
  605. // #1683
  606. it('should properly sync back up when mutating then replace', function (done) {
  607. var vm = new Vue({
  608. el: el,
  609. data: {
  610. items: [1, 2]
  611. },
  612. template: '<comp :items.sync="items"></comp>',
  613. components: {
  614. comp: {
  615. props: ['items']
  616. }
  617. }
  618. })
  619. var child = vm.$children[0]
  620. child.items.push(3)
  621. var newArray = child.items = [4]
  622. Vue.nextTick(function () {
  623. expect(child.items).toBe(newArray)
  624. expect(vm.items).toBe(newArray)
  625. done()
  626. })
  627. })
  628. it('treat boolean props properly', function () {
  629. var vm = new Vue({
  630. el: el,
  631. template: '<comp v-ref:child prop-a prop-b="prop-b"></comp>',
  632. components: {
  633. comp: {
  634. props: {
  635. propA: Boolean,
  636. propB: Boolean,
  637. propC: Boolean
  638. }
  639. }
  640. }
  641. })
  642. expect(vm.$refs.child.propA).toBe(true)
  643. expect(vm.$refs.child.propB).toBe(true)
  644. expect(vm.$refs.child.propC).toBe(false)
  645. })
  646. it('detect possible camelCase prop usage', function () {
  647. new Vue({
  648. el: el,
  649. template: '<comp propA="true" :propB="true" v-bind:propC.sync="true"></comp>',
  650. components: {
  651. comp: {
  652. props: ['propA', 'propB', 'prop-c']
  653. }
  654. }
  655. })
  656. expect(getWarnCount()).toBe(3)
  657. expect('did you mean `prop-a`').toHaveBeenWarned()
  658. expect('did you mean `prop-b`').toHaveBeenWarned()
  659. expect('did you mean `prop-c`').toHaveBeenWarned()
  660. })
  661. it('should use default for undefined values', function (done) {
  662. var vm = new Vue({
  663. el: el,
  664. template: '<comp :a="a"></comp>',
  665. data: {
  666. a: undefined
  667. },
  668. components: {
  669. comp: {
  670. template: '{{a}}',
  671. props: {
  672. a: {
  673. default: 1
  674. }
  675. }
  676. }
  677. }
  678. })
  679. expect(vm.$el.textContent).toBe('1')
  680. vm.a = 2
  681. Vue.nextTick(function () {
  682. expect(vm.$el.textContent).toBe('2')
  683. vm.a = undefined
  684. Vue.nextTick(function () {
  685. expect(vm.$el.textContent).toBe('1')
  686. done()
  687. })
  688. })
  689. })
  690. it('non reactive values passed down as prop should not be converted', function (done) {
  691. var a = Object.freeze({
  692. nested: {
  693. msg: 'hello'
  694. }
  695. })
  696. var parent = new Vue({
  697. el: el,
  698. template: '<comp :a="a.nested"></comp>',
  699. data: {
  700. a: a
  701. },
  702. components: {
  703. comp: {
  704. props: ['a']
  705. }
  706. }
  707. })
  708. var child = parent.$children[0]
  709. expect(child.a.msg).toBe('hello')
  710. expect(child.a.__ob__).toBeUndefined() // should not be converted
  711. parent.a = Object.freeze({
  712. nested: {
  713. msg: 'yo'
  714. }
  715. })
  716. Vue.nextTick(function () {
  717. expect(child.a.msg).toBe('yo')
  718. expect(child.a.__ob__).toBeUndefined()
  719. done()
  720. })
  721. })
  722. it('inline prop values should be converted', function (done) {
  723. var vm = new Vue({
  724. el: el,
  725. template: '<comp :a="[1, 2, 3]"></comp>',
  726. components: {
  727. comp: {
  728. props: ['a'],
  729. template: '<div v-for="i in a">{{ i }}</div>'
  730. }
  731. }
  732. })
  733. expect(vm.$el.textContent).toBe('123')
  734. vm.$children[0].a.pop()
  735. Vue.nextTick(function () {
  736. expect(vm.$el.textContent).toBe('12')
  737. done()
  738. })
  739. })
  740. // #2549
  741. it('mutating child prop binding should be reactive', function (done) {
  742. var vm = new Vue({
  743. el: el,
  744. template: '<comp :list="list"></comp>',
  745. data: {
  746. list: [1, 2, 3]
  747. },
  748. components: {
  749. comp: {
  750. props: ['list'],
  751. template: '<div v-for="i in list">{{ i }}</div>',
  752. created: function () {
  753. this.list = [2, 3, 4]
  754. }
  755. }
  756. }
  757. })
  758. expect(vm.$el.textContent).toBe('234')
  759. vm.$children[0].list.push(5)
  760. Vue.nextTick(function () {
  761. expect(vm.$el.textContent).toBe('2345')
  762. done()
  763. })
  764. })
  765. it('prop default value should be reactive', function (done) {
  766. var vm = new Vue({
  767. el: el,
  768. template: '<comp :list="list"></comp>',
  769. data: {
  770. list: undefined
  771. },
  772. components: {
  773. comp: {
  774. props: {
  775. list: {
  776. default: function () {
  777. return [2, 3, 4]
  778. }
  779. }
  780. },
  781. template: '<div v-for="i in list">{{ i }}</div>'
  782. }
  783. }
  784. })
  785. expect(vm.$el.textContent).toBe('234')
  786. vm.$children[0].list.push(5)
  787. Vue.nextTick(function () {
  788. expect(vm.$el.textContent).toBe('2345')
  789. done()
  790. })
  791. })
  792. it('prop coerced value should be reactive', function (done) {
  793. var vm = new Vue({
  794. el: el,
  795. template: '<comp :obj="obj"></comp>',
  796. data: {
  797. obj: { ok: true }
  798. },
  799. components: {
  800. comp: {
  801. props: {
  802. obj: {
  803. coerce: function () {
  804. return { ok: false }
  805. }
  806. }
  807. },
  808. template: '<div>{{ obj.ok }}</div>'
  809. }
  810. }
  811. })
  812. expect(vm.$el.textContent).toBe('false')
  813. vm.$children[0].obj.ok = true
  814. Vue.nextTick(function () {
  815. expect(vm.$el.textContent).toBe('true')
  816. done()
  817. })
  818. })
  819. it('prop coercion should be applied after defaulting', function () {
  820. var vm = new Vue({
  821. el: el,
  822. template: '<comp></comp>',
  823. components: {
  824. comp: {
  825. props: {
  826. color: {
  827. type: String,
  828. default: 'blue',
  829. coerce: function (color) {
  830. return 'color-' + color
  831. }
  832. }
  833. },
  834. template: '<div>{{ color }}</div>'
  835. }
  836. }
  837. })
  838. expect(vm.$el.textContent).toBe('color-blue')
  839. })
  840. })