From 98fb63cf410c3457bec535cff49845bbbe3614c7 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 1 Mar 2024 08:49:28 +0400 Subject: [PATCH] Add remove to dataSources --- src/dataSources/index.ts | 13 +++++- src/dom_components/model/Component.ts | 2 +- test/specs/dataSources/index.ts | 59 +++++++++++++++++---------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/dataSources/index.ts b/src/dataSources/index.ts index f8645b58a..bd33e73cb 100644 --- a/src/dataSources/index.ts +++ b/src/dataSources/index.ts @@ -1,5 +1,5 @@ import { ItemManagerModule, ModuleConfig } from '../abstract/Module'; -import { AddOptions, ObjectAny } from '../common'; +import { AddOptions, ObjectAny, RemoveOptions } from '../common'; import EditorModel from '../editor/model/Editor'; import { get, stringToPath } from '../utils/mixins'; import DataRecord from './model/DataRecord'; @@ -30,6 +30,17 @@ export default class DataSourceManager extends ItemManagerModule { acc[ds.id] = ds.records.reduce((accR, dr, i) => { diff --git a/src/dom_components/model/Component.ts b/src/dom_components/model/Component.ts index 09b378e44..cedecbcf9 100644 --- a/src/dom_components/model/Component.ts +++ b/src/dom_components/model/Component.ts @@ -1114,7 +1114,7 @@ export default class Component extends StyleableModel { * // append at specific index (eg. at the beginning) * someComponent.append(otherComponent, { at: 0 }); */ - append(components: ComponentAdd, opts: AddOptions = {}): Component[] { + append(components: ComponentAdd, opts: AddOptions = {}): T[] { const compArr = isArray(components) ? [...components] : [components]; const toAppend = compArr.map(comp => { if (isString(comp)) { diff --git a/test/specs/dataSources/index.ts b/test/specs/dataSources/index.ts index aa0e17197..5333e00be 100644 --- a/test/specs/dataSources/index.ts +++ b/test/specs/dataSources/index.ts @@ -2,6 +2,7 @@ import Editor from '../../../src/editor/model/Editor'; import DataSourceManager from '../../../src/dataSources'; import { DataSourceProps, DataSourcesEvents } from '../../../src/dataSources/types'; import ComponentWrapper from '../../../src/dom_components/model/ComponentWrapper'; +import ComponentDataVariable from '../../../src/dom_components/model/ComponentDataVariable'; describe('DataSourceManager', () => { let em: Editor; @@ -53,6 +54,13 @@ describe('DataSourceManager', () => { let fixtures: HTMLElement; let cmpRoot: ComponentWrapper; + const addDataVariable = (path = 'ds1.id1.name') => + cmpRoot.append({ + type: 'data-variable', + value: 'default', + path, + })[0]; + beforeEach(() => { document.body.innerHTML = '
'; const { Pages, Components } = em; @@ -69,31 +77,19 @@ describe('DataSourceManager', () => { }); test('component is properly initiliazed with default value', () => { - const cmpVar = cmpRoot.append({ - type: 'data-variable', - value: 'default', - path: 'ds1.id2.name', - })[0]; + const cmpVar = addDataVariable(); expect(cmpVar.getEl()?.innerHTML).toBe('default'); }); test('component is properly initiliazed with current value', () => { dsm.add(dsTest); - const cmpVar = cmpRoot.append({ - type: 'data-variable', - value: 'default', - path: 'ds1.id2.name', - })[0]; - expect(cmpVar.getEl()?.innerHTML).toBe('Name2'); + const cmpVar = addDataVariable(); + expect(cmpVar.getEl()?.innerHTML).toBe('Name1'); }); test('component is properly updating on record add', () => { const ds = dsm.add(dsTest); - const cmpVar = cmpRoot.append({ - type: 'data-variable', - value: 'default', - path: 'ds1[id4]name', - })[0]; + const cmpVar = addDataVariable('ds1[id4]name'); const eventFn = jest.fn(); em.on(`${DataSourcesEvents.path}:ds1.id4.name`, eventFn); const newRecord = ds.addRecord({ id: 'id4', name: 'Name4' }); @@ -104,16 +100,35 @@ describe('DataSourceManager', () => { }); test('component is properly updating on data source add', () => { - const cmpVar = cmpRoot.append({ - type: 'data-variable', - value: 'default', - path: 'ds1.id1.name', - })[0]; + const eventFn = jest.fn(); + em.on(DataSourcesEvents.add, eventFn); + const cmpVar = addDataVariable(); const ds = dsm.add(dsTest); + expect(eventFn).toBeCalledTimes(1); + expect(eventFn).toBeCalledWith(ds, expect.any(Object)); expect(cmpVar.getEl()?.innerHTML).toBe('Name1'); }); - test('component is properly updating on data source reset', () => {}); + test('component is properly updating on data source reset', () => { + dsm.add(dsTest); + const cmpVar = addDataVariable(); + const el = cmpVar.getEl()!; + expect(el.innerHTML).toBe('Name1'); + dsm.all.reset(); + expect(el.innerHTML).toBe('default'); + }); + + test('component is properly updating on data source remove', () => { + const eventFn = jest.fn(); + em.on(DataSourcesEvents.remove, eventFn); + const ds = dsm.add(dsTest); + const cmpVar = addDataVariable(); + const el = cmpVar.getEl()!; + dsm.remove('ds1'); + expect(eventFn).toBeCalledTimes(1); + expect(eventFn).toBeCalledWith(ds, expect.any(Object)); + expect(el.innerHTML).toBe('default'); + }); test('component is properly updating on record change', () => {});