diff --git a/test/unit/object-merge.test.ts b/test/unit/object-merge.test.ts index c1df993..b931709 100644 --- a/test/unit/object-merge.test.ts +++ b/test/unit/object-merge.test.ts @@ -1,7 +1,7 @@ -import { MergeContext } from 'src/object-merge' +import { createMergedObject, MergeContext } from '../../src/object-merge' import { createProxy } from '../../src/object-merge/proxy' -describe('proxy', () => { +describe('simple proxy operations', () => { let context: MergeContext beforeEach(() => { @@ -12,7 +12,7 @@ describe('proxy', () => { } }) - test('proxy (get)', () => { + test('proxy has same structure as target', () => { const target = { str: 'test', obj: { @@ -26,7 +26,7 @@ describe('proxy', () => { 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 proxy = createProxy(context, target, {}) @@ -44,7 +44,7 @@ describe('proxy', () => { 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 proxy = createProxy(context, target, {}) @@ -67,18 +67,21 @@ describe('proxy', () => { 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 proxy = createProxy(context, target, {}) + const proxy = createProxy(context, target, { str2: 'test' }) proxy.obj = { str: 'test' } 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 proxy = createProxy(context, target, {}) @@ -92,7 +95,7 @@ describe('proxy', () => { 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 proxy = createProxy(context, target, {}) @@ -106,3 +109,108 @@ describe('proxy', () => { expect(context.active.obj).toEqual({}) }) }) + +describe('multiple proxy operations', () => { + test('two sources are merged into active', () => { + const active: Record = {} + const { addSource } = createMergedObject((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 = {} + const { addSource, delSource } = createMergedObject((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 = {} + const { compute, addSource, delSource } = createMergedObject((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 = {} + const { compute, addSource, delSource } = createMergedObject((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') + }) +}) diff --git a/test/unit/render.test.ts b/test/unit/render.test.ts index 598f438..deb0d22 100644 --- a/test/unit/render.test.ts +++ b/test/unit/render.test.ts @@ -54,7 +54,8 @@ describe('render', () => { const key = 'DescriptionTest' const data = 'my description' const config = { - tag: 'meta' + tag: 'meta', + keyAttribute: 'name' } const res = render.renderMeta(context, key, data, config) @@ -72,7 +73,8 @@ describe('render', () => { const key = 'DescriptionTest2' const data = { content: 'my description 2' } const config = { - tag: 'meta' + tag: 'meta', + keyAttribute: 'name' } const res = render.renderMeta(context, key, data, config)