| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- import Vue from 'vue'
- describe('Component async', () => {
- const oldSetTimeout = setTimeout
- const oldClearTimeout = clearTimeout
- // will contain pending timeouts set during the test iteration
- // will contain the id of the timeout as the key, and the millisecond timeout as the value
- // this helps to identify the timeout that is still pending
- let timeoutsPending = {}
- beforeAll(function () {
- // @ts-expect-error
- global.setTimeout = function (func, delay) {
- if (delay >= 1000) {
- // skip vitest internal timeouts
- return
- }
- const id = oldSetTimeout(function () {
- delete timeoutsPending[id]
- func()
- }, delay) as any
- timeoutsPending[id] = delay
- return id
- }
- global.clearTimeout = function (id) {
- oldClearTimeout(id)
- delete timeoutsPending[id]
- }
- })
- afterAll(function () {
- global.setTimeout = oldSetTimeout
- global.clearTimeout = oldClearTimeout
- })
- beforeEach(() => {
- // reset the timeouts for this iteration
- timeoutsPending = {}
- })
- afterEach(() => {
- // after the test is complete no timeouts that have been set up during the test should still be active
- // compare stringified JSON for better error message containing ID and millisecond timeout
- expect(JSON.stringify(timeoutsPending)).toEqual(JSON.stringify({}))
- })
- it('normal', done => {
- const vm = new Vue({
- template: '<div><test></test></div>',
- components: {
- test: resolve => {
- setTimeout(() => {
- resolve({
- template: '<div>hi</div>'
- })
- // wait for parent update
- Vue.nextTick(next)
- }, 0)
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<!---->')
- expect(vm.$children.length).toBe(0)
- function next() {
- expect(vm.$el.innerHTML).toBe('<div>hi</div>')
- expect(vm.$children.length).toBe(1)
- done()
- }
- })
- it('resolve ES module default', done => {
- const vm = new Vue({
- template: '<div><test></test></div>',
- components: {
- test: resolve => {
- setTimeout(() => {
- resolve({
- __esModule: true,
- default: {
- template: '<div>hi</div>'
- }
- })
- // wait for parent update
- Vue.nextTick(next)
- }, 0)
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<!---->')
- expect(vm.$children.length).toBe(0)
- function next() {
- expect(vm.$el.innerHTML).toBe('<div>hi</div>')
- expect(vm.$children.length).toBe(1)
- done()
- }
- })
- it('as root', done => {
- const vm = new Vue({
- template: '<test></test>',
- components: {
- test: resolve => {
- setTimeout(() => {
- resolve({
- template: '<div>hi</div>'
- })
- // wait for parent update
- Vue.nextTick(next)
- }, 0)
- }
- }
- }).$mount()
- expect(vm.$el.nodeType).toBe(8)
- expect(vm.$children.length).toBe(0)
- function next() {
- expect(vm.$el.nodeType).toBe(1)
- expect(vm.$el.outerHTML).toBe('<div>hi</div>')
- expect(vm.$children.length).toBe(1)
- done()
- }
- })
- it('dynamic', done => {
- const vm = new Vue({
- template: '<component :is="view"></component>',
- data: {
- view: 'view-a'
- },
- components: {
- 'view-a': resolve => {
- setTimeout(() => {
- resolve({
- template: '<div>A</div>'
- })
- Vue.nextTick(step1)
- }, 0)
- },
- 'view-b': resolve => {
- setTimeout(() => {
- resolve({
- template: '<p>B</p>'
- })
- Vue.nextTick(step2)
- }, 0)
- }
- }
- }).$mount()
- let aCalled = false
- function step1() {
- // ensure A is resolved only once
- expect(aCalled).toBe(false)
- aCalled = true
- expect(vm.$el.tagName).toBe('DIV')
- expect(vm.$el.textContent).toBe('A')
- vm.view = 'view-b'
- }
- function step2() {
- expect(vm.$el.tagName).toBe('P')
- expect(vm.$el.textContent).toBe('B')
- vm.view = 'view-a'
- waitForUpdate(() => {
- expect(vm.$el.tagName).toBe('DIV')
- expect(vm.$el.textContent).toBe('A')
- }).then(done)
- }
- })
- it('warn reject', () => {
- new Vue({
- template: '<test></test>',
- components: {
- test: (resolve, reject) => {
- reject('nooooo')
- }
- }
- }).$mount()
- expect('Reason: nooooo').toHaveBeenWarned()
- })
- it('with v-for', done => {
- const vm = new Vue({
- template: '<div><test v-for="n in list" :key="n" :n="n"></test></div>',
- data: {
- list: [1, 2, 3]
- },
- components: {
- test: resolve => {
- setTimeout(() => {
- resolve({
- props: ['n'],
- template: '<div>{{n}}</div>'
- })
- Vue.nextTick(next)
- }, 0)
- }
- }
- }).$mount()
- function next() {
- expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
- done()
- }
- })
- it('returning Promise', done => {
- const vm = new Vue({
- template: '<div><test></test></div>',
- components: {
- test: () => {
- return new Promise(resolve => {
- setTimeout(() => {
- resolve({
- template: '<div>hi</div>'
- })
- // wait for promise resolve and then parent update
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 0)
- })
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<!---->')
- expect(vm.$children.length).toBe(0)
- function next() {
- expect(vm.$el.innerHTML).toBe('<div>hi</div>')
- expect(vm.$children.length).toBe(1)
- done()
- }
- })
- describe('loading/error/timeout', () => {
- it('with loading component', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise(resolve => {
- setTimeout(() => {
- resolve({ template: '<div>hi</div>' })
- // wait for promise resolve and then parent update
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 50)
- }),
- loading: { template: `<div>loading</div>` },
- delay: 1
- })
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<!---->')
- let loadingAsserted = false
- setTimeout(() => {
- Vue.nextTick(() => {
- loadingAsserted = true
- expect(vm.$el.textContent).toBe('loading')
- })
- }, 1)
- function next() {
- expect(loadingAsserted).toBe(true)
- expect(vm.$el.textContent).toBe('hi')
- done()
- }
- })
- it('with loading component (0 delay)', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise(resolve => {
- setTimeout(() => {
- resolve({ template: '<div>hi</div>' })
- // wait for promise resolve and then parent update
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 50)
- }),
- loading: { template: `<div>loading</div>` },
- delay: 0
- })
- }
- }).$mount()
- expect(vm.$el.textContent).toBe('loading')
- function next() {
- expect(vm.$el.textContent).toBe('hi')
- done()
- }
- })
- it('with error component', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise((resolve, reject) => {
- setTimeout(() => {
- reject()
- // wait for promise resolve and then parent update
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 50)
- }),
- loading: { template: `<div>loading</div>` },
- error: { template: `<div>error</div>` },
- delay: 0
- })
- }
- }).$mount()
- expect(vm.$el.textContent).toBe('loading')
- function next() {
- expect(`Failed to resolve async component`).toHaveBeenWarned()
- expect(vm.$el.textContent).toBe('error')
- done()
- }
- })
- it('with error component + timeout', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise((resolve, reject) => {
- setTimeout(() => {
- resolve({ template: '<div>hi</div>' })
- // wait for promise resolve and then parent update
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 50)
- }),
- loading: { template: `<div>loading</div>` },
- error: { template: `<div>error</div>` },
- delay: 0,
- timeout: 1
- })
- }
- }).$mount()
- expect(vm.$el.textContent).toBe('loading')
- setTimeout(() => {
- Vue.nextTick(() => {
- expect(`Failed to resolve async component`).toHaveBeenWarned()
- expect(vm.$el.textContent).toBe('error')
- })
- }, 1)
- function next() {
- expect(vm.$el.textContent).toBe('error') // late resolve ignored
- done()
- }
- })
- it('should not trigger timeout if resolved', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise((resolve, reject) => {
- setTimeout(() => {
- resolve({ template: '<div>hi</div>' })
- }, 10)
- }),
- error: { template: `<div>error</div>` },
- timeout: 20
- })
- }
- }).$mount()
- setTimeout(() => {
- expect(vm.$el.textContent).toBe('hi')
- expect(`Failed to resolve async component`).not.toHaveBeenWarned()
- done()
- }, 50)
- })
- it('should not have running timeout/loading if resolved', done => {
- const vm = new Vue({
- template: `<div><test/></div>`,
- components: {
- test: () => ({
- component: new Promise((resolve, reject) => {
- setTimeout(() => {
- resolve({ template: '<div>hi</div>' })
- Promise.resolve().then(() => {
- Vue.nextTick(next)
- })
- }, 10)
- }),
- loading: { template: `<div>loading</div>` },
- delay: 30,
- error: { template: `<div>error</div>` },
- timeout: 40
- })
- }
- }).$mount()
- function next() {
- expect(vm.$el.textContent).toBe('hi')
- // the afterEach() will ensure that the timeouts for delay and timeout have been cleared
- done()
- }
- })
- // #7107
- it(`should work when resolving sync in sibling component's mounted hook`, done => {
- let resolveTwo
- const vm = new Vue({
- template: `<div><one/> <two/></div>`,
- components: {
- one: {
- template: `<div>one</div>`,
- mounted() {
- resolveTwo()
- }
- },
- two: resolve => {
- resolveTwo = () => {
- resolve({
- template: `<div>two</div>`
- })
- }
- }
- }
- }).$mount()
- expect(vm.$el.textContent).toBe('one ')
- waitForUpdate(() => {
- expect(vm.$el.textContent).toBe('one two')
- }).then(done)
- })
- })
- })
|