/** * @jest-environment jsdom */ import { resolve } from 'path' import { mount, createLocalVue } from '@vue/test-utils' import { renderToString } from '@vue/server-test-utils' import { loadFixture } from '../../../test/utils' import { vmTick } from './__utils__' jest.useFakeTimers() describe('nuxt-loading', () => { let localVue let Component beforeAll(async () => { const config = await loadFixture('basic') const componentDir = resolve(config.rootDir, '.nuxt/components') localVue = createLocalVue() Component = (await import(resolve(componentDir, 'nuxt-loading.vue'))).default }) afterEach(() => jest.clearAllTimers()) test('removed when not loading', () => { const str = renderToString(Component) expect(str).toBe('') }) test('added when loading', () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ throttle: 0 }) wrapper.vm.start() expect(wrapper.html()).toBe('
') }) test('percentage changed after 1s', () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ duration: 1000, throttle: 0 }) wrapper.vm.start() jest.advanceTimersByTime(250) const html = wrapper.html() expect(html).not.toBe('') expect(wrapper.vm.get()).not.toBe(0) }) test('can be finished', async () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ duration: 1000, throttle: 0 }) wrapper.vm.start() wrapper.vm.finish() let html = wrapper.html() expect(html).toBe('') expect(wrapper.vm.get()).toBe(100) jest.runAllTimers() await vmTick(wrapper.vm) html = wrapper.html() expect(html).toBeUndefined() expect(wrapper.vm.get()).toBe(0) }) test('can fail', () => { const wrapper = mount(Component, { localVue }) wrapper.vm.set(50) wrapper.vm.fail() const html = wrapper.html() expect(html).toBe('') }) test('not shown until throttle', () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ duration: 2000, throttle: 1000 }) wrapper.vm.start() jest.advanceTimersByTime(250) let html = wrapper.html() expect(html).toBeUndefined() jest.advanceTimersByTime(1000) html = wrapper.html() expect(html).not.toBe('') expect(html).toContain('