2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-20 00:50:33 +03:00

feat: add possibility to add additional meta info

refactor: server injectors

feat: add head, bodyPrepend, bodyAppend injectors

refactor: create browserbuild through rollup replace (not separate entry)
This commit is contained in:
pimlie
2019-09-13 00:08:21 +02:00
committed by Pim
parent 0e49a9c43e
commit 0ab76ee16b
27 changed files with 389 additions and 387 deletions
+2 -2
View File
@@ -146,7 +146,7 @@ describe('client', () => {
})
test('afterNavigation function is called with refreshOnce: true', async () => {
const Vue = loadVueMetaPlugin(false, { refreshOnceOnNavigation: true })
const Vue = loadVueMetaPlugin({ refreshOnceOnNavigation: true })
const afterNavigation = jest.fn()
const component = Vue.component('nav-component', {
render: h => h('div'),
@@ -181,7 +181,7 @@ describe('client', () => {
})
test('afterNavigation function is called with refreshOnce: false', async () => {
const Vue = loadVueMetaPlugin(false, { refreshOnceOnNavigation: false })
const Vue = loadVueMetaPlugin({ refreshOnceOnNavigation: false })
const afterNavigation = jest.fn()
const component = Vue.component('nav-component', {
render: h => h('div'),
+2 -2
View File
@@ -3,7 +3,7 @@ import { defaultOptions } from '../../src/shared/constants'
import metaInfoData from '../utils/meta-info-data'
import { titleGenerator } from '../../src/server/generators'
const generateServerInjector = metaInfo => _generateServerInjector(defaultOptions, metaInfo)
const generateServerInjector = metaInfo => _generateServerInjector(defaultOptions, metaInfo).injectors
describe('generators', () => {
for (const type in metaInfoData) {
@@ -78,7 +78,7 @@ describe('extra tests', () => {
const title = null
const generatedTitle = titleGenerator({}, 'title', title)
expect(generatedTitle.text()).toEqual('')
expect(generatedTitle).toEqual('')
})
test('auto add ssrAttribute', () => {
+2 -2
View File
@@ -40,7 +40,7 @@ describe('getComponentOption', () => {
})
test('fetches deeply nested component options and merges them', () => {
const localVue = loadVueMetaPlugin(true, { keyName: 'foo' })
const localVue = loadVueMetaPlugin({ keyName: 'foo' })
localVue.component('merge-child', { render: h => h('div'), foo: { bar: 'baz' } })
const component = localVue.component('parent', {
@@ -92,7 +92,7 @@ describe('getComponentOption', () => {
}) */
test('only traverses branches with metaInfo components', () => {
const localVue = loadVueMetaPlugin(false, { keyName: 'foo' })
const localVue = loadVueMetaPlugin({ keyName: 'foo' })
localVue.component('meta-child', {
foo: { bar: 'baz' },
-108
View File
@@ -1,108 +0,0 @@
import { mount, VueMetaServerPlugin, loadVueMetaPlugin } from '../utils'
import { defaultOptions } from '../../src/shared/constants'
jest.mock('../../package.json', () => ({
version: 'test-version'
}))
describe('plugin', () => {
let Vue
beforeEach(() => jest.clearAllMocks())
beforeAll(() => (Vue = loadVueMetaPlugin()))
test('is loaded', () => {
const instance = new Vue({ metaInfo: {} })
expect(instance.$meta).toEqual(expect.any(Function))
expect(instance.$meta().inject).toEqual(expect.any(Function))
expect(instance.$meta().refresh).toEqual(expect.any(Function))
expect(instance.$meta().getOptions).toEqual(expect.any(Function))
expect(instance.$meta().inject()).toBeDefined()
expect(instance.$meta().refresh()).toBeDefined()
const options = instance.$meta().getOptions()
expect(options).toBeDefined()
expect(options.keyName).toBe(defaultOptions.keyName)
})
test('component has _hasMetaInfo set to true', () => {
const Component = Vue.component('test-component', {
template: '<div>Test</div>',
[defaultOptions.keyName]: {
title: 'Hello World'
}
})
const { vm } = mount(Component, { localVue: Vue })
expect(vm._hasMetaInfo).toBe(true)
})
test('plugin sets package version', () => {
expect(VueMetaServerPlugin.version).toBe('test-version')
})
test('plugin isnt be installed twice', () => {
expect(Vue.__vuemeta_installed).toBe(true)
Vue.prototype.$meta = undefined
Vue.use({ ...VueMetaServerPlugin })
expect(Vue.prototype.$meta).toBeUndefined()
// reset Vue
Vue = loadVueMetaPlugin(true)
})
test('prints deprecation warning once when using _hasMetaInfo', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
const Component = Vue.component('test-component', {
template: '<div>Test</div>',
[defaultOptions.keyName]: {
title: 'Hello World'
}
})
Vue.config.devtools = true
const { vm } = mount(Component, { localVue: Vue })
expect(vm._hasMetaInfo).toBe(true)
expect(warn).toHaveBeenCalledTimes(1)
expect(vm._hasMetaInfo).toBe(true)
expect(warn).toHaveBeenCalledTimes(1)
warn.mockRestore()
})
test('can use hasMetaInfo export', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
const Component = Vue.component('test-component', {
template: '<div>Test</div>',
[defaultOptions.keyName]: {
title: 'Hello World'
}
})
const { vm } = mount(Component, { localVue: Vue })
expect(VueMetaServerPlugin.hasMetaInfo(vm)).toBe(true)
expect(warn).not.toHaveBeenCalled()
warn.mockRestore()
})
test('can use generate export', () => {
const rawInfo = {
meta: [{ charset: 'utf-8' }]
}
const metaInfo = VueMetaServerPlugin.generate(rawInfo)
expect(metaInfo.meta.text()).toBe('<meta data-vue-meta="ssr" charset="utf-8">')
// no error on not provided metaInfo types
expect(metaInfo.script.text()).toBe('')
})
})
@@ -1,5 +1,5 @@
import { triggerUpdate, batchUpdate } from '../../src/client/update'
import { mount, vmTick, VueMetaBrowserPlugin, loadVueMetaPlugin } from '../utils'
import { mount, vmTick, VueMetaPlugin, loadVueMetaPlugin } from '../utils'
import { defaultOptions } from '../../src/shared/constants'
jest.mock('../../src/client/update')
@@ -15,6 +15,7 @@ describe('plugin', () => {
test('not loaded when no metaInfo defined', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
process.server = false
const instance = new Vue()
expect(instance.$meta).toEqual(expect.any(Function))
@@ -23,14 +24,16 @@ describe('plugin', () => {
expect(instance.$meta().refresh).toEqual(expect.any(Function))
expect(instance.$meta().getOptions).toEqual(expect.any(Function))
expect(instance.$meta().inject()).not.toBeDefined()
expect(instance.$meta().inject()).toBeUndefined()
expect(warn).toHaveBeenCalledTimes(1)
expect(instance.$meta().refresh()).not.toBeDefined()
expect(instance.$meta().refresh()).toEqual({})
expect(warn).toHaveBeenCalledTimes(2)
instance.$meta().getOptions()
expect(warn).toHaveBeenCalledTimes(3)
expect(warn).toHaveBeenCalledTimes(2)
warn.mockRestore()
delete process.server
})
test('is loaded', () => {
@@ -62,14 +65,14 @@ describe('plugin', () => {
})
test('plugin sets package version', () => {
expect(VueMetaBrowserPlugin.version).toBe('test-version')
expect(VueMetaPlugin.version).toBe('test-version')
})
test('plugin isnt be installed twice', () => {
expect(Vue.__vuemeta_installed).toBe(true)
Vue.prototype.$meta = undefined
Vue.use({ ...VueMetaBrowserPlugin })
Vue.use({ ...VueMetaPlugin })
expect(Vue.prototype.$meta).toBeUndefined()
@@ -77,6 +80,57 @@ describe('plugin', () => {
Vue = loadVueMetaPlugin(true)
})
test('prints deprecation warning once when using _hasMetaInfo', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
const Component = Vue.component('test-component', {
template: '<div>Test</div>',
[defaultOptions.keyName]: {
title: 'Hello World'
}
})
Vue.config.devtools = true
const { vm } = mount(Component, { localVue: Vue })
expect(vm._hasMetaInfo).toBe(true)
expect(warn).toHaveBeenCalledTimes(1)
expect(vm._hasMetaInfo).toBe(true)
expect(warn).toHaveBeenCalledTimes(1)
warn.mockRestore()
})
test('can use hasMetaInfo export', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
const Component = Vue.component('test-component', {
template: '<div>Test</div>',
[defaultOptions.keyName]: {
title: 'Hello World'
}
})
const { vm } = mount(Component, { localVue: Vue })
expect(VueMetaPlugin.hasMetaInfo(vm)).toBe(true)
expect(warn).not.toHaveBeenCalled()
warn.mockRestore()
})
test('can use generate export', () => {
const rawInfo = {
meta: [{ charset: 'utf-8' }]
}
const metaInfo = VueMetaPlugin.generate(rawInfo)
expect(metaInfo.meta.text()).toBe('<meta data-vue-meta="ssr" charset="utf-8">')
// no error on not provided metaInfo types
expect(metaInfo.script.text()).toBe('')
})
test('updates can be paused and resumed', async () => {
const { batchUpdate: _batchUpdate } = jest.requireActual('../../src/client/update')
const batchUpdateSpy = batchUpdate.mockImplementation(_batchUpdate)
+2 -1
View File
@@ -22,7 +22,8 @@ export function getVueMetaPath (browser) {
return path.resolve(__dirname, `../..${browser ? '/dist/vue-meta.js' : ''}`)
}
return path.resolve(__dirname, `../../src${browser ? '/browser' : ''}`)
process.server = !browser
return path.resolve(__dirname, `../../src`)
}
export function webpackRun (config) {
+4 -10
View File
@@ -2,28 +2,22 @@ import { JSDOM } from 'jsdom'
import { mount, shallowMount, createWrapper, createLocalVue } from '@vue/test-utils'
import { renderToString } from '@vue/server-test-utils'
import { defaultOptions } from '../../src/shared/constants'
import VueMetaBrowserPlugin from '../../src/browser'
import VueMetaServerPlugin from '../../src'
import VueMetaPlugin from '../../src'
export {
mount,
shallowMount,
createWrapper,
renderToString,
VueMetaBrowserPlugin,
VueMetaServerPlugin
VueMetaPlugin
}
export function getVue () {
return createLocalVue()
}
export function loadVueMetaPlugin (browser, options, localVue = getVue()) {
if (browser) {
localVue.use(VueMetaBrowserPlugin, Object.assign({}, defaultOptions, options))
} else {
localVue.use(VueMetaServerPlugin, Object.assign({}, defaultOptions, options))
}
export function loadVueMetaPlugin (options, localVue = getVue()) {
localVue.use(VueMetaPlugin, Object.assign({}, defaultOptions, options))
return localVue
}
+2
View File
@@ -1,2 +1,4 @@
process.server = true
jest.useFakeTimers()
jest.setTimeout(30000)