mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-24 09:30:34 +03:00
refactor: small improvements (mainly tests)
This commit is contained in:
committed by
Alexander Lichter
parent
5ad671169b
commit
f490a48b99
+2
-2
@@ -25,8 +25,8 @@ export function arrayMerge({ component, tagIDKeyName, metaTemplateKeyName, conte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// when sourceItem explictly defines contentKeyName or innerHTML as undefined, its
|
// when sourceItem explictly defines contentKeyName or innerHTML as undefined, its
|
||||||
// an indication that we need to skip the default behaviour
|
// an indication that we need to skip the default behaviour or child has preference over parent
|
||||||
// So we keep the targetItem and ignore/remove the sourceItem
|
// which means we keep the targetItem and ignore/remove the sourceItem
|
||||||
if ((sourceItem.hasOwnProperty(contentKeyName) && sourceItem[contentKeyName] === undefined) ||
|
if ((sourceItem.hasOwnProperty(contentKeyName) && sourceItem[contentKeyName] === undefined) ||
|
||||||
(sourceItem.hasOwnProperty('innerHTML') && sourceItem.innerHTML === undefined)) {
|
(sourceItem.hasOwnProperty('innerHTML') && sourceItem.innerHTML === undefined)) {
|
||||||
destination.push(targetItem)
|
destination.push(targetItem)
|
||||||
|
|||||||
+23
-18
@@ -109,22 +109,23 @@ describe('client', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('changed function is called', async () => {
|
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
|
let context
|
||||||
const changed = jest.fn(function () {
|
const changed = jest.fn(function () {
|
||||||
context = this
|
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()
|
jest.runAllTimers()
|
||||||
|
|
||||||
expect(changed).toHaveBeenCalledTimes(1)
|
expect(changed).toHaveBeenCalledTimes(2)
|
||||||
// TODO: this isnt what the docs say
|
expect(context._uid).toBe(wrapper.vm._uid)
|
||||||
expect(context._uid).not.toBe(wrapper.vm._uid)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('afterNavigation function is called', () => {
|
test('afterNavigation function is called', () => {
|
||||||
@@ -136,15 +137,19 @@ describe('client', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const guards = {}
|
const guards = {}
|
||||||
Vue.prototype.$router = {
|
const wrapper = mount(component, {
|
||||||
beforeEach(fn) {
|
localVue: Vue,
|
||||||
guards.before = fn
|
mocks: {
|
||||||
},
|
$router: {
|
||||||
afterEach(fn) {
|
beforeEach(fn) {
|
||||||
guards.after = fn
|
guards.before = fn
|
||||||
|
},
|
||||||
|
afterEach(fn) {
|
||||||
|
guards.after = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
const wrapper = mount(component, { localVue: Vue })
|
|
||||||
|
|
||||||
expect(guards.before).toBeDefined()
|
expect(guards.before).toBeDefined()
|
||||||
expect(guards.after).toBeDefined()
|
expect(guards.after).toBeDefined()
|
||||||
|
|||||||
Vendored
+10
-2
@@ -11,16 +11,24 @@ export default {
|
|||||||
components: {
|
components: {
|
||||||
HelloWorld
|
HelloWorld
|
||||||
},
|
},
|
||||||
|
props: {
|
||||||
|
changed: {
|
||||||
|
type: Function
|
||||||
|
}
|
||||||
|
},
|
||||||
metaInfo() {
|
metaInfo() {
|
||||||
return {
|
return {
|
||||||
changed: this.changed
|
changed: this._changed
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
childVisible: false,
|
childVisible: false,
|
||||||
changed: () => {}
|
_changed: () => {}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this._changed = this.changed.bind(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -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', () => {
|
test('properly uses meta templates with one-level-deep nested children template', () => {
|
||||||
Vue.component('merge-child', {
|
Vue.component('merge-child', {
|
||||||
render: h => h('div'),
|
render: h => h('div'),
|
||||||
|
|||||||
@@ -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()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import {
|
||||||
|
keyName,
|
||||||
|
attribute,
|
||||||
|
ssrAttribute,
|
||||||
|
tagIDKeyName,
|
||||||
|
metaTemplateKeyName,
|
||||||
|
contentKeyName
|
||||||
|
} from '../../src/shared/constants'
|
||||||
|
|
||||||
|
export const defaultOptions = {
|
||||||
|
keyName,
|
||||||
|
attribute,
|
||||||
|
ssrAttribute,
|
||||||
|
tagIDKeyName,
|
||||||
|
metaTemplateKeyName,
|
||||||
|
contentKeyName
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user