| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- import {
- ComponentInternalInstance,
- getCurrentInstance,
- render,
- h,
- nodeOps,
- FunctionalComponent,
- defineComponent,
- ref,
- serializeInner,
- createApp,
- provide,
- inject
- } from '@vue/runtime-test'
- import { render as domRender, nextTick } from 'vue'
- describe('component props', () => {
- test('stateful', () => {
- let props: any
- let attrs: any
- let proxy: any
- const Comp = defineComponent({
- props: ['fooBar', 'barBaz'],
- render() {
- props = this.$props
- attrs = this.$attrs
- proxy = this
- }
- })
- const root = nodeOps.createElement('div')
- render(h(Comp, { fooBar: 1, bar: 2 }), root)
- expect(proxy.fooBar).toBe(1)
- expect(props).toEqual({ fooBar: 1 })
- expect(attrs).toEqual({ bar: 2 })
- // test passing kebab-case and resolving to camelCase
- render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
- expect(proxy.fooBar).toBe(2)
- expect(props).toEqual({ fooBar: 2 })
- expect(attrs).toEqual({ bar: 3, baz: 4 })
- // test updating kebab-case should not delete it (#955)
- render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4, barBaz: 5 }), root)
- expect(proxy.fooBar).toBe(3)
- expect(proxy.barBaz).toBe(5)
- expect(props).toEqual({ fooBar: 3, barBaz: 5 })
- expect(attrs).toEqual({ bar: 3, baz: 4 })
- render(h(Comp, { qux: 5 }), root)
- expect(proxy.fooBar).toBeUndefined()
- // remove the props with camelCase key (#1412)
- expect(proxy.barBaz).toBeUndefined()
- expect(props).toEqual({})
- expect(attrs).toEqual({ qux: 5 })
- })
- test('stateful with setup', () => {
- let props: any
- let attrs: any
- const Comp = defineComponent({
- props: ['foo'],
- setup(_props, { attrs: _attrs }) {
- return () => {
- props = _props
- attrs = _attrs
- }
- }
- })
- const root = nodeOps.createElement('div')
- render(h(Comp, { foo: 1, bar: 2 }), root)
- expect(props).toEqual({ foo: 1 })
- expect(attrs).toEqual({ bar: 2 })
- render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
- expect(props).toEqual({ foo: 2 })
- expect(attrs).toEqual({ bar: 3, baz: 4 })
- render(h(Comp, { qux: 5 }), root)
- expect(props).toEqual({})
- expect(attrs).toEqual({ qux: 5 })
- })
- test('functional with declaration', () => {
- let props: any
- let attrs: any
- const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
- props = _props
- attrs = _attrs
- }
- Comp.props = ['foo']
- const root = nodeOps.createElement('div')
- render(h(Comp, { foo: 1, bar: 2 }), root)
- expect(props).toEqual({ foo: 1 })
- expect(attrs).toEqual({ bar: 2 })
- render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
- expect(props).toEqual({ foo: 2 })
- expect(attrs).toEqual({ bar: 3, baz: 4 })
- render(h(Comp, { qux: 5 }), root)
- expect(props).toEqual({})
- expect(attrs).toEqual({ qux: 5 })
- })
- test('functional without declaration', () => {
- let props: any
- let attrs: any
- const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
- props = _props
- attrs = _attrs
- }
- const root = nodeOps.createElement('div')
- render(h(Comp, { foo: 1 }), root)
- expect(props).toEqual({ foo: 1 })
- expect(attrs).toEqual({ foo: 1 })
- expect(props).toBe(attrs)
- render(h(Comp, { bar: 2 }), root)
- expect(props).toEqual({ bar: 2 })
- expect(attrs).toEqual({ bar: 2 })
- expect(props).toBe(attrs)
- })
- test('boolean casting', () => {
- let proxy: any
- const Comp = {
- props: {
- foo: Boolean,
- bar: Boolean,
- baz: Boolean,
- qux: Boolean
- },
- render() {
- proxy = this
- }
- }
- render(
- h(Comp, {
- // absent should cast to false
- bar: '', // empty string should cast to true
- baz: 'baz', // same string should cast to true
- qux: 'ok' // other values should be left in-tact (but raise warning)
- }),
- nodeOps.createElement('div')
- )
- expect(proxy.foo).toBe(false)
- expect(proxy.bar).toBe(true)
- expect(proxy.baz).toBe(true)
- expect(proxy.qux).toBe('ok')
- expect('type check failed for prop "qux"').toHaveBeenWarned()
- })
- test('default value', () => {
- let proxy: any
- const defaultFn = jest.fn(() => ({ a: 1 }))
- const defaultBaz = jest.fn(() => ({ b: 1 }))
- const Comp = {
- props: {
- foo: {
- default: 1
- },
- bar: {
- default: defaultFn
- },
- baz: {
- type: Function,
- default: defaultBaz
- }
- },
- render() {
- proxy = this
- }
- }
- const root = nodeOps.createElement('div')
- render(h(Comp, { foo: 2 }), root)
- expect(proxy.foo).toBe(2)
- const prevBar = proxy.bar
- expect(proxy.bar).toEqual({ a: 1 })
- expect(proxy.baz).toEqual(defaultBaz)
- expect(defaultFn).toHaveBeenCalledTimes(1)
- expect(defaultBaz).toHaveBeenCalledTimes(0)
- // #999: updates should not cause default factory of unchanged prop to be
- // called again
- render(h(Comp, { foo: 3 }), root)
- expect(proxy.foo).toBe(3)
- expect(proxy.bar).toEqual({ a: 1 })
- expect(proxy.bar).toBe(prevBar)
- expect(defaultFn).toHaveBeenCalledTimes(1)
- render(h(Comp, { bar: { b: 2 } }), root)
- expect(proxy.foo).toBe(1)
- expect(proxy.bar).toEqual({ b: 2 })
- expect(defaultFn).toHaveBeenCalledTimes(1)
- render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
- expect(proxy.foo).toBe(3)
- expect(proxy.bar).toEqual({ b: 3 })
- expect(defaultFn).toHaveBeenCalledTimes(1)
- render(h(Comp, { bar: { b: 4 } }), root)
- expect(proxy.foo).toBe(1)
- expect(proxy.bar).toEqual({ b: 4 })
- expect(defaultFn).toHaveBeenCalledTimes(1)
- })
- test('using inject in default value factory', () => {
- const Child = defineComponent({
- props: {
- test: {
- default: () => inject('test', 'default')
- }
- },
- setup(props) {
- return () => {
- return h('div', props.test)
- }
- }
- })
- const Comp = {
- setup() {
- provide('test', 'injected')
- return () => h(Child)
- }
- }
- const root = nodeOps.createElement('div')
- render(h(Comp), root)
- expect(serializeInner(root)).toBe(`<div>injected</div>`)
- })
- test('optimized props updates', async () => {
- const Child = defineComponent({
- props: ['foo'],
- template: `<div>{{ foo }}</div>`
- })
- const foo = ref(1)
- const id = ref('a')
- const Comp = defineComponent({
- setup() {
- return {
- foo,
- id
- }
- },
- components: { Child },
- template: `<Child :foo="foo" :id="id"/>`
- })
- // Note this one is using the main Vue render so it can compile template
- // on the fly
- const root = document.createElement('div')
- domRender(h(Comp), root)
- expect(root.innerHTML).toBe('<div id="a">1</div>')
- foo.value++
- await nextTick()
- expect(root.innerHTML).toBe('<div id="a">2</div>')
- id.value = 'b'
- await nextTick()
- expect(root.innerHTML).toBe('<div id="b">2</div>')
- })
- test('warn props mutation', () => {
- let instance: ComponentInternalInstance
- let setupProps: any
- const Comp = {
- props: ['foo'],
- setup(props: any) {
- instance = getCurrentInstance()!
- setupProps = props
- return () => null
- }
- }
- render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
- expect(setupProps.foo).toBe(1)
- expect(instance!.props.foo).toBe(1)
- setupProps.foo = 2
- expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
- expect(() => {
- ;(instance!.proxy as any).foo = 2
- }).toThrow(TypeError)
- expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
- })
- test('merging props from mixins and extends', () => {
- let setupProps: any
- let renderProxy: any
- const E = {
- props: ['base']
- }
- const M1 = {
- props: ['m1']
- }
- const M2 = {
- props: { m2: null }
- }
- const Comp = {
- props: ['self'],
- mixins: [M1, M2],
- extends: E,
- setup(props: any) {
- setupProps = props
- },
- render(this: any) {
- renderProxy = this
- return h('div', [this.self, this.base, this.m1, this.m2])
- }
- }
- const root = nodeOps.createElement('div')
- const props = {
- self: 'from self, ',
- base: 'from base, ',
- m1: 'from mixin 1, ',
- m2: 'from mixin 2'
- }
- render(h(Comp, props), root)
- expect(serializeInner(root)).toMatch(
- `from self, from base, from mixin 1, from mixin 2`
- )
- expect(setupProps).toMatchObject(props)
- expect(renderProxy.$props).toMatchObject(props)
- })
- test('merging props from global mixins', () => {
- let setupProps: any
- let renderProxy: any
- const M1 = {
- props: ['m1']
- }
- const M2 = {
- props: { m2: null }
- }
- const Comp = {
- props: ['self'],
- setup(props: any) {
- setupProps = props
- },
- render(this: any) {
- renderProxy = this
- return h('div', [this.self, this.m1, this.m2])
- }
- }
- const props = {
- self: 'from self, ',
- m1: 'from mixin 1, ',
- m2: 'from mixin 2'
- }
- const app = createApp(Comp, props)
- app.mixin(M1)
- app.mixin(M2)
- const root = nodeOps.createElement('div')
- app.mount(root)
- expect(serializeInner(root)).toMatch(
- `from self, from mixin 1, from mixin 2`
- )
- expect(setupProps).toMatchObject(props)
- expect(renderProxy.$props).toMatchObject(props)
- })
- })
|