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

test: increase coverage, add missing tests

fix: issues discovered by adding missing tests
This commit is contained in:
pimlie
2019-03-08 22:13:21 +01:00
committed by Alexander Lichter
parent ce7eaf56d3
commit 5f8025e126
18 changed files with 494 additions and 112 deletions
+47 -48
View File
@@ -2,7 +2,7 @@ import { metaInfoOptionKeys, metaInfoAttributeKeys } from '../shared/constants'
import isArray from '../shared/isArray'
import { updateAttribute, updateTag, updateTitle } from './updaters'
const getTag = (tags, tag) => {
function getTag(tags, tag) {
if (!tags[tag]) {
tags[tag] = document.getElementsByTagName(tag)[0]
}
@@ -23,54 +23,53 @@ export default function updateClientMetaInfo(options = {}, newInfo) {
const htmlTag = getTag(tags, 'html')
// if this is not a server render, then update
if (htmlTag.getAttribute(ssrAttribute) === null) {
// initialize tracked changes
const addedTags = {}
const removedTags = {}
for (const type in newInfo) {
// ignore these
if (metaInfoOptionKeys.includes(type)) {
continue
}
if (type === 'title') {
// update the title
updateTitle(newInfo.title)
continue
}
if (metaInfoAttributeKeys.includes(type)) {
const tagName = type.substr(0, 4)
updateAttribute(options, newInfo[type], getTag(tags, tagName))
continue
}
// tags should always be an array, ignore if it isnt
if (!isArray(newInfo[type])) {
continue
}
const { oldTags, newTags } = updateTag(
options,
type,
newInfo[type],
getTag(tags, 'head'),
getTag(tags, 'body')
)
if (newTags.length) {
addedTags[type] = newTags
removedTags[type] = oldTags
}
}
return { addedTags, removedTags }
} else {
// remove the server render attribute so we can update on changes
// if this is a server render, then dont update
if (htmlTag.getAttribute(ssrAttribute)) {
// remove the server render attribute so we can update on (next) changes
htmlTag.removeAttribute(ssrAttribute)
return false
}
return false
// initialize tracked changes
const addedTags = {}
const removedTags = {}
for (const type in newInfo) {
// ignore these
if (metaInfoOptionKeys.includes(type)) {
continue
}
if (type === 'title') {
// update the title
updateTitle(newInfo.title)
continue
}
if (metaInfoAttributeKeys.includes(type)) {
const tagName = type.substr(0, 4)
updateAttribute(options, newInfo[type], getTag(tags, tagName))
continue
}
// tags should always be an array, ignore if it isnt
if (!isArray(newInfo[type])) {
continue
}
const { oldTags, newTags } = updateTag(
options,
type,
newInfo[type],
getTag(tags, 'head'),
getTag(tags, 'body')
)
if (newTags.length) {
addedTags[type] = newTags
removedTags[type] = oldTags
}
}
return { addedTags, removedTags }
}
-4
View File
@@ -38,10 +38,6 @@ export default function getMetaInfo(options = {}, component, escapeSequences = [
}
disableOptionKeys.forEach((disableKey, index) => {
if (!info[disableKey]) {
return
}
if (index === 0) {
ensureIsArray(info, disableKey)
} else if (index === 1) {
+18 -10
View File
@@ -18,16 +18,25 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
const sourceIndex = source.findIndex(item => item[tagIDKeyName] === targetItem[tagIDKeyName])
const sourceItem = source[sourceIndex]
// source doesnt contain any duplicate id's
// or the source item should be ignored
if (sourceIndex === -1 ||
(sourceItem.hasOwnProperty(contentKeyName) && sourceItem[contentKeyName] === undefined) ||
(sourceItem.hasOwnProperty('innerHTML') && sourceItem.innerHTML === undefined)
) {
// source doesnt contain any duplicate vmid's, we can keep targetItem
if (sourceIndex === -1) {
destination.push(targetItem)
return
}
// when sourceItem explictly defines contentKeyName or innerHTML as undefined, its
// an indication that we need to skip the default behaviour
// So we keep the targetItem and ignore/remove the sourceItem
if ((sourceItem.hasOwnProperty(contentKeyName) && sourceItem[contentKeyName] === undefined) ||
(sourceItem.hasOwnProperty('innerHTML') && sourceItem.innerHTML === undefined)) {
destination.push(targetItem)
// remove current index from source array so its not concatenated to destination below
source.splice(sourceIndex, 1)
return
}
// we now know that targetItem is a duplicate and we should ignore it in favor of sourceItem
// if source specifies null as content then ignore both the target as the source
if (sourceItem[contentKeyName] === null || sourceItem.innerHTML === null) {
// remove current index from source array so its not concatenated to destination below
@@ -35,7 +44,6 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
return
}
// we now know that targetItem is a duplicate and we should ignore it in favor of sourceItem
// now we only need to check if the target has a template to combine it with the source
const targetTemplate = targetItem[metaTemplateKeyName]
if (!targetTemplate) {
@@ -64,9 +72,9 @@ export function merge(target, source, options = {}) {
delete source.title
}
for (const attrKey in metaInfoAttributeKeys) {
metaInfoAttributeKeys.forEach((attrKey) => {
if (!source[attrKey]) {
continue
return
}
for (const key in source[attrKey]) {
@@ -74,7 +82,7 @@ export function merge(target, source, options = {}) {
delete source[attrKey][key]
}
}
}
})
return deepmerge(target, source, {
arrayMerge: (t, s) => arrayMerge(options, t, s)
+2
View File
@@ -114,12 +114,14 @@ export default function createMixin(Vue, options) {
// Wait that element is hidden before refreshing meta tags (to support animations)
const interval = setInterval(() => {
if (this.$el && this.$el.offsetParent !== null) {
/* istanbul ignore next line */
return
}
clearInterval(interval)
if (!this.$parent) {
/* istanbul ignore next line */
return
}