mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-21 06:40:34 +03:00
feat: support generating tags directly from metaInfo object
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { getComponentMetaInfo } from '../../src/shared/getComponentOption'
|
||||
import _getMetaInfo from '../../src/shared/getMetaInfo'
|
||||
import { mount, createWrapper, loadVueMetaPlugin, vmTick } from '../utils'
|
||||
import { defaultOptions } from '../../src/shared/constants'
|
||||
@@ -7,7 +8,7 @@ import HelloWorld from '../components/hello-world.vue'
|
||||
import KeepAlive from '../components/keep-alive.vue'
|
||||
import Changed from '../components/changed.vue'
|
||||
|
||||
const getMetaInfo = component => _getMetaInfo(defaultOptions, component)
|
||||
const getMetaInfo = component => _getMetaInfo(defaultOptions, getComponentMetaInfo(defaultOptions, component))
|
||||
|
||||
jest.mock('../../src/utils/window', () => ({
|
||||
hasGlobalWindow: false
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { getComponentMetaInfo } from '../../src/shared/getComponentOption'
|
||||
import _getMetaInfo from '../../src/shared/getMetaInfo'
|
||||
import { loadVueMetaPlugin } from '../utils'
|
||||
import { defaultOptions } from '../../src/shared/constants'
|
||||
import { serverSequences } from '../../src/shared/escaping'
|
||||
|
||||
const getMetaInfo = (component, escapeSequences) => _getMetaInfo(defaultOptions, component, escapeSequences)
|
||||
const getMetaInfo = (component, escapeSequences) => _getMetaInfo(defaultOptions, getComponentMetaInfo(defaultOptions, component), escapeSequences)
|
||||
|
||||
describe('escaping', () => {
|
||||
let Vue
|
||||
|
||||
@@ -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 = (type, data) => _generateServerInjector(defaultOptions, type, data)
|
||||
const generateServerInjector = metaInfo => _generateServerInjector(defaultOptions, metaInfo)
|
||||
|
||||
describe('generators', () => {
|
||||
for (const type in metaInfoData) {
|
||||
@@ -34,9 +34,9 @@ describe('generators', () => {
|
||||
}
|
||||
|
||||
const defaultTestFn = () => {
|
||||
const tags = generateServerInjector(type, testInfo.data)
|
||||
testCases[action](tags)
|
||||
return tags
|
||||
const newInfo = generateServerInjector({ [type]: testInfo.data })
|
||||
testCases[action](newInfo[type])
|
||||
return newInfo[type]
|
||||
}
|
||||
|
||||
let testFn
|
||||
@@ -62,6 +62,18 @@ describe('generators', () => {
|
||||
})
|
||||
|
||||
describe('extra tests', () => {
|
||||
test('empty config doesnt generate a tag', () => {
|
||||
const { meta } = generateServerInjector({ meta: [] })
|
||||
|
||||
expect(meta.text()).toEqual('')
|
||||
})
|
||||
|
||||
test('config with empty object doesnt generate a tag', () => {
|
||||
const { meta } = generateServerInjector({ meta: [{}] })
|
||||
|
||||
expect(meta.text()).toEqual('')
|
||||
})
|
||||
|
||||
test('title generator should return an empty string when title is null', () => {
|
||||
const title = null
|
||||
const generatedTitle = titleGenerator({}, 'title', title)
|
||||
@@ -70,19 +82,19 @@ describe('extra tests', () => {
|
||||
})
|
||||
|
||||
test('auto add ssrAttribute', () => {
|
||||
const htmlAttrs = generateServerInjector('htmlAttrs', {})
|
||||
const { htmlAttrs } = generateServerInjector({ htmlAttrs: {} })
|
||||
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
|
||||
|
||||
const headAttrs = generateServerInjector('headAttrs', {})
|
||||
const { headAttrs } = generateServerInjector({ headAttrs: {} })
|
||||
expect(headAttrs.text(true)).toBe('')
|
||||
|
||||
const bodyAttrs = generateServerInjector('bodyAttrs', {})
|
||||
const { bodyAttrs } = generateServerInjector({ bodyAttrs: {} })
|
||||
expect(bodyAttrs.text(true)).toBe('')
|
||||
})
|
||||
|
||||
test('script prepend body', () => {
|
||||
const tags = [{ src: '/script.js', pbody: true }]
|
||||
const scriptTags = generateServerInjector('script', tags)
|
||||
const { script: scriptTags } = generateServerInjector({ script: tags })
|
||||
|
||||
expect(scriptTags.text()).toBe('')
|
||||
expect(scriptTags.text({ body: true })).toBe('')
|
||||
@@ -91,7 +103,7 @@ describe('extra tests', () => {
|
||||
|
||||
test('script append body', () => {
|
||||
const tags = [{ src: '/script.js', body: true }]
|
||||
const scriptTags = generateServerInjector('script', tags)
|
||||
const { script: scriptTags } = generateServerInjector({ script: tags })
|
||||
|
||||
expect(scriptTags.text()).toBe('')
|
||||
expect(scriptTags.text({ body: true })).toBe('<script data-vue-meta="ssr" src="/script.js" data-body="true"></script>')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import getComponentOption from '../../src/shared/getComponentOption'
|
||||
import { getComponentOption } from '../../src/shared/getComponentOption'
|
||||
import { inMetaInfoBranch } from '../../src/shared/meta-helpers'
|
||||
import { mount, getVue, loadVueMetaPlugin } from '../utils'
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { getComponentMetaInfo } from '../../src/shared/getComponentOption'
|
||||
import _getMetaInfo from '../../src/shared/getMetaInfo'
|
||||
import { loadVueMetaPlugin } from '../utils'
|
||||
import { defaultOptions } from '../../src/shared/constants'
|
||||
|
||||
const getMetaInfo = component => _getMetaInfo(defaultOptions, component)
|
||||
const getMetaInfo = component => _getMetaInfo(defaultOptions, getComponentMetaInfo(defaultOptions, component), [], component)
|
||||
|
||||
describe('getMetaInfo', () => {
|
||||
let Vue
|
||||
|
||||
@@ -93,4 +93,16 @@ describe('plugin', () => {
|
||||
|
||||
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('')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -225,13 +225,6 @@ const metaInfoData = {
|
||||
data: {},
|
||||
expect: ['<body>']
|
||||
}
|
||||
},
|
||||
empty: {
|
||||
add: {
|
||||
data: [{}],
|
||||
expect: [''],
|
||||
test: side => side === 'server'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user