mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-05-23 19:04:04 +03:00
596 lines
13 KiB
JavaScript
596 lines
13 KiB
JavaScript
import _getMetaInfo from '../src/shared/getMetaInfo'
|
|
import { defaultOptions, loadVueMetaPlugin } from './utils'
|
|
|
|
const getMetaInfo = component => _getMetaInfo(defaultOptions, component)
|
|
|
|
describe('getMetaInfo', () => {
|
|
let Vue
|
|
|
|
beforeAll(() => (Vue = loadVueMetaPlugin()))
|
|
|
|
test('returns appropriate defaults when no meta info is found', () => {
|
|
const component = new Vue()
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: '',
|
|
titleChunk: '',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('returns metaInfo when used in component', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('converts base tag to array', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
base: { href: 'href' }
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [],
|
|
base: [
|
|
{ href: 'href' }
|
|
],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('removes duplicate metaInfo in same component', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'a',
|
|
property: 'a',
|
|
content: 'a'
|
|
},
|
|
{
|
|
vmid: 'a',
|
|
property: 'a',
|
|
content: 'b'
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'a',
|
|
property: 'a',
|
|
content: 'b'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses string titleTemplates', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
titleTemplate: '%s World',
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello World',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s World',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses function titleTemplates', () => {
|
|
const titleTemplate = chunk => `${chunk} Function World`
|
|
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
titleTemplate,
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello Function World',
|
|
titleChunk: 'Hello',
|
|
titleTemplate,
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('has the proper `this` binding when using function titleTemplates', () => {
|
|
const titleTemplate = function (chunk) {
|
|
return `${chunk} ${this.helloWorldText}`
|
|
}
|
|
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
titleTemplate,
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
]
|
|
},
|
|
data() {
|
|
return {
|
|
helloWorldText: 'Function World'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello Function World',
|
|
titleChunk: 'Hello',
|
|
titleTemplate,
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{ charset: 'utf-8' }
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses string meta templates', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: '%s - My page'
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title - My page'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses function meta templates', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: chunk => `${chunk} - My page`
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title - My page'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses content only if template is not defined', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title'
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses content only if template is null', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: null
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses content only if template is false', () => {
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: false
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses meta templates with one-level-deep nested children content', () => {
|
|
Vue.component('merge-child', {
|
|
render: h => h('div'),
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'An important title!'
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: chunk => `${chunk} - My page`
|
|
}
|
|
]
|
|
},
|
|
el: document.createElement('div'),
|
|
render: h => h('div', null, [h('merge-child')])
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'An important title! - My page'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
// TODO: Still failing :( Child template won't be applied if child has no content as well
|
|
|
|
test('properly uses meta templates with one-level-deep nested children template', () => {
|
|
Vue.component('merge-child', {
|
|
render: h => h('div'),
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
template: chunk => `${chunk} - My page`
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: chunk => `${chunk} - SHOULD NEVER HAPPEN`
|
|
}
|
|
]
|
|
},
|
|
el: document.createElement('div'),
|
|
render: h => h('div', null, [h('merge-child')])
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title - My page'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
|
|
test('properly uses meta templates with one-level-deep nested children template and content', () => {
|
|
Vue.component('merge-child', {
|
|
render: h => h('div'),
|
|
metaInfo: {
|
|
title: 'Hello',
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'An important title!',
|
|
template: chunk => `${chunk} - My page`
|
|
}
|
|
]
|
|
}
|
|
})
|
|
|
|
const component = new Vue({
|
|
metaInfo: {
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'Test title',
|
|
template: chunk => `${chunk} - SHOULD NEVER HAPPEN`
|
|
}
|
|
]
|
|
},
|
|
el: document.createElement('div'),
|
|
render: h => h('div', null, [h('merge-child')])
|
|
})
|
|
|
|
expect(getMetaInfo(component)).toEqual({
|
|
title: 'Hello',
|
|
titleChunk: 'Hello',
|
|
titleTemplate: '%s',
|
|
htmlAttrs: {},
|
|
headAttrs: {},
|
|
bodyAttrs: {},
|
|
meta: [
|
|
{
|
|
vmid: 'og:title',
|
|
property: 'og:title',
|
|
content: 'An important title! - My page'
|
|
}
|
|
],
|
|
base: [],
|
|
link: [],
|
|
style: [],
|
|
script: [],
|
|
noscript: [],
|
|
__dangerouslyDisableSanitizers: [],
|
|
__dangerouslyDisableSanitizersByTagID: {}
|
|
})
|
|
})
|
|
})
|