/** * @jest-environment jsdom */ import { mount, createLocalVue } from '@vue/test-utils' import { renderToString } from '@vue/server-test-utils' import { compileTemplate, importComponent, vmTick } from './__utils__' jest.useFakeTimers() describe('nuxt-loading', () => { let localVue let Component beforeAll(async () => { const compiledTemplate = await compileTemplate('components/nuxt-loading.vue') Component = await importComponent(compiledTemplate) localVue = createLocalVue() }) afterEach(() => jest.clearAllTimers()) test('removed when not loading', async () => { const str = await renderToString(Component) expect(str).toBe('') }) test('added when loading', async () => { const wrapper = mount(Component) wrapper.setData({ throttle: 0 }) wrapper.vm.start() await vmTick(wrapper.vm) expect(wrapper.html()).toBe('
') }) test('percentage changed after 1s', async () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ duration: 1000, throttle: 0 }) wrapper.vm.start() jest.advanceTimersByTime(250) await vmTick(wrapper.vm) 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() await vmTick(wrapper.vm) let html = wrapper.html() expect(html).toBe('') expect(wrapper.vm.get()).toBe(100) jest.runAllTimers() await vmTick(wrapper.vm) html = wrapper.html() expect(html).toBe('') expect(wrapper.vm.get()).toBe(0) }) test('can fail', async () => { const wrapper = mount(Component, { localVue }) wrapper.vm.set(50) wrapper.vm.fail() await vmTick(wrapper.vm) const html = wrapper.html() expect(html).toBe('') }) test('not shown until throttle', async () => { const wrapper = mount(Component, { localVue }) wrapper.setData({ duration: 2000, throttle: 1000 }) wrapper.vm.start() await vmTick(wrapper.vm) jest.advanceTimersByTime(250) await vmTick(wrapper.vm) let html = wrapper.html() expect(html).toBe('') jest.advanceTimersByTime(1000) await vmTick(wrapper.vm) html = wrapper.html() expect(html).not.toBe('') expect(html).toContain('