mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-24 21:30:38 +03:00
chore: add/update tests
This commit is contained in:
+118
-10
@@ -1,7 +1,7 @@
|
|||||||
import { MergeContext } from 'src/object-merge'
|
import { createMergedObject, MergeContext } from '../../src/object-merge'
|
||||||
import { createProxy } from '../../src/object-merge/proxy'
|
import { createProxy } from '../../src/object-merge/proxy'
|
||||||
|
|
||||||
describe('proxy', () => {
|
describe('simple proxy operations', () => {
|
||||||
let context: MergeContext<any>
|
let context: MergeContext<any>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -12,7 +12,7 @@ describe('proxy', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('proxy (get)', () => {
|
test('proxy has same structure as target', () => {
|
||||||
const target = {
|
const target = {
|
||||||
str: 'test',
|
str: 'test',
|
||||||
obj: {
|
obj: {
|
||||||
@@ -26,7 +26,7 @@ describe('proxy', () => {
|
|||||||
expect(proxy.obj.str).toBe('test')
|
expect(proxy.obj.str).toBe('test')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('string (set, update, delete)', () => {
|
test('updating the proxy updates the active object in the context', () => {
|
||||||
const target = {}
|
const target = {}
|
||||||
|
|
||||||
const proxy = createProxy(context, target, {})
|
const proxy = createProxy(context, target, {})
|
||||||
@@ -44,7 +44,7 @@ describe('proxy', () => {
|
|||||||
expect(context.active.str).toBeUndefined()
|
expect(context.active.str).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('array (set, update, delete)', () => {
|
test('updating individual elements in an array child on the proxy is supported', () => {
|
||||||
const target = {}
|
const target = {}
|
||||||
|
|
||||||
const proxy = createProxy(context, target, {})
|
const proxy = createProxy(context, target, {})
|
||||||
@@ -67,18 +67,21 @@ describe('proxy', () => {
|
|||||||
expect(context.active.arr).toBeUndefined()
|
expect(context.active.arr).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('proxy (set object)', () => {
|
test('updating an object child on the proxy sets the new object value as active', () => {
|
||||||
const target = {}
|
const target = {}
|
||||||
|
|
||||||
const proxy = createProxy(context, target, {})
|
const proxy = createProxy(context, target, { str2: 'test' })
|
||||||
|
|
||||||
proxy.obj = { str: 'test' }
|
proxy.obj = { str: 'test' }
|
||||||
|
|
||||||
expect(context.active.obj).toBeDefined()
|
expect(context.active.obj).toBeDefined()
|
||||||
expect(context.active.obj.str).toBe('test')
|
expect('str2' in context.active.obj).toBe(false)
|
||||||
|
expect(context.active.obj).toEqual({
|
||||||
|
str: 'test'
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('proxy (remove)', () => {
|
test('removing an object child also removes it from active', () => {
|
||||||
const target = {}
|
const target = {}
|
||||||
|
|
||||||
const proxy = createProxy(context, target, {})
|
const proxy = createProxy(context, target, {})
|
||||||
@@ -92,7 +95,7 @@ describe('proxy', () => {
|
|||||||
expect(context.active.obj).not.toBeDefined()
|
expect(context.active.obj).not.toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('proxy (remove child)', () => {
|
test('removing a property from an object child removes it form active but keeps the object child', () => {
|
||||||
const target = {}
|
const target = {}
|
||||||
|
|
||||||
const proxy = createProxy(context, target, {})
|
const proxy = createProxy(context, target, {})
|
||||||
@@ -106,3 +109,108 @@ describe('proxy', () => {
|
|||||||
expect(context.active.obj).toEqual({})
|
expect(context.active.obj).toEqual({})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('multiple proxy operations', () => {
|
||||||
|
test('two sources are merged into active', () => {
|
||||||
|
const active: Record<string, any> = {}
|
||||||
|
const { addSource } = createMergedObject<typeof active>((values, _contexts) => {
|
||||||
|
const value = values[values.length - 1]
|
||||||
|
return value
|
||||||
|
}, active)
|
||||||
|
|
||||||
|
addSource({
|
||||||
|
str1: 'test',
|
||||||
|
obj: {
|
||||||
|
str1: 'test'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
addSource({
|
||||||
|
str2: 'test',
|
||||||
|
obj: {
|
||||||
|
str2: 'test'
|
||||||
|
}
|
||||||
|
}, {}, true)
|
||||||
|
|
||||||
|
expect(active.str1).not.toBeUndefined()
|
||||||
|
expect(active.str2).not.toBeUndefined()
|
||||||
|
expect(active.obj.str1).not.toBeUndefined()
|
||||||
|
expect(active.obj.str2).not.toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a source can be removed by its source reference', () => {
|
||||||
|
const active: Record<string, any> = {}
|
||||||
|
const { addSource, delSource } = createMergedObject<typeof active>((values, _contexts) => {
|
||||||
|
const value = values[values.length - 1]
|
||||||
|
return value
|
||||||
|
}, active)
|
||||||
|
|
||||||
|
addSource({ str: 'test1' })
|
||||||
|
|
||||||
|
const source2 = { str: 'test2' }
|
||||||
|
addSource(source2, {}, true)
|
||||||
|
|
||||||
|
expect(active.str).toBe('test2')
|
||||||
|
|
||||||
|
delSource(source2)
|
||||||
|
|
||||||
|
expect(active.str).toBe('test1')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a source can be removed by its proxy', () => {
|
||||||
|
const active: Record<string, any> = {}
|
||||||
|
const { compute, addSource, delSource } = createMergedObject<typeof active>((values, _contexts) => {
|
||||||
|
const value = values[values.length - 1]
|
||||||
|
return value
|
||||||
|
}, active)
|
||||||
|
|
||||||
|
addSource({ str: 'test1' })
|
||||||
|
|
||||||
|
const proxy2 = addSource({ str: 'test2' })
|
||||||
|
compute()
|
||||||
|
|
||||||
|
expect(active.str).toBe('test2')
|
||||||
|
|
||||||
|
delSource(proxy2)
|
||||||
|
|
||||||
|
expect(active.str).toBe('test1')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('nested objects on a proxy source recompute all child properties ', () => {
|
||||||
|
const active: Record<string, any> = {}
|
||||||
|
const { compute, addSource, delSource } = createMergedObject<typeof active>((values, _contexts) => {
|
||||||
|
const value = values[values.length - 1]
|
||||||
|
return value
|
||||||
|
}, active)
|
||||||
|
|
||||||
|
addSource({
|
||||||
|
obj: {
|
||||||
|
str1: 'test1',
|
||||||
|
str2: 'empty'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const proxy2 = addSource({
|
||||||
|
obj: {
|
||||||
|
str1: 'test2'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
compute()
|
||||||
|
|
||||||
|
expect(active.obj.str1).toBe('test2')
|
||||||
|
expect(active.obj.str2).toBe('empty')
|
||||||
|
|
||||||
|
proxy2.obj = {
|
||||||
|
str1: 'test3'
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(active.obj.str1).toBe('test3')
|
||||||
|
expect(active.obj.str2).toBe('empty')
|
||||||
|
|
||||||
|
delSource(proxy2)
|
||||||
|
|
||||||
|
expect(active.obj.str1).toBe('test1')
|
||||||
|
expect(active.obj.str2).toBe('empty')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ describe('render', () => {
|
|||||||
const key = 'DescriptionTest'
|
const key = 'DescriptionTest'
|
||||||
const data = 'my description'
|
const data = 'my description'
|
||||||
const config = {
|
const config = {
|
||||||
tag: 'meta'
|
tag: 'meta',
|
||||||
|
keyAttribute: 'name'
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = render.renderMeta(context, key, data, config)
|
const res = render.renderMeta(context, key, data, config)
|
||||||
@@ -72,7 +73,8 @@ describe('render', () => {
|
|||||||
const key = 'DescriptionTest2'
|
const key = 'DescriptionTest2'
|
||||||
const data = { content: 'my description 2' }
|
const data = { content: 'my description 2' }
|
||||||
const config = {
|
const config = {
|
||||||
tag: 'meta'
|
tag: 'meta',
|
||||||
|
keyAttribute: 'name'
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = render.renderMeta(context, key, data, config)
|
const res = render.renderMeta(context, key, data, config)
|
||||||
|
|||||||
Reference in New Issue
Block a user