| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700 |
- import {
- defineAsyncComponent,
- h,
- Component,
- ref,
- nextTick,
- Suspense
- } from '../src'
- import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
- const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
- describe('api: defineAsyncComponent', () => {
- test('simple usage', async () => {
- let resolve: (comp: Component) => void
- const Foo = defineAsyncComponent(
- () =>
- 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
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(r => {
- resolve = r as any
- }),
- loadingComponent: () => 'loading',
- 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
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(r => {
- resolve = r as any
- }),
- loadingComponent: () => 'loading',
- 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
- const Foo = defineAsyncComponent(
- () =>
- 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
- 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)
- })
- const handler = (app.config.errorHandler = jest.fn())
- app.mount(root)
- expect(serializeInner(root)).toBe('<!---->')
- const err = new Error('errored out')
- reject!(err)
- await timeout()
- expect(handler).toHaveBeenCalled()
- 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')
- })
- // #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')
- })
- test('error with error + loading components', 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,
- loadingComponent: () => 'loading',
- 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()
- expect(handler).toHaveBeenCalled()
- 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
- const Foo = defineAsyncComponent({
- 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
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(_resolve => {
- resolve = _resolve as any
- }),
- timeout: 1,
- errorComponent: () => 'timed out'
- })
- 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(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
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(_resolve => {
- resolve = _resolve as any
- }),
- delay: 1,
- timeout: 16,
- errorComponent: () => 'timed out',
- loadingComponent: () => 'loading'
- })
- 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(serializeInner(root)).toBe('timed out')
- expect(handler).toHaveBeenCalled()
- resolve!(() => 'resolved')
- await timeout()
- expect(serializeInner(root)).toBe('resolved')
- })
- test('timeout without error component, but with loading component', async () => {
- let resolve: (comp: Component) => void
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(_resolve => {
- resolve = _resolve as any
- }),
- delay: 1,
- timeout: 16,
- loadingComponent: () => 'loading'
- })
- 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
- const Foo = defineAsyncComponent(
- () =>
- new Promise(_resolve => {
- resolve = _resolve as any
- })
- )
- const root = nodeOps.createElement('div')
- const app = createApp({
- render: () =>
- h(Suspense, null, {
- default: () => h('div', [h(Foo), ' & ', h(Foo)]),
- fallback: () => 'loading'
- })
- })
- app.mount(root)
- expect(serializeInner(root)).toBe('loading')
- resolve!(() => 'resolved')
- await timeout()
- expect(serializeInner(root)).toBe('<div>resolved & resolved</div>')
- })
- test('suspensible: false', async () => {
- let resolve: (comp: Component) => void
- const Foo = defineAsyncComponent({
- loader: () =>
- new Promise(_resolve => {
- resolve = _resolve as any
- }),
- suspensible: false
- })
- const root = nodeOps.createElement('div')
- const app = createApp({
- render: () =>
- h(Suspense, null, {
- default: () => h('div', [h(Foo), ' & ', h(Foo)]),
- fallback: () => 'loading'
- })
- })
- app.mount(root)
- // should not show suspense fallback
- expect(serializeInner(root)).toBe('<div><!----> & <!----></div>')
- resolve!(() => 'resolved')
- await timeout()
- expect(serializeInner(root)).toBe('<div>resolved & resolved</div>')
- })
- test('suspense with error handling', async () => {
- let reject: (e: Error) => void
- const Foo = defineAsyncComponent(
- () =>
- new Promise((_resolve, _reject) => {
- reject = _reject
- })
- )
- const root = nodeOps.createElement('div')
- const app = createApp({
- render: () =>
- h(Suspense, null, {
- default: () => h('div', [h(Foo), ' & ', h(Foo)]),
- 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()
- expect(serializeInner(root)).toBe('<div><!----> & <!----></div>')
- })
- 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
- })
- },
- onError(error, retry, fail) {
- if (error.message.match(/foo/)) {
- retry()
- } else {
- fail()
- }
- }
- })
- 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
- })
- },
- onError(error, retry, fail) {
- if (error.message.match(/bar/)) {
- retry()
- } else {
- fail()
- }
- }
- })
- 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('<!---->')
- })
- test('retry (fail w/ max retry attempts)', async () => {
- let loaderCallCount = 0
- let reject: (e: Error) => void
- const Foo = defineAsyncComponent({
- loader: () => {
- loaderCallCount++
- return new Promise((_resolve, _reject) => {
- reject = _reject
- })
- },
- onError(error, retry, fail, attempts) {
- if (error.message.match(/foo/) && attempts <= 1) {
- retry()
- } else {
- fail()
- }
- }
- })
- 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('<!---->')
- })
- test('template ref forwarding', async () => {
- let resolve: (comp: Component) => void
- const Foo = defineAsyncComponent(
- () =>
- new Promise(r => {
- resolve = r as any
- })
- )
- const fooRef = ref()
- 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')
- })
- })
|