prop_spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. {
  295. name: 'test',
  296. type: type,
  297. validator: validator
  298. }
  299. ]
  300. }
  301. }
  302. })
  303. }
  304. it('string', function () {
  305. makeInstance('hello', String)
  306. expect(_.warn).not.toHaveBeenCalled()
  307. makeInstance(123, String)
  308. expect(hasWarned(_, 'Expected String')).toBe(true)
  309. })
  310. it('number', function () {
  311. makeInstance(123, Number)
  312. expect(_.warn).not.toHaveBeenCalled()
  313. makeInstance('123', Number)
  314. expect(hasWarned(_, 'Expected Number')).toBe(true)
  315. })
  316. it('boolean', function () {
  317. makeInstance(true, Boolean)
  318. expect(_.warn).not.toHaveBeenCalled()
  319. makeInstance('123', Boolean)
  320. expect(hasWarned(_, 'Expected Boolean')).toBe(true)
  321. })
  322. it('function', function () {
  323. makeInstance(function () {}, Function)
  324. expect(_.warn).not.toHaveBeenCalled()
  325. makeInstance(123, Function)
  326. expect(hasWarned(_, 'Expected Function')).toBe(true)
  327. })
  328. it('object', function () {
  329. makeInstance({}, Object)
  330. expect(_.warn).not.toHaveBeenCalled()
  331. makeInstance([], Object)
  332. expect(hasWarned(_, 'Expected Object')).toBe(true)
  333. })
  334. it('array', function () {
  335. makeInstance([], Array)
  336. expect(_.warn).not.toHaveBeenCalled()
  337. makeInstance({}, Array)
  338. expect(hasWarned(_, 'Expected Array')).toBe(true)
  339. })
  340. it('custom constructor', function () {
  341. function Class () {}
  342. makeInstance(new Class(), Class)
  343. expect(_.warn).not.toHaveBeenCalled()
  344. makeInstance({}, Class)
  345. expect(hasWarned(_, 'Expected custom type')).toBe(true)
  346. })
  347. it('custom validator', function () {
  348. makeInstance(123, null, function (v) {
  349. return v === 123
  350. })
  351. expect(_.warn).not.toHaveBeenCalled()
  352. makeInstance(123, null, function (v) {
  353. return v === 234
  354. })
  355. expect(hasWarned(_, 'custom validator check failed')).toBe(true)
  356. })
  357. it('type check + custom validator', function () {
  358. makeInstance(123, Number, function (v) {
  359. return v === 123
  360. })
  361. expect(_.warn).not.toHaveBeenCalled()
  362. makeInstance(123, Number, function (v) {
  363. return v === 234
  364. })
  365. expect(hasWarned(_, 'custom validator check failed')).toBe(true)
  366. makeInstance(123, String, function (v) {
  367. return v === 123
  368. })
  369. expect(hasWarned(_, 'Expected String')).toBe(true)
  370. })
  371. it('required', function () {
  372. new Vue({
  373. el: document.createElement('div'),
  374. template: '<test></test>',
  375. components: {
  376. test: {
  377. props: [
  378. {
  379. name: 'prop',
  380. required: true
  381. }
  382. ]
  383. }
  384. }
  385. })
  386. expect(hasWarned(_, 'Missing required prop')).toBe(true)
  387. })
  388. })
  389. it('alternative syntax', function () {
  390. new Vue({
  391. el: el,
  392. template: '<test :b="a" :c="d"></test>',
  393. data: {
  394. a: 'AAA',
  395. d: 'DDD'
  396. },
  397. components: {
  398. test: {
  399. props: {
  400. b: String,
  401. c: {
  402. type: Number
  403. },
  404. d: {
  405. required: true
  406. }
  407. },
  408. template: '<p>{{b}}</p><p>{{c}}</p>'
  409. }
  410. }
  411. })
  412. expect(hasWarned(_, 'Missing required prop')).toBe(true)
  413. expect(hasWarned(_, 'Expected Number')).toBe(true)
  414. expect(el.textContent).toBe('AAA')
  415. })
  416. it('should not overwrite default value for an absent Boolean prop', function () {
  417. var vm = new Vue({
  418. el: el,
  419. template: '<test></test>',
  420. components: {
  421. test: {
  422. props: {
  423. prop: Boolean
  424. },
  425. data: function () {
  426. return {
  427. prop: true
  428. }
  429. },
  430. template: '{{prop}}'
  431. }
  432. }
  433. })
  434. expect(vm.$children[0].prop).toBe(true)
  435. expect(vm.$el.textContent).toBe('true')
  436. expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
  437. prop: true
  438. }))
  439. })
  440. it('should respect default value of a Boolean prop', function () {
  441. var vm = new Vue({
  442. el: el,
  443. template: '<test></test>',
  444. components: {
  445. test: {
  446. props: {
  447. prop: {
  448. type: Boolean,
  449. default: true
  450. }
  451. },
  452. template: '{{prop}}'
  453. }
  454. }
  455. })
  456. expect(vm.$el.textContent).toBe('true')
  457. })
  458. it('should initialize with default value when not provided & has default data', function (done) {
  459. var vm = new Vue({
  460. el: el,
  461. template: '<test></test>',
  462. components: {
  463. test: {
  464. props: {
  465. prop: {
  466. type: String,
  467. default: 'hello'
  468. }
  469. },
  470. data: function () {
  471. return {
  472. other: 'world'
  473. }
  474. },
  475. template: '{{prop}} {{other}}'
  476. }
  477. }
  478. })
  479. expect(vm.$el.textContent).toBe('hello world')
  480. vm.$children[0].prop = 'bye'
  481. _.nextTick(function () {
  482. expect(vm.$el.textContent).toBe('bye world')
  483. done()
  484. })
  485. })
  486. it('should warn data fields already defined as a prop', function () {
  487. new Vue({
  488. el: el,
  489. props: {
  490. a: null
  491. },
  492. data: {
  493. a: 1
  494. }
  495. })
  496. expect(hasWarned(_, 'already defined as a prop')).toBe(true)
  497. })
  498. it('should not warn for non-required, absent prop', function () {
  499. new Vue({
  500. el: el,
  501. template: '<test></test>',
  502. components: {
  503. test: {
  504. props: {
  505. prop: {
  506. type: String
  507. }
  508. }
  509. }
  510. }
  511. })
  512. expect(_.warn).not.toHaveBeenCalled()
  513. })
  514. })
  515. }