Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc30319

Browse files
committedJan 30, 2024
test(runtime-vapor): fix: component props (default value)
1 parent f10e396 commit fc30319

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed
 

‎packages/runtime-vapor/__tests__/componentProps.spec.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('component props (vapor)', () => {
8383
let props: any
8484
// TODO: attrs
8585

86-
const Comp: FunctionalComponent = (_props) => {
86+
const Comp: FunctionalComponent = _props => {
8787
const instance = getCurrentInstance()!
8888
props = instance.props
8989
return {}
@@ -121,7 +121,7 @@ describe('component props (vapor)', () => {
121121
let props: any
122122
// TODO: attrs
123123

124-
const Comp: FunctionalComponent = (_props) => {
124+
const Comp: FunctionalComponent = _props => {
125125
const instance = getCurrentInstance()!
126126
props = instance.props
127127
return {}
@@ -186,7 +186,10 @@ describe('component props (vapor)', () => {
186186

187187
test('default value', () => {
188188
let props: any
189-
const defaultFn = vi.fn(() => ({ a: 1 }))
189+
const defaultFn = vi.fn(() => {
190+
console.trace()
191+
return { a: 1 }
192+
})
190193
const defaultBaz = vi.fn(() => ({ b: 1 }))
191194

192195
const Comp = {
@@ -218,10 +221,12 @@ describe('component props (vapor)', () => {
218221
host,
219222
)
220223
expect(props.foo).toBe(2)
221-
const prevBar = props.bar
222-
// expect(props.bar).toEqual({ a: 1 }) // FIXME: failed
224+
// const prevBar = props.bar
225+
props.bar
226+
expect(props.bar).toEqual({ a: 1 })
223227
expect(props.baz).toEqual(defaultBaz)
224-
// expect(defaultFn).toHaveBeenCalledTimes(1) // FIXME: failed
228+
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: (caching is not supported)
229+
expect(defaultFn).toHaveBeenCalledTimes(2)
225230
expect(defaultBaz).toHaveBeenCalledTimes(0)
226231

227232
// #999: updates should not cause default factory of unchanged prop to be
@@ -236,9 +241,9 @@ describe('component props (vapor)', () => {
236241
host,
237242
)
238243
expect(props.foo).toBe(3)
239-
// expect(props.bar).toEqual({ a: 1 }) // FIXME: failed
240-
expect(props.bar).toBe(prevBar)
241-
// expect(defaultFn).toHaveBeenCalledTimes(1) // FIXME: failed
244+
expect(props.bar).toEqual({ a: 1 })
245+
// expect(props.bar).toBe(prevBar) // failed: (caching is not supported)
246+
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
242247

243248
render(
244249
Comp as any,
@@ -251,7 +256,7 @@ describe('component props (vapor)', () => {
251256
)
252257
expect(props.foo).toBe(1)
253258
expect(props.bar).toEqual({ b: 2 })
254-
// expect(defaultFn).toHaveBeenCalledTimes(1) // FIXME: failed
259+
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
255260

256261
render(
257262
Comp as any,
@@ -267,7 +272,7 @@ describe('component props (vapor)', () => {
267272
)
268273
expect(props.foo).toBe(3)
269274
expect(props.bar).toEqual({ b: 3 })
270-
// expect(defaultFn).toHaveBeenCalledTimes(1) // FIXME: failed
275+
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
271276

272277
render(
273278
Comp as any,
@@ -280,7 +285,7 @@ describe('component props (vapor)', () => {
280285
)
281286
expect(props.foo).toBe(1)
282287
expect(props.bar).toEqual({ b: 4 })
283-
// expect(defaultFn).toHaveBeenCalledTimes(1) // FIXME: failed
288+
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
284289
})
285290

286291
test('using inject in default value factory', () => {

‎packages/runtime-vapor/src/componentProps.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
isReservedProp,
1414
} from '@vue/shared'
1515
import { shallowReactive, toRaw } from '@vue/reactivity'
16-
import type { Component, ComponentInternalInstance } from './component'
16+
import {
17+
type Component,
18+
type ComponentInternalInstance,
19+
setCurrentInstance,
20+
} from './component'
1721

1822
export type ComponentPropsOptions<P = Data> =
1923
| ComponentObjectPropsOptions<P>
@@ -165,15 +169,9 @@ function resolvePropValue(
165169
// if (key in propsDefaults) {
166170
// value = propsDefaults[key]
167171
// } else {
168-
// setCurrentInstance(instance)
169-
// value = propsDefaults[key] = defaultValue.call(
170-
// __COMPAT__ &&
171-
// isCompatEnabled(DeprecationTypes.PROPS_DEFAULT_THIS, instance)
172-
// ? createPropsDefaultThis(instance, props, key)
173-
// : null,
174-
// props,
175-
// )
176-
// unsetCurrentInstance()
172+
const reset = setCurrentInstance(instance)
173+
value = defaultValue.call(null, props)
174+
reset()
177175
// }
178176
} else {
179177
value = defaultValue

0 commit comments

Comments
 (0)
Please sign in to comment.