2020-03-22 04:01:08 +08:00
|
|
|
import {
|
2020-03-26 23:59:54 +08:00
|
|
|
defineAsyncComponent,
|
2020-03-22 04:01:08 +08:00
|
|
|
h,
|
|
|
|
Component,
|
|
|
|
ref,
|
|
|
|
nextTick,
|
2022-05-12 08:19:11 +08:00
|
|
|
Suspense,
|
|
|
|
KeepAlive
|
2020-03-22 04:01:08 +08:00
|
|
|
} from '../src'
|
|
|
|
import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
|
2022-05-12 08:19:11 +08:00
|
|
|
import { onActivated } from '../src/components/KeepAlive'
|
2020-03-22 04:01:08 +08:00
|
|
|
|
|
|
|
const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
|
|
|
|
|
2020-03-26 23:59:54 +08:00
|
|
|
describe('api: defineAsyncComponent', () => {
|
2020-03-22 04:01:08 +08:00
|
|
|
test('simple usage', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent(
|
2020-03-22 04:01:08 +08:00
|
|
|
() =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
// first time resolve, wait for macro task since there are multiple
|
|
|
|
// microtasks / .then() calls
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// already resolved component should update on nextTick
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with loading component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
}),
|
2020-03-27 08:58:31 +08:00
|
|
|
loadingComponent: () => 'loading',
|
2020-03-22 04:01:08 +08:00
|
|
|
delay: 1 // defaults to 200
|
|
|
|
})
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
// due to the delay, initial mount should be empty
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// loading show up after delay
|
|
|
|
await timeout(1)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// already resolved component should update on nextTick without loading
|
|
|
|
// state
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with loading component + explicit delay (0)', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
}),
|
2020-03-27 08:58:31 +08:00
|
|
|
loadingComponent: () => 'loading',
|
2020-03-22 04:01:08 +08:00
|
|
|
delay: 0
|
|
|
|
})
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
// with delay: 0, should show loading immediately
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// already resolved component should update on nextTick without loading
|
|
|
|
// state
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('error without error component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
let reject: (e: Error) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent(
|
2020-03-22 04:01:08 +08:00
|
|
|
() =>
|
|
|
|
new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
reject = _reject
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
const err = new Error('foo')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
expect(handler.mock.calls[0][0]).toBe(err)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// errored out on previous load, toggle and mock success this time
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// should render this time
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('error with error component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
let reject: (e: Error) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
reject = _reject
|
|
|
|
}),
|
2020-03-27 08:58:31 +08:00
|
|
|
errorComponent: (props: { error: Error }) => props.error.message
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
const err = new Error('errored out')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
2020-03-24 04:14:56 +08:00
|
|
|
expect(handler).toHaveBeenCalled()
|
2020-03-22 04:01:08 +08:00
|
|
|
expect(serializeInner(root)).toBe('errored out')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// errored out on previous load, toggle and mock success this time
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// should render this time
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
2020-09-16 23:10:16 +08:00
|
|
|
// #2129
|
|
|
|
test('error with error component, without global handler', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
let reject: (e: Error) => void
|
|
|
|
const Foo = defineAsyncComponent({
|
|
|
|
loader: () =>
|
|
|
|
new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
reject = _reject
|
|
|
|
}),
|
|
|
|
errorComponent: (props: { error: Error }) => props.error.message
|
|
|
|
})
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
const err = new Error('errored out')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('errored out')
|
|
|
|
expect(
|
|
|
|
'Unhandled error during execution of async component loader'
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// errored out on previous load, toggle and mock success this time
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// should render this time
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
2020-03-22 04:01:08 +08:00
|
|
|
test('error with error + loading components', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
let reject: (e: Error) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
reject = _reject
|
|
|
|
}),
|
2020-03-27 08:58:31 +08:00
|
|
|
errorComponent: (props: { error: Error }) => props.error.message,
|
|
|
|
loadingComponent: () => 'loading',
|
2020-03-22 04:01:08 +08:00
|
|
|
delay: 1
|
|
|
|
})
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo) : null)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
|
|
|
|
// due to the delay, initial mount should be empty
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// loading show up after delay
|
|
|
|
await timeout(1)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
const err = new Error('errored out')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
2020-03-24 04:14:56 +08:00
|
|
|
expect(handler).toHaveBeenCalled()
|
2020-03-22 04:01:08 +08:00
|
|
|
expect(serializeInner(root)).toBe('errored out')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// errored out on previous load, toggle and mock success this time
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// loading show up after delay
|
|
|
|
await timeout(1)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
// should render this time
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('timeout without error component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
}),
|
|
|
|
timeout: 1
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
await timeout(1)
|
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
expect(handler.mock.calls[0][0].message).toMatch(
|
|
|
|
`Async component timed out after 1ms.`
|
|
|
|
)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// if it resolved after timeout, should still work
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('timeout with error component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
}),
|
|
|
|
timeout: 1,
|
2020-03-27 08:58:31 +08:00
|
|
|
errorComponent: () => 'timed out'
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
await timeout(1)
|
2020-03-24 04:14:56 +08:00
|
|
|
expect(handler).toHaveBeenCalled()
|
2020-03-22 04:01:08 +08:00
|
|
|
expect(serializeInner(root)).toBe('timed out')
|
|
|
|
|
|
|
|
// if it resolved after timeout, should still work
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('timeout with error + loading components', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
}),
|
|
|
|
delay: 1,
|
|
|
|
timeout: 16,
|
2020-03-27 08:58:31 +08:00
|
|
|
errorComponent: () => 'timed out',
|
|
|
|
loadingComponent: () => 'loading'
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
2020-03-24 04:14:56 +08:00
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
2020-03-22 04:01:08 +08:00
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
await timeout(1)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
await timeout(16)
|
|
|
|
expect(serializeInner(root)).toBe('timed out')
|
2020-03-24 04:14:56 +08:00
|
|
|
expect(handler).toHaveBeenCalled()
|
2020-03-22 04:01:08 +08:00
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('timeout without error component, but with loading component', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
}),
|
|
|
|
delay: 1,
|
|
|
|
timeout: 16,
|
2020-03-27 08:58:31 +08:00
|
|
|
loadingComponent: () => 'loading'
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
await timeout(1)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
await timeout(16)
|
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
expect(handler.mock.calls[0][0].message).toMatch(
|
|
|
|
`Async component timed out after 16ms.`
|
|
|
|
)
|
|
|
|
// should still display loading
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with suspense', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent(
|
2020-03-22 04:01:08 +08:00
|
|
|
() =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () =>
|
|
|
|
h(Suspense, null, {
|
2020-09-16 00:45:06 +08:00
|
|
|
default: () => h('div', [h(Foo), ' & ', h(Foo)]),
|
2020-03-22 04:01:08 +08:00
|
|
|
fallback: () => 'loading'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
2020-09-16 00:45:06 +08:00
|
|
|
expect(serializeInner(root)).toBe('<div>resolved & resolved</div>')
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('suspensible: false', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent({
|
2020-03-22 04:01:08 +08:00
|
|
|
loader: () =>
|
|
|
|
new Promise(_resolve => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
}),
|
|
|
|
suspensible: false
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () =>
|
|
|
|
h(Suspense, null, {
|
2020-09-16 00:45:06 +08:00
|
|
|
default: () => h('div', [h(Foo), ' & ', h(Foo)]),
|
2020-03-22 04:01:08 +08:00
|
|
|
fallback: () => 'loading'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
// should not show suspense fallback
|
2020-09-16 00:45:06 +08:00
|
|
|
expect(serializeInner(root)).toBe('<div><!----> & <!----></div>')
|
2020-03-22 04:01:08 +08:00
|
|
|
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
2020-09-16 00:45:06 +08:00
|
|
|
expect(serializeInner(root)).toBe('<div>resolved & resolved</div>')
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-24 04:14:56 +08:00
|
|
|
test('suspense with error handling', async () => {
|
|
|
|
let reject: (e: Error) => void
|
2020-03-26 23:59:54 +08:00
|
|
|
const Foo = defineAsyncComponent(
|
2020-03-24 04:14:56 +08:00
|
|
|
() =>
|
|
|
|
new Promise((_resolve, _reject) => {
|
|
|
|
reject = _reject
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () =>
|
|
|
|
h(Suspense, null, {
|
2020-09-16 00:45:06 +08:00
|
|
|
default: () => h('div', [h(Foo), ' & ', h(Foo)]),
|
2020-03-24 04:14:56 +08:00
|
|
|
fallback: () => 'loading'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('loading')
|
|
|
|
|
|
|
|
reject!(new Error('no'))
|
|
|
|
await timeout()
|
|
|
|
expect(handler).toHaveBeenCalled()
|
2020-09-16 00:45:06 +08:00
|
|
|
expect(serializeInner(root)).toBe('<div><!----> & <!----></div>')
|
2020-03-24 04:14:56 +08:00
|
|
|
})
|
2020-03-27 08:58:31 +08:00
|
|
|
|
|
|
|
test('retry (success)', async () => {
|
|
|
|
let loaderCallCount = 0
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
let reject: (e: Error) => void
|
|
|
|
|
|
|
|
const Foo = defineAsyncComponent({
|
|
|
|
loader: () => {
|
|
|
|
loaderCallCount++
|
|
|
|
return new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve as any
|
|
|
|
reject = _reject
|
|
|
|
})
|
|
|
|
},
|
2020-04-08 02:34:42 +08:00
|
|
|
onError(error, retry, fail) {
|
|
|
|
if (error.message.match(/foo/)) {
|
|
|
|
retry()
|
|
|
|
} else {
|
|
|
|
fail()
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 08:58:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(loaderCallCount).toBe(1)
|
|
|
|
|
|
|
|
const err = new Error('foo')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
expect(handler).not.toHaveBeenCalled()
|
|
|
|
expect(loaderCallCount).toBe(2)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// should render this time
|
|
|
|
resolve!(() => 'resolved')
|
|
|
|
await timeout()
|
|
|
|
expect(handler).not.toHaveBeenCalled()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('retry (skipped)', async () => {
|
|
|
|
let loaderCallCount = 0
|
|
|
|
let reject: (e: Error) => void
|
|
|
|
|
|
|
|
const Foo = defineAsyncComponent({
|
|
|
|
loader: () => {
|
|
|
|
loaderCallCount++
|
|
|
|
return new Promise((_resolve, _reject) => {
|
|
|
|
reject = _reject
|
|
|
|
})
|
|
|
|
},
|
2020-04-08 02:34:42 +08:00
|
|
|
onError(error, retry, fail) {
|
|
|
|
if (error.message.match(/bar/)) {
|
|
|
|
retry()
|
|
|
|
} else {
|
|
|
|
fail()
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 08:58:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(loaderCallCount).toBe(1)
|
|
|
|
|
|
|
|
const err = new Error('foo')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
// should fail because retryWhen returns false
|
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
expect(handler.mock.calls[0][0]).toBe(err)
|
|
|
|
expect(loaderCallCount).toBe(1)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
})
|
|
|
|
|
2020-04-08 02:34:42 +08:00
|
|
|
test('retry (fail w/ max retry attempts)', async () => {
|
2020-03-27 08:58:31 +08:00
|
|
|
let loaderCallCount = 0
|
|
|
|
let reject: (e: Error) => void
|
|
|
|
|
|
|
|
const Foo = defineAsyncComponent({
|
|
|
|
loader: () => {
|
|
|
|
loaderCallCount++
|
|
|
|
return new Promise((_resolve, _reject) => {
|
|
|
|
reject = _reject
|
|
|
|
})
|
|
|
|
},
|
2020-04-08 02:34:42 +08:00
|
|
|
onError(error, retry, fail, attempts) {
|
|
|
|
if (error.message.match(/foo/) && attempts <= 1) {
|
|
|
|
retry()
|
|
|
|
} else {
|
|
|
|
fail()
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 08:58:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
|
|
|
render: () => h(Foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = (app.config.errorHandler = jest.fn())
|
|
|
|
app.mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(loaderCallCount).toBe(1)
|
|
|
|
|
|
|
|
// first retry
|
|
|
|
const err = new Error('foo')
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
expect(handler).not.toHaveBeenCalled()
|
|
|
|
expect(loaderCallCount).toBe(2)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
|
|
|
|
// 2nd retry, should fail due to reaching maxRetries
|
|
|
|
reject!(err)
|
|
|
|
await timeout()
|
|
|
|
expect(handler).toHaveBeenCalled()
|
|
|
|
expect(handler.mock.calls[0][0]).toBe(err)
|
|
|
|
expect(loaderCallCount).toBe(2)
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
})
|
2020-12-01 07:59:14 +08:00
|
|
|
|
|
|
|
test('template ref forwarding', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
const Foo = defineAsyncComponent(
|
|
|
|
() =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2021-03-02 04:00:32 +08:00
|
|
|
const fooRef = ref<any>(null)
|
2020-12-01 07:59:14 +08:00
|
|
|
const toggle = ref(true)
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () => (toggle.value ? h(Foo, { ref: fooRef }) : null)
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(fooRef.value).toBe(null)
|
|
|
|
|
|
|
|
resolve!({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
id: 'foo'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render: () => 'resolved'
|
|
|
|
})
|
|
|
|
// first time resolve, wait for macro task since there are multiple
|
|
|
|
// microtasks / .then() calls
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
expect(fooRef.value.id).toBe('foo')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(fooRef.value).toBe(null)
|
|
|
|
|
|
|
|
// already resolved component should update on nextTick
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved')
|
|
|
|
expect(fooRef.value.id).toBe('foo')
|
|
|
|
})
|
2021-03-02 04:00:32 +08:00
|
|
|
|
|
|
|
// #3188
|
|
|
|
test('the forwarded template ref should always exist when doing multi patching', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
const Foo = defineAsyncComponent(
|
|
|
|
() =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const fooRef = ref<any>(null)
|
|
|
|
const toggle = ref(true)
|
|
|
|
const updater = ref(0)
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () =>
|
|
|
|
toggle.value ? [h(Foo, { ref: fooRef }), updater.value] : null
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
expect(serializeInner(root)).toBe('<!---->0')
|
|
|
|
expect(fooRef.value).toBe(null)
|
|
|
|
|
|
|
|
resolve!({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
id: 'foo'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render: () => 'resolved'
|
|
|
|
})
|
|
|
|
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved0')
|
|
|
|
expect(fooRef.value.id).toBe('foo')
|
|
|
|
|
|
|
|
updater.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved1')
|
|
|
|
expect(fooRef.value.id).toBe('foo')
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(fooRef.value).toBe(null)
|
|
|
|
})
|
2021-08-17 03:35:50 +08:00
|
|
|
|
|
|
|
test('vnode hooks on async wrapper', async () => {
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
const Foo = defineAsyncComponent(
|
|
|
|
() =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const updater = ref(0)
|
|
|
|
|
|
|
|
const vnodeHooks = {
|
|
|
|
onVnodeBeforeMount: jest.fn(),
|
|
|
|
onVnodeMounted: jest.fn(),
|
|
|
|
onVnodeBeforeUpdate: jest.fn(),
|
|
|
|
onVnodeUpdated: jest.fn(),
|
|
|
|
onVnodeBeforeUnmount: jest.fn(),
|
|
|
|
onVnodeUnmounted: jest.fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp({
|
|
|
|
render: () => (toggle.value ? [h(Foo, vnodeHooks), updater.value] : null)
|
|
|
|
}).mount(root)
|
|
|
|
|
|
|
|
expect(serializeInner(root)).toBe('<!---->0')
|
|
|
|
|
|
|
|
resolve!({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
id: 'foo'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render: () => 'resolved'
|
|
|
|
})
|
|
|
|
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('resolved0')
|
|
|
|
expect(vnodeHooks.onVnodeBeforeMount).toHaveBeenCalledTimes(1)
|
|
|
|
expect(vnodeHooks.onVnodeMounted).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
updater.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('resolved1')
|
|
|
|
expect(vnodeHooks.onVnodeBeforeUpdate).toHaveBeenCalledTimes(1)
|
|
|
|
expect(vnodeHooks.onVnodeUpdated).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(serializeInner(root)).toBe('<!---->')
|
|
|
|
expect(vnodeHooks.onVnodeBeforeUnmount).toHaveBeenCalledTimes(1)
|
|
|
|
expect(vnodeHooks.onVnodeUnmounted).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2022-05-12 08:19:11 +08:00
|
|
|
|
2022-05-12 11:29:26 +08:00
|
|
|
test('with KeepAlive', async () => {
|
2022-05-12 08:19:11 +08:00
|
|
|
const spy = jest.fn()
|
|
|
|
let resolve: (comp: Component) => void
|
|
|
|
|
|
|
|
const Foo = defineAsyncComponent(
|
|
|
|
() =>
|
|
|
|
new Promise(r => {
|
|
|
|
resolve = r as any
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-05-12 11:29:26 +08:00
|
|
|
const Bar = defineAsyncComponent(() => Promise.resolve(() => 'Bar'))
|
|
|
|
|
|
|
|
const toggle = ref(true)
|
2022-05-12 08:19:11 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const app = createApp({
|
2022-05-12 11:29:26 +08:00
|
|
|
render: () => h(KeepAlive, [toggle.value ? h(Foo) : h(Bar)])
|
2022-05-12 08:19:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
app.mount(root)
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
resolve!({
|
|
|
|
setup() {
|
|
|
|
onActivated(() => {
|
|
|
|
spy()
|
|
|
|
})
|
2022-05-12 11:29:26 +08:00
|
|
|
return () => 'Foo'
|
2022-05-12 08:19:11 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await timeout()
|
2022-05-12 11:29:26 +08:00
|
|
|
expect(serializeInner(root)).toBe('Foo')
|
2022-05-12 08:19:11 +08:00
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
|
2022-05-12 11:29:26 +08:00
|
|
|
toggle.value = false
|
|
|
|
await timeout()
|
|
|
|
expect(serializeInner(root)).toBe('Bar')
|
|
|
|
})
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|