props.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. import Vue from 'vue'
  2. import { hasSymbol } from 'core/util/env'
  3. import testObjectOption from '../../../helpers/test-object-option'
  4. describe('Options props', () => {
  5. testObjectOption('props')
  6. it('array syntax', done => {
  7. const vm = new Vue({
  8. data: {
  9. b: 'bar'
  10. },
  11. template: '<test v-bind:b="b" ref="child"></test>',
  12. components: {
  13. test: {
  14. props: ['b'],
  15. template: '<div>{{b}}</div>'
  16. }
  17. }
  18. }).$mount()
  19. expect(vm.$el.innerHTML).toBe('bar')
  20. vm.b = 'baz'
  21. waitForUpdate(() => {
  22. expect(vm.$el.innerHTML).toBe('baz')
  23. vm.$refs.child.b = 'qux'
  24. })
  25. .then(() => {
  26. expect(vm.$el.innerHTML).toBe('qux')
  27. expect('Avoid mutating a prop directly').toHaveBeenWarned()
  28. })
  29. .then(done)
  30. })
  31. it('object syntax', done => {
  32. const vm = new Vue({
  33. data: {
  34. b: 'bar'
  35. },
  36. template: '<test v-bind:b="b" ref="child"></test>',
  37. components: {
  38. test: {
  39. props: { b: String },
  40. template: '<div>{{b}}</div>'
  41. }
  42. }
  43. }).$mount()
  44. expect(vm.$el.innerHTML).toBe('bar')
  45. vm.b = 'baz'
  46. waitForUpdate(() => {
  47. expect(vm.$el.innerHTML).toBe('baz')
  48. vm.$refs.child.b = 'qux'
  49. })
  50. .then(() => {
  51. expect(vm.$el.innerHTML).toBe('qux')
  52. expect('Avoid mutating a prop directly').toHaveBeenWarned()
  53. })
  54. .then(done)
  55. })
  56. it('warn mixed syntax', () => {
  57. new Vue({
  58. props: [{ b: String }]
  59. })
  60. expect('props must be strings when using array syntax').toHaveBeenWarned()
  61. })
  62. it('default values', () => {
  63. const vm = new Vue({
  64. data: {
  65. b: undefined
  66. },
  67. template: '<test :b="b"></test>',
  68. components: {
  69. test: {
  70. props: {
  71. a: {
  72. default: 'A' // absent
  73. },
  74. b: {
  75. default: 'B' // undefined
  76. }
  77. },
  78. template: '<div>{{a}}{{b}}</div>'
  79. }
  80. }
  81. }).$mount()
  82. expect(vm.$el.textContent).toBe('AB')
  83. })
  84. it('default value reactivity', done => {
  85. const vm = new Vue({
  86. props: {
  87. a: {
  88. default: () => ({ b: 1 })
  89. }
  90. },
  91. propsData: {
  92. a: undefined
  93. },
  94. template: '<div>{{ a.b }}</div>'
  95. }).$mount()
  96. expect(vm.$el.textContent).toBe('1')
  97. vm.a.b = 2
  98. waitForUpdate(() => {
  99. expect(vm.$el.textContent).toBe('2')
  100. }).then(done)
  101. })
  102. it('default value Function', () => {
  103. const func = () => 132
  104. const vm = new Vue({
  105. props: {
  106. a: {
  107. type: Function,
  108. default: func
  109. }
  110. },
  111. propsData: {
  112. a: undefined
  113. }
  114. })
  115. expect(vm.a).toBe(func)
  116. })
  117. it('warn object/array default values', () => {
  118. new Vue({
  119. props: {
  120. a: {
  121. default: { b: 1 }
  122. }
  123. },
  124. propsData: {
  125. a: undefined
  126. }
  127. })
  128. expect(
  129. 'Props with type Object/Array must use a factory function'
  130. ).toHaveBeenWarned()
  131. })
  132. it('warn missing required', () => {
  133. new Vue({
  134. template: '<test></test>',
  135. components: {
  136. test: {
  137. props: { a: { required: true } },
  138. template: '<div>{{a}}</div>'
  139. }
  140. }
  141. }).$mount()
  142. expect('Missing required prop: "a"').toHaveBeenWarned()
  143. })
  144. describe('assertions', () => {
  145. function makeInstance(value, type, validator?, required?) {
  146. return new Vue({
  147. template: '<test :test="val"></test>',
  148. data: {
  149. val: value
  150. },
  151. components: {
  152. test: {
  153. template: '<div></div>',
  154. props: {
  155. test: {
  156. type,
  157. validator,
  158. required
  159. }
  160. }
  161. }
  162. }
  163. }).$mount()
  164. }
  165. it('string', () => {
  166. makeInstance('hello', String)
  167. expect((console.error as any).mock.calls.length).toBe(0)
  168. makeInstance(123, String)
  169. expect(
  170. 'Expected String with value "123", got Number with value 123'
  171. ).toHaveBeenWarned()
  172. })
  173. it('number', () => {
  174. makeInstance(123, Number)
  175. expect((console.error as any).mock.calls.length).toBe(0)
  176. makeInstance('123', Number)
  177. expect(
  178. 'Expected Number with value 123, got String with value "123"'
  179. ).toHaveBeenWarned()
  180. })
  181. it('number & boolean', () => {
  182. makeInstance(123, Number)
  183. expect((console.error as any).mock.calls.length).toBe(0)
  184. makeInstance(false, Number)
  185. expect('Expected Number, got Boolean with value false').toHaveBeenWarned()
  186. })
  187. it('string & boolean', () => {
  188. makeInstance('hello', String)
  189. expect((console.error as any).mock.calls.length).toBe(0)
  190. makeInstance(true, String)
  191. expect('Expected String, got Boolean with value true').toHaveBeenWarned()
  192. })
  193. it('boolean', () => {
  194. makeInstance(true, Boolean)
  195. expect((console.error as any).mock.calls.length).toBe(0)
  196. makeInstance('123', Boolean)
  197. expect('Expected Boolean, got String with value "123"').toHaveBeenWarned()
  198. })
  199. it('function', () => {
  200. makeInstance(() => {}, Function)
  201. expect((console.error as any).mock.calls.length).toBe(0)
  202. makeInstance(123, Function)
  203. expect('Expected Function, got Number with value 123').toHaveBeenWarned()
  204. })
  205. it('object', () => {
  206. makeInstance({}, Object)
  207. expect((console.error as any).mock.calls.length).toBe(0)
  208. makeInstance([], Object)
  209. expect('Expected Object, got Array').toHaveBeenWarned()
  210. })
  211. it('array', () => {
  212. makeInstance([], Array)
  213. expect((console.error as any).mock.calls.length).toBe(0)
  214. makeInstance({}, Array)
  215. expect('Expected Array, got Object').toHaveBeenWarned()
  216. })
  217. it('primitive wrapper objects', () => {
  218. /* eslint-disable no-new-wrappers */
  219. makeInstance(new String('s'), String)
  220. expect((console.error as any).mock.calls.length).toBe(0)
  221. makeInstance(new Number(1), Number)
  222. expect((console.error as any).mock.calls.length).toBe(0)
  223. makeInstance(new Boolean(true), Boolean)
  224. expect((console.error as any).mock.calls.length).toBe(0)
  225. /* eslint-enable no-new-wrappers */
  226. })
  227. if (hasSymbol) {
  228. it('symbol', () => {
  229. makeInstance(Symbol('foo'), Symbol)
  230. expect((console.error as any).mock.calls.length).toBe(0)
  231. makeInstance({}, Symbol)
  232. expect('Expected Symbol, got Object').toHaveBeenWarned()
  233. })
  234. it('warns when expected an explicable type but Symbol was provided', () => {
  235. makeInstance(Symbol('foo'), String)
  236. expect('Expected String, got Symbol').toHaveBeenWarned()
  237. })
  238. it('warns when expected an explicable type but Symbol was provided', () => {
  239. makeInstance(Symbol('foo'), [String, Number])
  240. expect('Expected String, Number, got Symbol').toHaveBeenWarned()
  241. })
  242. }
  243. if (typeof BigInt !== 'undefined') {
  244. /* global BigInt */
  245. it('bigint', () => {
  246. makeInstance(BigInt(100), BigInt)
  247. expect((console.error as any).mock.calls.length).toBe(0)
  248. makeInstance({}, BigInt)
  249. expect('Expected BigInt, got Object').toHaveBeenWarned()
  250. })
  251. }
  252. it('custom constructor', () => {
  253. function Class() {}
  254. makeInstance(new Class(), Class)
  255. expect((console.error as any).mock.calls.length).toBe(0)
  256. makeInstance({}, Class)
  257. expect('type check failed').toHaveBeenWarned()
  258. })
  259. it('multiple types', () => {
  260. makeInstance([], [Array, Number, Boolean])
  261. expect((console.error as any).mock.calls.length).toBe(0)
  262. makeInstance({}, [Array, Number, Boolean])
  263. expect('Expected Array, Number, Boolean, got Object').toHaveBeenWarned()
  264. })
  265. it('custom validator', () => {
  266. makeInstance(123, null, v => v === 123)
  267. expect((console.error as any).mock.calls.length).toBe(0)
  268. makeInstance(123, null, v => v === 234)
  269. expect('custom validator check failed').toHaveBeenWarned()
  270. })
  271. it('type check + custom validator', () => {
  272. makeInstance(123, Number, v => v === 123)
  273. expect((console.error as any).mock.calls.length).toBe(0)
  274. makeInstance(123, Number, v => v === 234)
  275. expect('custom validator check failed').toHaveBeenWarned()
  276. makeInstance(123, String, v => v === 123)
  277. expect(
  278. 'Expected String with value "123", got Number with value 123'
  279. ).toHaveBeenWarned()
  280. })
  281. it('multiple types + custom validator', () => {
  282. makeInstance(123, [Number, String, Boolean], v => v === 123)
  283. expect((console.error as any).mock.calls.length).toBe(0)
  284. makeInstance(123, [Number, String, Boolean], v => v === 234)
  285. expect('custom validator check failed').toHaveBeenWarned()
  286. makeInstance(123, [String, Boolean], v => v === 123)
  287. expect('Expected String, Boolean').toHaveBeenWarned()
  288. })
  289. it('optional with type + null/undefined', () => {
  290. makeInstance(undefined, String)
  291. expect((console.error as any).mock.calls.length).toBe(0)
  292. makeInstance(null, String)
  293. expect((console.error as any).mock.calls.length).toBe(0)
  294. })
  295. it('required with type + null/undefined', () => {
  296. makeInstance(undefined, String, null, true)
  297. expect((console.error as any).mock.calls.length).toBe(1)
  298. expect('Expected String').toHaveBeenWarned()
  299. makeInstance(null, Boolean, null, true)
  300. expect((console.error as any).mock.calls.length).toBe(2)
  301. expect('Expected Boolean').toHaveBeenWarned()
  302. })
  303. it('optional prop of any type (type: true or prop: true)', () => {
  304. makeInstance(1, true)
  305. expect((console.error as any).mock.calls.length).toBe(0)
  306. makeInstance('any', true)
  307. expect((console.error as any).mock.calls.length).toBe(0)
  308. makeInstance({}, true)
  309. expect((console.error as any).mock.calls.length).toBe(0)
  310. makeInstance(undefined, true)
  311. expect((console.error as any).mock.calls.length).toBe(0)
  312. makeInstance(null, true)
  313. expect((console.error as any).mock.calls.length).toBe(0)
  314. })
  315. })
  316. it('should work with v-bind', () => {
  317. const vm = new Vue({
  318. template: `<test v-bind="{ a: 1, b: 2 }"></test>`,
  319. components: {
  320. test: {
  321. props: ['a', 'b'],
  322. template: '<div>{{ a }} {{ b }}</div>'
  323. }
  324. }
  325. }).$mount()
  326. expect(vm.$el.textContent).toBe('1 2')
  327. })
  328. it('should warn data fields already defined as a prop', () => {
  329. new Vue({
  330. template: '<test a="1"></test>',
  331. components: {
  332. test: {
  333. template: '<div></div>',
  334. data: function () {
  335. return { a: 123 }
  336. },
  337. props: {
  338. a: null
  339. }
  340. }
  341. }
  342. }).$mount()
  343. expect('already declared as a prop').toHaveBeenWarned()
  344. })
  345. it('should warn methods already defined as a prop', () => {
  346. new Vue({
  347. template: '<test a="1"></test>',
  348. components: {
  349. test: {
  350. template: '<div></div>',
  351. props: {
  352. a: null
  353. },
  354. methods: {
  355. a() {}
  356. }
  357. }
  358. }
  359. }).$mount()
  360. expect(`Method "a" has already been defined as a prop`).toHaveBeenWarned()
  361. expect(`Avoid mutating a prop directly`).toHaveBeenWarned()
  362. })
  363. it('treat boolean props properly', () => {
  364. const vm = new Vue({
  365. template: '<comp ref="child" prop-a prop-b="prop-b"></comp>',
  366. components: {
  367. comp: {
  368. template: '<div></div>',
  369. props: {
  370. propA: Boolean,
  371. propB: Boolean,
  372. propC: Boolean
  373. }
  374. }
  375. }
  376. }).$mount()
  377. expect(vm.$refs.child.propA).toBe(true)
  378. expect(vm.$refs.child.propB).toBe(true)
  379. expect(vm.$refs.child.propC).toBe(false)
  380. })
  381. it('should respect default value of a Boolean prop', function () {
  382. const vm = new Vue({
  383. template: '<test></test>',
  384. components: {
  385. test: {
  386. props: {
  387. prop: {
  388. type: Boolean,
  389. default: true
  390. }
  391. },
  392. template: '<div>{{prop}}</div>'
  393. }
  394. }
  395. }).$mount()
  396. expect(vm.$el.textContent).toBe('true')
  397. })
  398. it('non reactive values passed down as prop should not be converted', done => {
  399. const a = Object.freeze({
  400. nested: {
  401. msg: 'hello'
  402. }
  403. })
  404. const parent = new Vue({
  405. template: '<comp :a="a.nested"></comp>',
  406. data: {
  407. a: a
  408. },
  409. components: {
  410. comp: {
  411. template: '<div></div>',
  412. props: ['a']
  413. }
  414. }
  415. }).$mount()
  416. const child = parent.$children[0]
  417. expect(child.a.msg).toBe('hello')
  418. expect(child.a.__ob__).toBeUndefined() // should not be converted
  419. parent.a = Object.freeze({
  420. nested: {
  421. msg: 'yo'
  422. }
  423. })
  424. waitForUpdate(() => {
  425. expect(child.a.msg).toBe('yo')
  426. expect(child.a.__ob__).toBeUndefined()
  427. }).then(done)
  428. })
  429. it('should not warn for non-required, absent prop', function () {
  430. new Vue({
  431. template: '<test></test>',
  432. components: {
  433. test: {
  434. template: '<div></div>',
  435. props: {
  436. prop: {
  437. type: String
  438. }
  439. }
  440. }
  441. }
  442. }).$mount()
  443. expect((console.error as any).mock.calls.length).toBe(0)
  444. })
  445. // #3453
  446. it('should not fire watcher on object/array props when parent re-renders', done => {
  447. const spy = vi.fn()
  448. const vm = new Vue({
  449. data: {
  450. arr: []
  451. },
  452. template: '<test :prop="arr">hi</test>',
  453. components: {
  454. test: {
  455. props: ['prop'],
  456. watch: {
  457. prop: spy
  458. },
  459. template: '<div><slot></slot></div>'
  460. }
  461. }
  462. }).$mount()
  463. vm.$forceUpdate()
  464. waitForUpdate(() => {
  465. expect(spy).not.toHaveBeenCalled()
  466. }).then(done)
  467. })
  468. // #4090
  469. it('should not trigger watcher on default value', done => {
  470. const spy = vi.fn()
  471. const vm = new Vue({
  472. template: `<test :value="a" :test="b"></test>`,
  473. data: {
  474. a: 1,
  475. b: undefined
  476. },
  477. components: {
  478. test: {
  479. template: '<div>{{ value }}</div>',
  480. props: {
  481. value: { type: Number },
  482. test: {
  483. type: Object,
  484. default: () => ({})
  485. }
  486. },
  487. watch: {
  488. test: spy
  489. }
  490. }
  491. }
  492. }).$mount()
  493. vm.a++
  494. waitForUpdate(() => {
  495. expect(spy).not.toHaveBeenCalled()
  496. vm.b = {}
  497. })
  498. .then(() => {
  499. expect(spy.mock.calls.length).toBe(1)
  500. })
  501. .then(() => {
  502. vm.b = undefined
  503. })
  504. .then(() => {
  505. expect(spy.mock.calls.length).toBe(2)
  506. vm.a++
  507. })
  508. .then(() => {
  509. expect(spy.mock.calls.length).toBe(2)
  510. })
  511. .then(done)
  512. })
  513. it('warn reserved props', () => {
  514. const specialAttrs = ['key', 'ref', 'slot', 'is', 'slot-scope']
  515. new Vue({
  516. props: specialAttrs
  517. })
  518. specialAttrs.forEach(attr => {
  519. expect(`"${attr}" is a reserved attribute`).toHaveBeenWarned()
  520. })
  521. })
  522. it('should consider order when casting [Boolean, String] multi-type props', () => {
  523. const vm = new Vue({
  524. template: '<test ref="test" booleanOrString stringOrBoolean />',
  525. components: {
  526. test: {
  527. template: '<div></div>',
  528. props: {
  529. booleanOrString: [Boolean, String],
  530. stringOrBoolean: [String, Boolean]
  531. }
  532. }
  533. }
  534. }).$mount()
  535. expect(vm.$refs.test.$props.booleanOrString).toBe(true)
  536. expect(vm.$refs.test.$props.stringOrBoolean).toBe('')
  537. })
  538. it('should warn when a prop type is not a constructor', () => {
  539. new Vue({
  540. template: '<div>{{a}}</div>',
  541. props: {
  542. a: {
  543. type: 'String',
  544. default: 'test'
  545. }
  546. }
  547. }).$mount()
  548. expect(
  549. 'Invalid prop type: "String" is not a constructor'
  550. ).toHaveBeenWarned()
  551. })
  552. })