Skip to content

Commit

Permalink
Up
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Mar 2, 2024
1 parent 7abb437 commit 5b2b60d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
39 changes: 33 additions & 6 deletions src/dataSources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,58 @@ export default class DataSourceManager extends ItemManagerModule<ModuleConfig, D
super(em, 'DataSources', new DataSources([], em), DataSourcesEvents);
}

getValue(key: string | string[], defValue: any) {
return get(this.getContext(), key, defValue);
}

/**
* Add new data source.
* @param {Object} props Data source properties.
* @returns {[DataSource]} Added data source.
* @example
* const ds = dsm.add({
* id: 'my_data_source_id',
* records: [
* { id: 'id1', name: 'value1' },
* { id: 'id2', name: 'value2' }
* ]
* });
*/
add(props: DataSourceProps, opts: AddOptions = {}) {
const { all } = this;
props.id = props.id || this._createId();
return all.add(props, opts);
}

/**
* Get data source.
* @param {String} id Data source id.
* @returns {[DataSource]} Data source.
* @example
* const ds = dsm.get('my_data_source_id');
*/
get(id: string) {
return this.all.get(id);
}

/**
* Remove data source.
* @param {String|[DataSource]} id Id of the data source
* @returns {[DataSource]} Removed data source
* @param {String|[DataSource]} id Id of the data source.
* @returns {[DataSource]} Removed data source.
* @example
* const removed = dsm.remove('DS_ID');
*/
remove(id: string | DataSource, opts?: RemoveOptions) {
return this.__remove(id, opts);
}

/**
* Get value from data sources by key
* @param {String} key Path to value.
* @param {any} defValue
* @returns {any}
* const value = dsm.getValue('ds_id.record_id.propName', 'defaultValue');
*/
getValue(key: string | string[], defValue: any) {
return get(this.getContext(), key, defValue);
}

getContext() {
return this.all.reduce((acc, ds) => {
acc[ds.id] = ds.records.reduce((accR, dr, i) => {
Expand Down
18 changes: 15 additions & 3 deletions test/specs/dataSources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ describe('DataSourceManager', () => {
expect(dsm.get(dsTest.id)).toBe(ds);
});

test.todo('remove DataSource');
test.todo('update DataSource');
test.todo('update DataSource record');
test('remove DataSource', () => {
const event = jest.fn();
em.on(dsm.events.remove, event);
const ds = addDataSource();
dsm.remove('ds1');
expect(dsm.getAll().length).toBe(0);
expect(event).toBeCalledTimes(1);
expect(event).toBeCalledWith(ds, expect.any(Object));
});

describe('DataSource with DataVariable component', () => {
let fixtures: HTMLElement;
Expand Down Expand Up @@ -89,6 +95,12 @@ describe('DataSourceManager', () => {
const cmpVar = addDataVariable();
expect(cmpVar.toHTML()).toBe('<div>Name1</div>');
});

test('component exports properly with variable', () => {
addDataSource();
const cmpVar = addDataVariable();
expect(cmpVar.getInnerHTML({ keepVariables: true })).toBe('ds1.id1.name');
});
});

test('component is properly initiliazed with default value', () => {
Expand Down

0 comments on commit 5b2b60d

Please sign in to comment.