|
| 1 | +import { |
| 2 | + ComponentInternalInstance, |
| 3 | + getCurrentInstance, |
| 4 | + render, |
| 5 | + h, |
| 6 | + nodeOps, |
| 7 | + FunctionalComponent, |
| 8 | + defineComponent, |
| 9 | + ref |
| 10 | +} from '@vue/runtime-test' |
| 11 | +import { render as domRender, nextTick } from 'vue' |
| 12 | +import { mockWarn } from '@vue/shared' |
| 13 | + |
| 14 | +describe('component props', () => { |
| 15 | + mockWarn() |
| 16 | + |
| 17 | + test('stateful', () => { |
| 18 | + let props: any |
| 19 | + let attrs: any |
| 20 | + let proxy: any |
| 21 | + |
| 22 | + const Comp = defineComponent({ |
| 23 | + props: ['foo'], |
| 24 | + render() { |
| 25 | + props = this.$props |
| 26 | + attrs = this.$attrs |
| 27 | + proxy = this |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + const root = nodeOps.createElement('div') |
| 32 | + render(h(Comp, { foo: 1, bar: 2 }), root) |
| 33 | + expect(proxy.foo).toBe(1) |
| 34 | + expect(props).toEqual({ foo: 1 }) |
| 35 | + expect(attrs).toEqual({ bar: 2 }) |
| 36 | + |
| 37 | + render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root) |
| 38 | + expect(proxy.foo).toBe(2) |
| 39 | + expect(props).toEqual({ foo: 2 }) |
| 40 | + expect(attrs).toEqual({ bar: 3, baz: 4 }) |
| 41 | + |
| 42 | + render(h(Comp, { qux: 5 }), root) |
| 43 | + expect(proxy.foo).toBeUndefined() |
| 44 | + expect(props).toEqual({}) |
| 45 | + expect(attrs).toEqual({ qux: 5 }) |
| 46 | + }) |
| 47 | + |
| 48 | + test('stateful with setup', () => { |
| 49 | + let props: any |
| 50 | + let attrs: any |
| 51 | + |
| 52 | + const Comp = defineComponent({ |
| 53 | + props: ['foo'], |
| 54 | + setup(_props, { attrs: _attrs }) { |
| 55 | + return () => { |
| 56 | + props = _props |
| 57 | + attrs = _attrs |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + const root = nodeOps.createElement('div') |
| 63 | + render(h(Comp, { foo: 1, bar: 2 }), root) |
| 64 | + expect(props).toEqual({ foo: 1 }) |
| 65 | + expect(attrs).toEqual({ bar: 2 }) |
| 66 | + |
| 67 | + render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root) |
| 68 | + expect(props).toEqual({ foo: 2 }) |
| 69 | + expect(attrs).toEqual({ bar: 3, baz: 4 }) |
| 70 | + |
| 71 | + render(h(Comp, { qux: 5 }), root) |
| 72 | + expect(props).toEqual({}) |
| 73 | + expect(attrs).toEqual({ qux: 5 }) |
| 74 | + }) |
| 75 | + |
| 76 | + test('functional with declaration', () => { |
| 77 | + let props: any |
| 78 | + let attrs: any |
| 79 | + |
| 80 | + const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => { |
| 81 | + props = _props |
| 82 | + attrs = _attrs |
| 83 | + } |
| 84 | + Comp.props = ['foo'] |
| 85 | + |
| 86 | + const root = nodeOps.createElement('div') |
| 87 | + render(h(Comp, { foo: 1, bar: 2 }), root) |
| 88 | + expect(props).toEqual({ foo: 1 }) |
| 89 | + expect(attrs).toEqual({ bar: 2 }) |
| 90 | + |
| 91 | + render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root) |
| 92 | + expect(props).toEqual({ foo: 2 }) |
| 93 | + expect(attrs).toEqual({ bar: 3, baz: 4 }) |
| 94 | + |
| 95 | + render(h(Comp, { qux: 5 }), root) |
| 96 | + expect(props).toEqual({}) |
| 97 | + expect(attrs).toEqual({ qux: 5 }) |
| 98 | + }) |
| 99 | + |
| 100 | + test('functional without declaration', () => { |
| 101 | + let props: any |
| 102 | + let attrs: any |
| 103 | + const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => { |
| 104 | + props = _props |
| 105 | + attrs = _attrs |
| 106 | + } |
| 107 | + const root = nodeOps.createElement('div') |
| 108 | + |
| 109 | + render(h(Comp, { foo: 1 }), root) |
| 110 | + expect(props).toEqual({ foo: 1 }) |
| 111 | + expect(attrs).toEqual({ foo: 1 }) |
| 112 | + expect(props).toBe(attrs) |
| 113 | + |
| 114 | + render(h(Comp, { bar: 2 }), root) |
| 115 | + expect(props).toEqual({ bar: 2 }) |
| 116 | + expect(attrs).toEqual({ bar: 2 }) |
| 117 | + expect(props).toBe(attrs) |
| 118 | + }) |
| 119 | + |
| 120 | + test('boolean casting', () => { |
| 121 | + let proxy: any |
| 122 | + const Comp = { |
| 123 | + props: { |
| 124 | + foo: Boolean, |
| 125 | + bar: Boolean, |
| 126 | + baz: Boolean, |
| 127 | + qux: Boolean |
| 128 | + }, |
| 129 | + render() { |
| 130 | + proxy = this |
| 131 | + } |
| 132 | + } |
| 133 | + render( |
| 134 | + h(Comp, { |
| 135 | + // absent should cast to false |
| 136 | + bar: '', // empty string should cast to true |
| 137 | + baz: 'baz', // same string should cast to true |
| 138 | + qux: 'ok' // other values should be left in-tact (but raise warning) |
| 139 | + }), |
| 140 | + nodeOps.createElement('div') |
| 141 | + ) |
| 142 | + |
| 143 | + expect(proxy.foo).toBe(false) |
| 144 | + expect(proxy.bar).toBe(true) |
| 145 | + expect(proxy.baz).toBe(true) |
| 146 | + expect(proxy.qux).toBe('ok') |
| 147 | + expect('type check failed for prop "qux"').toHaveBeenWarned() |
| 148 | + }) |
| 149 | + |
| 150 | + test('default value', () => { |
| 151 | + let proxy: any |
| 152 | + const Comp = { |
| 153 | + props: { |
| 154 | + foo: { |
| 155 | + default: 1 |
| 156 | + }, |
| 157 | + bar: { |
| 158 | + default: () => ({ a: 1 }) |
| 159 | + } |
| 160 | + }, |
| 161 | + render() { |
| 162 | + proxy = this |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + const root = nodeOps.createElement('div') |
| 167 | + render(h(Comp, { foo: 2 }), root) |
| 168 | + expect(proxy.foo).toBe(2) |
| 169 | + expect(proxy.bar).toEqual({ a: 1 }) |
| 170 | + |
| 171 | + render(h(Comp, { foo: undefined, bar: { b: 2 } }), root) |
| 172 | + expect(proxy.foo).toBe(1) |
| 173 | + expect(proxy.bar).toEqual({ b: 2 }) |
| 174 | + }) |
| 175 | + |
| 176 | + test('optimized props updates', async () => { |
| 177 | + const Child = defineComponent({ |
| 178 | + props: ['foo'], |
| 179 | + template: `<div>{{ foo }}</div>` |
| 180 | + }) |
| 181 | + |
| 182 | + const foo = ref(1) |
| 183 | + const id = ref('a') |
| 184 | + |
| 185 | + const Comp = defineComponent({ |
| 186 | + setup() { |
| 187 | + return { |
| 188 | + foo, |
| 189 | + id |
| 190 | + } |
| 191 | + }, |
| 192 | + components: { Child }, |
| 193 | + template: `<Child :foo="foo" :id="id"/>` |
| 194 | + }) |
| 195 | + |
| 196 | + // Note this one is using the main Vue render so it can compile template |
| 197 | + // on the fly |
| 198 | + const root = document.createElement('div') |
| 199 | + domRender(h(Comp), root) |
| 200 | + expect(root.innerHTML).toBe('<div id="a">1</div>') |
| 201 | + |
| 202 | + foo.value++ |
| 203 | + await nextTick() |
| 204 | + expect(root.innerHTML).toBe('<div id="a">2</div>') |
| 205 | + |
| 206 | + id.value = 'b' |
| 207 | + await nextTick() |
| 208 | + expect(root.innerHTML).toBe('<div id="b">2</div>') |
| 209 | + }) |
| 210 | + |
| 211 | + test('warn props mutation', () => { |
| 212 | + let instance: ComponentInternalInstance |
| 213 | + let setupProps: any |
| 214 | + const Comp = { |
| 215 | + props: ['foo'], |
| 216 | + setup(props: any) { |
| 217 | + instance = getCurrentInstance()! |
| 218 | + setupProps = props |
| 219 | + return () => null |
| 220 | + } |
| 221 | + } |
| 222 | + render(h(Comp, { foo: 1 }), nodeOps.createElement('div')) |
| 223 | + expect(setupProps.foo).toBe(1) |
| 224 | + expect(instance!.props.foo).toBe(1) |
| 225 | + setupProps.foo = 2 |
| 226 | + expect(`Set operation on key "foo" failed`).toHaveBeenWarned() |
| 227 | + expect(() => { |
| 228 | + ;(instance!.proxy as any).foo = 2 |
| 229 | + }).toThrow(TypeError) |
| 230 | + expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned() |
| 231 | + }) |
| 232 | +}) |
0 commit comments