2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-12 06:52:24 +03:00

refactor: small improvements (mainly tests)

This commit is contained in:
pimlie
2019-03-08 23:14:24 +01:00
committed by Alexander Lichter
parent 5ad671169b
commit f490a48b99
6 changed files with 116 additions and 24 deletions
+2 -2
View File
@@ -25,8 +25,8 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
}
// 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
// an indication that we need to skip the default behaviour or child has preference over parent
// which means 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)
+23 -18
View File
@@ -109,22 +109,23 @@ describe('client', () => {
})
test('changed function is called', async () => {
const parentComponent = new Vue({ render: h => h('div') })
const wrapper = mount(Changed, { localVue: Vue, parentComponent })
await vmTick(wrapper.vm)
expect(wrapper.vm.$root._vueMeta.initialized).toBe(true)
let context
const changed = jest.fn(function () {
context = this
})
wrapper.setData({ changed, childVisible: true })
const wrapper = mount(Changed, { localVue: Vue, propsData: { changed } })
await vmTick(wrapper.vm)
expect(wrapper.vm.$root._vueMeta.initialized).toBe(true)
// TODO: does changed need to run on initialization?
expect(changed).toHaveBeenCalledTimes(1)
wrapper.setData({ childVisible: true })
jest.runAllTimers()
expect(changed).toHaveBeenCalledTimes(1)
// TODO: this isnt what the docs say
expect(context._uid).not.toBe(wrapper.vm._uid)
expect(changed).toHaveBeenCalledTimes(2)
expect(context._uid).toBe(wrapper.vm._uid)
})
test('afterNavigation function is called', () => {
@@ -136,15 +137,19 @@ describe('client', () => {
})
const guards = {}
Vue.prototype.$router = {
beforeEach(fn) {
guards.before = fn
},
afterEach(fn) {
guards.after = fn
const wrapper = mount(component, {
localVue: Vue,
mocks: {
$router: {
beforeEach(fn) {
guards.before = fn
},
afterEach(fn) {
guards.after = fn
}
}
}
}
const wrapper = mount(component, { localVue: Vue })
})
expect(guards.before).toBeDefined()
expect(guards.after).toBeDefined()
+10 -2
View File
@@ -11,16 +11,24 @@ export default {
components: {
HelloWorld
},
props: {
changed: {
type: Function
}
},
metaInfo() {
return {
changed: this.changed
changed: this._changed
}
},
data() {
return {
childVisible: false,
changed: () => {}
_changed: () => {}
}
},
mounted() {
this._changed = this.changed.bind(this)
}
}
</script>
-2
View File
@@ -483,8 +483,6 @@ describe('getMetaInfo', () => {
})
})
// 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'),
+64
View File
@@ -0,0 +1,64 @@
/**
* @jest-environment node
*/
import { ensureIsArray } from '../src/shared/ensure'
import setOptions from '../src/shared/options'
import { hasGlobalWindowFn } from '../src/shared/window'
import { defaultOptions } from './utils/constants'
const noop = () => {}
describe('shared', () => {
test('ensureIsArray ensures var is array', () => {
let a = { p: 1 }
expect(ensureIsArray(a)).toEqual([])
a = 1
expect(ensureIsArray(a)).toEqual([])
a = [1]
expect(ensureIsArray(a)).toBe(a)
})
test('ensureIsArray ensures obj prop is array', () => {
const a = { p: 1 }
expect(ensureIsArray(a, 'p')).toEqual({ p: [] })
})
test('no error when window is not defined', () => {
expect(hasGlobalWindowFn()).toBe(false)
})
test('can use setOptions', () => {
const keyName = 'MY KEY'
let options = { keyName }
options = setOptions(options)
expect(options.keyName).toBe(keyName)
expect(options.contentKeyName).toBeDefined()
expect(options.contentKeyName).toBe(defaultOptions.contentKeyName)
})
test('setOptions warns when afterNavigation not fn', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(noop)
let options = { afterNavigation: true }
options = setOptions(options)
expect(warn).toHaveBeenCalledTimes(1)
expect(options.afterNavigation).toBeUndefined()
warn.mockRestore()
})
test('setOptions sets refreshOnceOnNavigation when afterNavigation defined', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(noop)
let options = { afterNavigation: noop }
options = setOptions(options)
expect(warn).not.toHaveBeenCalled()
expect(options.refreshOnceOnNavigation).toBe(true)
warn.mockRestore()
})
})
+17
View File
@@ -0,0 +1,17 @@
import {
keyName,
attribute,
ssrAttribute,
tagIDKeyName,
metaTemplateKeyName,
contentKeyName
} from '../../src/shared/constants'
export const defaultOptions = {
keyName,
attribute,
ssrAttribute,
tagIDKeyName,
metaTemplateKeyName,
contentKeyName
}