2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-07 14:02:23 +03:00

feat: add pause/resume methods to pause updates

This commit is contained in:
pimlie
2019-02-20 14:46:05 +01:00
parent f270318c29
commit d237180cbd
8 changed files with 107 additions and 37 deletions
+66 -1
View File
@@ -1,5 +1,9 @@
import { mount, defaultOptions, VueMetaBrowserPlugin, loadVueMetaPlugin } from './utils'
import triggerUpdate from '../src/client/triggerUpdate'
import batchUpdate from '../src/client/batchUpdate'
import { mount, defaultOptions, vmTick, VueMetaBrowserPlugin, loadVueMetaPlugin } from './utils'
jest.mock('../src/client/triggerUpdate')
jest.mock('../src/client/batchUpdate')
jest.mock('../package.json', () => ({
version: 'test-version'
}))
@@ -7,6 +11,7 @@ jest.mock('../package.json', () => ({
describe('plugin', () => {
let Vue
beforeEach(() => jest.clearAllMocks())
beforeAll(() => (Vue = loadVueMetaPlugin(true)))
test('is loaded', () => {
@@ -35,4 +40,64 @@ describe('plugin', () => {
test('plugin sets package version', () => {
expect(VueMetaBrowserPlugin.version).toBe('test-version')
})
test('updates can be paused and resumed', async () => {
const _triggerUpdate = jest.requireActual('../src/client/triggerUpdate').default
const triggerUpdateSpy = triggerUpdate.mockImplementation(_triggerUpdate)
const Component = Vue.component('test-component', {
metaInfo() {
return {
title: this.title
}
},
props: {
title: {
type: String,
default: ''
}
},
template: '<div>Test</div>'
})
let title = 'first title'
const wrapper = mount(Component, {
localVue: Vue,
propsData: {
title
}
})
// no batchUpdate on initialization
expect(wrapper.vm.$root._vueMetaInitialized).toBe(false)
expect(wrapper.vm.$root._vueMetaPaused).toBeFalsy()
expect(triggerUpdateSpy).toHaveBeenCalledTimes(1)
expect(batchUpdate).not.toHaveBeenCalled()
jest.clearAllMocks()
await vmTick(wrapper.vm)
title = 'second title'
wrapper.setProps({ title })
// batchUpdate on normal update
expect(wrapper.vm.$root._vueMetaInitialized).toBe(true)
expect(wrapper.vm.$root._vueMetaPaused).toBeFalsy()
expect(triggerUpdateSpy).toHaveBeenCalledTimes(1)
expect(batchUpdate).toHaveBeenCalledTimes(1)
jest.clearAllMocks()
wrapper.vm.$meta().pause()
title = 'third title'
wrapper.setProps({ title })
// no batchUpdate when paused
expect(wrapper.vm.$root._vueMetaInitialized).toBe(true)
expect(wrapper.vm.$root._vueMetaPaused).toBe(true)
expect(triggerUpdateSpy).toHaveBeenCalledTimes(1)
expect(batchUpdate).not.toHaveBeenCalled()
jest.clearAllMocks()
const metaInfo = wrapper.vm.$meta().resume()
expect(metaInfo.title).toBe(title)
})
})