componentProps.spec.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import {
  2. ComponentInternalInstance,
  3. getCurrentInstance,
  4. render,
  5. h,
  6. nodeOps,
  7. FunctionalComponent,
  8. defineComponent,
  9. ref,
  10. serializeInner,
  11. createApp,
  12. provide,
  13. inject
  14. } from '@vue/runtime-test'
  15. import { render as domRender, nextTick } from 'vue'
  16. describe('component props', () => {
  17. test('stateful', () => {
  18. let props: any
  19. let attrs: any
  20. let proxy: any
  21. const Comp = defineComponent({
  22. props: ['fooBar', 'barBaz'],
  23. render() {
  24. props = this.$props
  25. attrs = this.$attrs
  26. proxy = this
  27. }
  28. })
  29. const root = nodeOps.createElement('div')
  30. render(h(Comp, { fooBar: 1, bar: 2 }), root)
  31. expect(proxy.fooBar).toBe(1)
  32. expect(props).toEqual({ fooBar: 1 })
  33. expect(attrs).toEqual({ bar: 2 })
  34. // test passing kebab-case and resolving to camelCase
  35. render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
  36. expect(proxy.fooBar).toBe(2)
  37. expect(props).toEqual({ fooBar: 2 })
  38. expect(attrs).toEqual({ bar: 3, baz: 4 })
  39. // test updating kebab-case should not delete it (#955)
  40. render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4, barBaz: 5 }), root)
  41. expect(proxy.fooBar).toBe(3)
  42. expect(proxy.barBaz).toBe(5)
  43. expect(props).toEqual({ fooBar: 3, barBaz: 5 })
  44. expect(attrs).toEqual({ bar: 3, baz: 4 })
  45. render(h(Comp, { qux: 5 }), root)
  46. expect(proxy.fooBar).toBeUndefined()
  47. // remove the props with camelCase key (#1412)
  48. expect(proxy.barBaz).toBeUndefined()
  49. expect(props).toEqual({})
  50. expect(attrs).toEqual({ qux: 5 })
  51. })
  52. test('stateful with setup', () => {
  53. let props: any
  54. let attrs: any
  55. const Comp = defineComponent({
  56. props: ['foo'],
  57. setup(_props, { attrs: _attrs }) {
  58. return () => {
  59. props = _props
  60. attrs = _attrs
  61. }
  62. }
  63. })
  64. const root = nodeOps.createElement('div')
  65. render(h(Comp, { foo: 1, bar: 2 }), root)
  66. expect(props).toEqual({ foo: 1 })
  67. expect(attrs).toEqual({ bar: 2 })
  68. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  69. expect(props).toEqual({ foo: 2 })
  70. expect(attrs).toEqual({ bar: 3, baz: 4 })
  71. render(h(Comp, { qux: 5 }), root)
  72. expect(props).toEqual({})
  73. expect(attrs).toEqual({ qux: 5 })
  74. })
  75. test('functional with declaration', () => {
  76. let props: any
  77. let attrs: any
  78. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  79. props = _props
  80. attrs = _attrs
  81. }
  82. Comp.props = ['foo']
  83. const root = nodeOps.createElement('div')
  84. render(h(Comp, { foo: 1, bar: 2 }), root)
  85. expect(props).toEqual({ foo: 1 })
  86. expect(attrs).toEqual({ bar: 2 })
  87. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  88. expect(props).toEqual({ foo: 2 })
  89. expect(attrs).toEqual({ bar: 3, baz: 4 })
  90. render(h(Comp, { qux: 5 }), root)
  91. expect(props).toEqual({})
  92. expect(attrs).toEqual({ qux: 5 })
  93. })
  94. test('functional without declaration', () => {
  95. let props: any
  96. let attrs: any
  97. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  98. props = _props
  99. attrs = _attrs
  100. }
  101. const root = nodeOps.createElement('div')
  102. render(h(Comp, { foo: 1 }), root)
  103. expect(props).toEqual({ foo: 1 })
  104. expect(attrs).toEqual({ foo: 1 })
  105. expect(props).toBe(attrs)
  106. render(h(Comp, { bar: 2 }), root)
  107. expect(props).toEqual({ bar: 2 })
  108. expect(attrs).toEqual({ bar: 2 })
  109. expect(props).toBe(attrs)
  110. })
  111. test('boolean casting', () => {
  112. let proxy: any
  113. const Comp = {
  114. props: {
  115. foo: Boolean,
  116. bar: Boolean,
  117. baz: Boolean,
  118. qux: Boolean
  119. },
  120. render() {
  121. proxy = this
  122. }
  123. }
  124. render(
  125. h(Comp, {
  126. // absent should cast to false
  127. bar: '', // empty string should cast to true
  128. baz: 'baz', // same string should cast to true
  129. qux: 'ok' // other values should be left in-tact (but raise warning)
  130. }),
  131. nodeOps.createElement('div')
  132. )
  133. expect(proxy.foo).toBe(false)
  134. expect(proxy.bar).toBe(true)
  135. expect(proxy.baz).toBe(true)
  136. expect(proxy.qux).toBe('ok')
  137. expect('type check failed for prop "qux"').toHaveBeenWarned()
  138. })
  139. test('default value', () => {
  140. let proxy: any
  141. const defaultFn = jest.fn(() => ({ a: 1 }))
  142. const defaultBaz = jest.fn(() => ({ b: 1 }))
  143. const Comp = {
  144. props: {
  145. foo: {
  146. default: 1
  147. },
  148. bar: {
  149. default: defaultFn
  150. },
  151. baz: {
  152. type: Function,
  153. default: defaultBaz
  154. }
  155. },
  156. render() {
  157. proxy = this
  158. }
  159. }
  160. const root = nodeOps.createElement('div')
  161. render(h(Comp, { foo: 2 }), root)
  162. expect(proxy.foo).toBe(2)
  163. const prevBar = proxy.bar
  164. expect(proxy.bar).toEqual({ a: 1 })
  165. expect(proxy.baz).toEqual(defaultBaz)
  166. expect(defaultFn).toHaveBeenCalledTimes(1)
  167. expect(defaultBaz).toHaveBeenCalledTimes(0)
  168. // #999: updates should not cause default factory of unchanged prop to be
  169. // called again
  170. render(h(Comp, { foo: 3 }), root)
  171. expect(proxy.foo).toBe(3)
  172. expect(proxy.bar).toEqual({ a: 1 })
  173. expect(proxy.bar).toBe(prevBar)
  174. expect(defaultFn).toHaveBeenCalledTimes(1)
  175. render(h(Comp, { bar: { b: 2 } }), root)
  176. expect(proxy.foo).toBe(1)
  177. expect(proxy.bar).toEqual({ b: 2 })
  178. expect(defaultFn).toHaveBeenCalledTimes(1)
  179. render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
  180. expect(proxy.foo).toBe(3)
  181. expect(proxy.bar).toEqual({ b: 3 })
  182. expect(defaultFn).toHaveBeenCalledTimes(1)
  183. render(h(Comp, { bar: { b: 4 } }), root)
  184. expect(proxy.foo).toBe(1)
  185. expect(proxy.bar).toEqual({ b: 4 })
  186. expect(defaultFn).toHaveBeenCalledTimes(1)
  187. })
  188. test('using inject in default value factory', () => {
  189. const Child = defineComponent({
  190. props: {
  191. test: {
  192. default: () => inject('test', 'default')
  193. }
  194. },
  195. setup(props) {
  196. return () => {
  197. return h('div', props.test)
  198. }
  199. }
  200. })
  201. const Comp = {
  202. setup() {
  203. provide('test', 'injected')
  204. return () => h(Child)
  205. }
  206. }
  207. const root = nodeOps.createElement('div')
  208. render(h(Comp), root)
  209. expect(serializeInner(root)).toBe(`<div>injected</div>`)
  210. })
  211. test('optimized props updates', async () => {
  212. const Child = defineComponent({
  213. props: ['foo'],
  214. template: `<div>{{ foo }}</div>`
  215. })
  216. const foo = ref(1)
  217. const id = ref('a')
  218. const Comp = defineComponent({
  219. setup() {
  220. return {
  221. foo,
  222. id
  223. }
  224. },
  225. components: { Child },
  226. template: `<Child :foo="foo" :id="id"/>`
  227. })
  228. // Note this one is using the main Vue render so it can compile template
  229. // on the fly
  230. const root = document.createElement('div')
  231. domRender(h(Comp), root)
  232. expect(root.innerHTML).toBe('<div id="a">1</div>')
  233. foo.value++
  234. await nextTick()
  235. expect(root.innerHTML).toBe('<div id="a">2</div>')
  236. id.value = 'b'
  237. await nextTick()
  238. expect(root.innerHTML).toBe('<div id="b">2</div>')
  239. })
  240. test('warn props mutation', () => {
  241. let instance: ComponentInternalInstance
  242. let setupProps: any
  243. const Comp = {
  244. props: ['foo'],
  245. setup(props: any) {
  246. instance = getCurrentInstance()!
  247. setupProps = props
  248. return () => null
  249. }
  250. }
  251. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  252. expect(setupProps.foo).toBe(1)
  253. expect(instance!.props.foo).toBe(1)
  254. setupProps.foo = 2
  255. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  256. expect(() => {
  257. ;(instance!.proxy as any).foo = 2
  258. }).toThrow(TypeError)
  259. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  260. })
  261. test('merging props from mixins and extends', () => {
  262. let setupProps: any
  263. let renderProxy: any
  264. const E = {
  265. props: ['base']
  266. }
  267. const M1 = {
  268. props: ['m1']
  269. }
  270. const M2 = {
  271. props: { m2: null }
  272. }
  273. const Comp = {
  274. props: ['self'],
  275. mixins: [M1, M2],
  276. extends: E,
  277. setup(props: any) {
  278. setupProps = props
  279. },
  280. render(this: any) {
  281. renderProxy = this
  282. return h('div', [this.self, this.base, this.m1, this.m2])
  283. }
  284. }
  285. const root = nodeOps.createElement('div')
  286. const props = {
  287. self: 'from self, ',
  288. base: 'from base, ',
  289. m1: 'from mixin 1, ',
  290. m2: 'from mixin 2'
  291. }
  292. render(h(Comp, props), root)
  293. expect(serializeInner(root)).toMatch(
  294. `from self, from base, from mixin 1, from mixin 2`
  295. )
  296. expect(setupProps).toMatchObject(props)
  297. expect(renderProxy.$props).toMatchObject(props)
  298. })
  299. test('merging props from global mixins', () => {
  300. let setupProps: any
  301. let renderProxy: any
  302. const M1 = {
  303. props: ['m1']
  304. }
  305. const M2 = {
  306. props: { m2: null }
  307. }
  308. const Comp = {
  309. props: ['self'],
  310. setup(props: any) {
  311. setupProps = props
  312. },
  313. render(this: any) {
  314. renderProxy = this
  315. return h('div', [this.self, this.m1, this.m2])
  316. }
  317. }
  318. const props = {
  319. self: 'from self, ',
  320. m1: 'from mixin 1, ',
  321. m2: 'from mixin 2'
  322. }
  323. const app = createApp(Comp, props)
  324. app.mixin(M1)
  325. app.mixin(M2)
  326. const root = nodeOps.createElement('div')
  327. app.mount(root)
  328. expect(serializeInner(root)).toMatch(
  329. `from self, from mixin 1, from mixin 2`
  330. )
  331. expect(setupProps).toMatchObject(props)
  332. expect(renderProxy.$props).toMatchObject(props)
  333. })
  334. })