Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/runtime-core/__tests__/apiAsyncComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,4 +960,31 @@ describe('api: defineAsyncComponent', () => {
expect(serializeInner(root)).toBe('Foo')
expect(spy).toBeCalledTimes(1)
})

test('with loading component + default delay', async () => {
vi.useFakeTimers()
try {
const Foo = defineAsyncComponent({
loader: () => new Promise(() => {}),
loadingComponent: () => 'loading',
})

const root = nodeOps.createElement('div')
createApp({
render: () => h(Foo),
}).mount(root)

// loading should wait for the default 200ms delay
expect(serializeInner(root)).toBe('<!---->')
vi.advanceTimersByTime(199)
await nextTick()
expect(serializeInner(root)).toBe('<!---->')

vi.advanceTimersByTime(1)
await nextTick()
expect(serializeInner(root)).toBe('loading')
} finally {
vi.useRealTimers()
}
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiAsyncComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export function createAsyncComponentContext<T, C = ConcreteComponent>(

// shared between core and vapor
export const useAsyncComponentState = (
delay: number | undefined,
delay: number | undefined = 200,
timeout: number | undefined,
onError: (err: Error) => void,
instance: GenericComponentInstance | null = currentInstance,
Expand Down
28 changes: 28 additions & 0 deletions packages/runtime-vapor/__tests__/apiDefineAsyncComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,34 @@ describe('api: defineAsyncComponent', () => {
expect(errors).toEqual([failure])
app.unmount()
})

test('with loading component + default delay', async () => {
vi.useFakeTimers()
try {
const Foo = defineVaporAsyncComponent({
loader: () => new Promise(() => {}),
loadingComponent: () => template('loading')(),
})

const { html } = define({
setup() {
return createComponent(Foo)
},
}).render()

// loading should wait for the default 200ms delay
expect(html()).toBe('<!--async component-->')
vi.advanceTimersByTime(199)
await nextTick()
expect(html()).toBe('<!--async component-->')

vi.advanceTimersByTime(1)
await nextTick()
expect(html()).toBe('loading<!--async component-->')
} finally {
vi.useRealTimers()
}
})
})

function mountAsyncError(
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6119,6 +6119,7 @@ describe('vdomInterop', () => {
})
},
loadingComponent: () => h('span', 'loading...'),
delay: 0,
})

const { html } = define({
Expand Down
Loading