prop_spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. var _ = require('../../../../../src/util')
  2. var Vue = require('../../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('prop', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. it('one way binding', function (done) {
  11. var vm = new Vue({
  12. el: el,
  13. data: {
  14. b: 'B'
  15. },
  16. template: '<test v-bind:b="b" v-ref:child></test>',
  17. components: {
  18. test: {
  19. props: ['b'],
  20. template: '{{b}}'
  21. }
  22. }
  23. })
  24. expect(el.innerHTML).toBe('<test>B</test>')
  25. vm.b = 'BB'
  26. _.nextTick(function () {
  27. expect(el.innerHTML).toBe('<test>BB</test>')
  28. vm.$refs.child.b = 'BBB'
  29. expect(vm.b).toBe('BB')
  30. _.nextTick(function () {
  31. expect(el.innerHTML).toBe('<test>BBB</test>')
  32. done()
  33. })
  34. })
  35. })
  36. it('with filters', function (done) {
  37. var vm = new Vue({
  38. el: el,
  39. template: '<test :name="a | test"></test>',
  40. data: {
  41. a: 123
  42. },
  43. filters: {
  44. test: function (v) {
  45. return v * 2
  46. }
  47. },
  48. components: {
  49. test: {
  50. props: ['name'],
  51. template: '{{name}}'
  52. }
  53. }
  54. })
  55. expect(el.textContent).toBe('246')
  56. vm.a = 234
  57. _.nextTick(function () {
  58. expect(el.textContent).toBe('468')
  59. done()
  60. })
  61. })
  62. it('two-way binding', function (done) {
  63. var vm = new Vue({
  64. el: el,
  65. data: {
  66. b: 'B',
  67. test: {
  68. a: 'A'
  69. }
  70. },
  71. template: '<test v-bind:testt.sync="test" :bb.sync="b" :a.sync=" test.a " v-ref:child></test>',
  72. components: {
  73. test: {
  74. props: ['testt', 'bb', 'a'],
  75. template: '{{testt.a}} {{bb}} {{a}}'
  76. }
  77. }
  78. })
  79. expect(el.firstChild.textContent).toBe('A B A')
  80. vm.test.a = 'AA'
  81. vm.b = 'BB'
  82. _.nextTick(function () {
  83. expect(el.firstChild.textContent).toBe('AA BB AA')
  84. vm.test = { a: 'AAA' }
  85. _.nextTick(function () {
  86. expect(el.firstChild.textContent).toBe('AAA BB AAA')
  87. vm.$data = {
  88. b: 'BBB',
  89. test: {
  90. a: 'AAAA'
  91. }
  92. }
  93. _.nextTick(function () {
  94. expect(el.firstChild.textContent).toBe('AAAA BBB AAAA')
  95. // test two-way
  96. vm.$refs.child.bb = 'B'
  97. vm.$refs.child.testt = { a: 'A' }
  98. _.nextTick(function () {
  99. expect(el.firstChild.textContent).toBe('A B A')
  100. expect(vm.test.a).toBe('A')
  101. expect(vm.test).toBe(vm.$refs.child.testt)
  102. expect(vm.b).toBe('B')
  103. vm.$refs.child.a = 'Oops'
  104. _.nextTick(function () {
  105. expect(el.firstChild.textContent).toBe('Oops B Oops')
  106. expect(vm.test.a).toBe('Oops')
  107. done()
  108. })
  109. })
  110. })
  111. })
  112. })
  113. })
  114. it('explicit one time binding', function (done) {
  115. var vm = new Vue({
  116. el: el,
  117. data: {
  118. b: 'B'
  119. },
  120. template: '<test :b.once="b" v-ref:child></test>',
  121. components: {
  122. test: {
  123. props: ['b'],
  124. template: '{{b}}'
  125. }
  126. }
  127. })
  128. expect(el.innerHTML).toBe('<test>B</test>')
  129. vm.b = 'BB'
  130. _.nextTick(function () {
  131. expect(el.innerHTML).toBe('<test>B</test>')
  132. done()
  133. })
  134. })
  135. it('warn non-settable parent path', function (done) {
  136. var vm = new Vue({
  137. el: el,
  138. data: {
  139. b: 'B'
  140. },
  141. template: '<test :b.sync=" b + \'B\'" v-ref:child></test>',
  142. components: {
  143. test: {
  144. props: ['b'],
  145. template: '{{b}}'
  146. }
  147. }
  148. })
  149. expect(hasWarned(_, 'Cannot bind two-way prop with non-settable parent path')).toBe(true)
  150. expect(el.innerHTML).toBe('<test>BB</test>')
  151. vm.b = 'BB'
  152. _.nextTick(function () {
  153. expect(el.innerHTML).toBe('<test>BBB</test>')
  154. vm.$refs.child.b = 'hahaha'
  155. _.nextTick(function () {
  156. expect(vm.b).toBe('BB')
  157. expect(el.innerHTML).toBe('<test>hahaha</test>')
  158. done()
  159. })
  160. })
  161. })
  162. it('warn expect two-way', function () {
  163. new Vue({
  164. el: el,
  165. template: '<test :test="ok"></test>',
  166. data: {
  167. ok: 'hi'
  168. },
  169. components: {
  170. test: {
  171. props: {
  172. test: {
  173. twoWay: true
  174. }
  175. }
  176. }
  177. }
  178. })
  179. expect(hasWarned(_, 'expects a two-way binding type')).toBe(true)
  180. })
  181. it('warn $data as prop', function () {
  182. new Vue({
  183. el: el,
  184. template: '<test></test>',
  185. data: {
  186. ok: 'hi'
  187. },
  188. components: {
  189. test: {
  190. props: ['$data']
  191. }
  192. }
  193. })
  194. expect(hasWarned(_, 'Do not use $data as prop')).toBe(true)
  195. })
  196. it('warn invalid keys', function () {
  197. new Vue({
  198. el: el,
  199. template: '<test :a.b.c="test"></test>',
  200. components: {
  201. test: {
  202. props: ['a.b.c']
  203. }
  204. }
  205. })
  206. expect(hasWarned(_, 'Invalid prop key')).toBe(true)
  207. })
  208. it('warn props with no el option', function () {
  209. new Vue({
  210. props: ['a']
  211. })
  212. expect(hasWarned(_, 'Props will not be compiled if no `el`')).toBe(true)
  213. })
  214. it('warn object/array default values', function () {
  215. new Vue({
  216. el: el,
  217. props: {
  218. arr: {
  219. type: Array,
  220. default: []
  221. },
  222. obj: {
  223. type: Object,
  224. default: {}
  225. }
  226. }
  227. })
  228. expect(hasWarned(_, 'Use a factory function to return the default value')).toBe(true)
  229. expect(_.warn.calls.count()).toBe(2)
  230. })
  231. it('teardown', function (done) {
  232. var vm = new Vue({
  233. el: el,
  234. data: {
  235. a: 'A',
  236. b: 'B'
  237. },
  238. template: '<test :aa.sync="a" :bb="b"></test>',
  239. components: {
  240. test: {
  241. props: ['aa', 'bb'],
  242. template: '{{aa}} {{bb}}'
  243. }
  244. }
  245. })
  246. var child = vm.$children[0]
  247. expect(el.firstChild.textContent).toBe('A B')
  248. child.aa = 'AA'
  249. vm.b = 'BB'
  250. _.nextTick(function () {
  251. expect(el.firstChild.textContent).toBe('AA BB')
  252. expect(vm.a).toBe('AA')
  253. // unbind the two props
  254. child._directives[0].unbind()
  255. child._directives[1].unbind()
  256. child.aa = 'AAA'
  257. vm.b = 'BBB'
  258. _.nextTick(function () {
  259. expect(el.firstChild.textContent).toBe('AAA BB')
  260. expect(vm.a).toBe('AA')
  261. done()
  262. })
  263. })
  264. })
  265. it('block instance with replace:true', function () {
  266. new Vue({
  267. el: el,
  268. template: '<test :b="a" :c="d"></test>',
  269. data: {
  270. a: 'AAA',
  271. d: 'DDD'
  272. },
  273. components: {
  274. test: {
  275. props: ['b', 'c'],
  276. template: '<p>{{b}}</p><p>{{c}}</p>',
  277. replace: true
  278. }
  279. }
  280. })
  281. expect(el.innerHTML).toBe('<p>AAA</p><p>DDD</p>')
  282. })
  283. describe('assertions', function () {
  284. function makeInstance (value, type, validator) {
  285. return new Vue({
  286. el: document.createElement('div'),
  287. template: '<test :test="val"></test>',
  288. data: {
  289. val: value
  290. },
  291. components: {
  292. test: {
  293. props: {
  294. test: {
  295. type: type,
  296. validator: validator
  297. }
  298. }
  299. }
  300. }
  301. })
  302. }
  303. it('string', function () {
  304. makeInstance('hello', String)
  305. expect(_.warn).not.toHaveBeenCalled()
  306. makeInstance(123, String)
  307. expect(hasWarned(_, 'Expected String')).toBe(true)
  308. })
  309. it('number', function () {
  310. makeInstance(123, Number)
  311. expect(_.warn).not.toHaveBeenCalled()
  312. makeInstance('123', Number)
  313. expect(hasWarned(_, 'Expected Number')).toBe(true)
  314. })
  315. it('boolean', function () {
  316. makeInstance(true, Boolean)
  317. expect(_.warn).not.toHaveBeenCalled()
  318. makeInstance('123', Boolean)
  319. expect(hasWarned(_, 'Expected Boolean')).toBe(true)
  320. })
  321. it('function', function () {
  322. makeInstance(function () {}, Function)
  323. expect(_.warn).not.toHaveBeenCalled()
  324. makeInstance(123, Function)
  325. expect(hasWarned(_, 'Expected Function')).toBe(true)
  326. })
  327. it('object', function () {
  328. makeInstance({}, Object)
  329. expect(_.warn).not.toHaveBeenCalled()
  330. makeInstance([], Object)
  331. expect(hasWarned(_, 'Expected Object')).toBe(true)
  332. })
  333. it('array', function () {
  334. makeInstance([], Array)
  335. expect(_.warn).not.toHaveBeenCalled()
  336. makeInstance({}, Array)
  337. expect(hasWarned(_, 'Expected Array')).toBe(true)
  338. })
  339. it('custom constructor', function () {
  340. function Class () {}
  341. makeInstance(new Class(), Class)
  342. expect(_.warn).not.toHaveBeenCalled()
  343. makeInstance({}, Class)
  344. expect(hasWarned(_, 'Expected custom type')).toBe(true)
  345. })
  346. it('custom validator', function () {
  347. makeInstance(123, null, function (v) {
  348. return v === 123
  349. })
  350. expect(_.warn).not.toHaveBeenCalled()
  351. makeInstance(123, null, function (v) {
  352. return v === 234
  353. })
  354. expect(hasWarned(_, 'custom validator check failed')).toBe(true)
  355. })
  356. it('type check + custom validator', function () {
  357. makeInstance(123, Number, function (v) {
  358. return v === 123
  359. })
  360. expect(_.warn).not.toHaveBeenCalled()
  361. makeInstance(123, Number, function (v) {
  362. return v === 234
  363. })
  364. expect(hasWarned(_, 'custom validator check failed')).toBe(true)
  365. makeInstance(123, String, function (v) {
  366. return v === 123
  367. })
  368. expect(hasWarned(_, 'Expected String')).toBe(true)
  369. })
  370. it('required', function () {
  371. new Vue({
  372. el: document.createElement('div'),
  373. template: '<test></test>',
  374. components: {
  375. test: {
  376. props: {
  377. prop: { required: true }
  378. }
  379. }
  380. }
  381. })
  382. expect(hasWarned(_, 'Missing required prop')).toBe(true)
  383. })
  384. })
  385. it('alternative syntax', function () {
  386. new Vue({
  387. el: el,
  388. template: '<test :b="a" :c="d"></test>',
  389. data: {
  390. a: 'AAA',
  391. d: 'DDD'
  392. },
  393. components: {
  394. test: {
  395. props: {
  396. b: String,
  397. c: {
  398. type: Number
  399. },
  400. d: {
  401. required: true
  402. }
  403. },
  404. template: '<p>{{b}}</p><p>{{c}}</p>'
  405. }
  406. }
  407. })
  408. expect(hasWarned(_, 'Missing required prop')).toBe(true)
  409. expect(hasWarned(_, 'Expected Number')).toBe(true)
  410. expect(el.textContent).toBe('AAA')
  411. })
  412. it('should not overwrite default value for an absent Boolean prop', function () {
  413. var vm = new Vue({
  414. el: el,
  415. template: '<test></test>',
  416. components: {
  417. test: {
  418. props: {
  419. prop: Boolean
  420. },
  421. data: function () {
  422. return {
  423. prop: true
  424. }
  425. },
  426. template: '{{prop}}'
  427. }
  428. }
  429. })
  430. expect(vm.$children[0].prop).toBe(true)
  431. expect(vm.$el.textContent).toBe('true')
  432. expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
  433. prop: true
  434. }))
  435. })
  436. it('should respect default value of a Boolean prop', function () {
  437. var vm = new Vue({
  438. el: el,
  439. template: '<test></test>',
  440. components: {
  441. test: {
  442. props: {
  443. prop: {
  444. type: Boolean,
  445. default: true
  446. }
  447. },
  448. template: '{{prop}}'
  449. }
  450. }
  451. })
  452. expect(vm.$el.textContent).toBe('true')
  453. })
  454. it('should initialize with default value when not provided & has default data', function (done) {
  455. var vm = new Vue({
  456. el: el,
  457. template: '<test></test>',
  458. components: {
  459. test: {
  460. props: {
  461. prop: {
  462. type: String,
  463. default: 'hello'
  464. },
  465. prop2: {
  466. type: Object,
  467. default: function () {
  468. return { vm: this }
  469. }
  470. }
  471. },
  472. data: function () {
  473. return {
  474. other: 'world'
  475. }
  476. },
  477. template: '{{prop}} {{other}}'
  478. }
  479. }
  480. })
  481. expect(vm.$el.textContent).toBe('hello world')
  482. // object/array default value initializers should be
  483. // called with the correct `this` context
  484. var child = vm.$children[0]
  485. expect(child.prop2.vm).toBe(child)
  486. vm.$children[0].prop = 'bye'
  487. _.nextTick(function () {
  488. expect(vm.$el.textContent).toBe('bye world')
  489. done()
  490. })
  491. })
  492. it('should warn data fields already defined as a prop', function () {
  493. new Vue({
  494. el: el,
  495. props: {
  496. a: null
  497. },
  498. data: {
  499. a: 1
  500. }
  501. })
  502. expect(hasWarned(_, 'already defined as a prop')).toBe(true)
  503. })
  504. it('should not warn for non-required, absent prop', function () {
  505. new Vue({
  506. el: el,
  507. template: '<test></test>',
  508. components: {
  509. test: {
  510. props: {
  511. prop: {
  512. type: String
  513. }
  514. }
  515. }
  516. }
  517. })
  518. expect(_.warn).not.toHaveBeenCalled()
  519. })
  520. // #1683
  521. it('should only trigger sync on reference change', function (done) {
  522. var vm = new Vue({
  523. el: el,
  524. data: {
  525. items: [1, 2]
  526. },
  527. template: '<comp :items.sync="items"></comp>',
  528. components: {
  529. comp: {
  530. props: ['items']
  531. }
  532. }
  533. })
  534. var child = vm.$children[0]
  535. child.items.push(3) // this should not trigger parent to sync it down
  536. var newArray = child.items = [4]
  537. _.nextTick(function () {
  538. expect(child.items).toBe(newArray)
  539. expect(vm.items).toBe(newArray)
  540. done()
  541. })
  542. })
  543. })
  544. }