Browse Source

Format

pull/6419/head
mohamedsalem401 2 years ago
parent
commit
e5fcba3eb8
  1. 16
      packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts
  2. 37
      packages/core/test/specs/data_sources/model/data_collection/ComponentDataCollection.getters-setters.ts

16
packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts

@ -8,19 +8,14 @@ import DataResolverListener from '../DataResolverListener';
import DataSource from '../DataSource'; import DataSource from '../DataSource';
import DataVariable, { DataVariableProps, DataVariableType } from '../DataVariable'; import DataVariable, { DataVariableProps, DataVariableType } from '../DataVariable';
import { isDataVariable } from '../utils'; import { isDataVariable } from '../utils';
import { import { DataCollectionType, keyCollectionDefinition, keyCollectionsStateMap, keyIsCollectionItem } from './constants';
DataCollectionType,
keyCollectionDefinition,
keyCollectionsStateMap,
keyIsCollectionItem
} from './constants';
import { import {
ComponentDataCollectionProps, ComponentDataCollectionProps,
DataCollectionConfig, DataCollectionConfig,
DataCollectionDataSource, DataCollectionDataSource,
DataCollectionProps, DataCollectionProps,
DataCollectionState, DataCollectionState,
DataCollectionStateMap DataCollectionStateMap,
} from './types'; } from './types';
import { getSymbolsToUpdate } from '../../../dom_components/model/SymbolUtils'; import { getSymbolsToUpdate } from '../../../dom_components/model/SymbolUtils';
import { StyleProps, UpdateStyleOptions } from '../../../domain_abstract/model/StyleableModel'; import { StyleProps, UpdateStyleOptions } from '../../../domain_abstract/model/StyleableModel';
@ -113,13 +108,12 @@ export default class ComponentDataCollection extends Component {
setDataSource(dataSource: DataCollectionDataSource) { setDataSource(dataSource: DataCollectionDataSource) {
this.set(keyCollectionDefinition, { this.set(keyCollectionDefinition, {
...this.collectionDef, ...this.collectionDef,
collectionConfig: { ...this.collectionConfig, dataSource } collectionConfig: { ...this.collectionConfig, dataSource },
}); });
} }
private getDataSourceItems() { private getDataSourceItems() {
return this.collectionDef?.collectionConfig ? return this.collectionDef?.collectionConfig ? getDataSourceItems(this.collectionConfig.dataSource, this.em) : [];
getDataSourceItems(this.collectionConfig.dataSource, this.em) : [];
} }
private getCollectionStateMap() { private getCollectionStateMap() {
@ -153,7 +147,7 @@ export default class ComponentDataCollection extends Component {
this.dataSourceWatcher = new DataResolverListener({ this.dataSourceWatcher = new DataResolverListener({
em, em,
resolver: new DataVariable({ type: DataVariableType, path }, { em }), resolver: new DataVariable({ type: DataVariableType, path }, { em }),
onUpdate: this.rebuildChildrenFromCollection onUpdate: this.rebuildChildrenFromCollection,
}); });
} }

37
packages/core/test/specs/data_sources/model/data_collection/ComponentDataCollection.getters-setters.ts

@ -3,7 +3,6 @@ import { DataVariableType } from '../../../../../src/data_sources/model/DataVari
import { import {
DataCollectionType, DataCollectionType,
DataCollectionVariableType, DataCollectionVariableType,
} from '../../../../../src/data_sources/model/data_collection/constants'; } from '../../../../../src/data_sources/model/data_collection/constants';
import { DataCollectionStateVariableType } from '../../../../../src/data_sources/model/data_collection/types'; import { DataCollectionStateVariableType } from '../../../../../src/data_sources/model/data_collection/types';
import EditorModel from '../../../../../src/editor/model/Editor'; import EditorModel from '../../../../../src/editor/model/Editor';
@ -12,7 +11,6 @@ import ComponentDataCollection from '../../../../../src/data_sources/model/data_
describe('Collection component getters and setters', () => { describe('Collection component getters and setters', () => {
let em: EditorModel; let em: EditorModel;
let editor: Editor;
let dsm: DataSourceManager; let dsm: DataSourceManager;
let dataSource: DataSource; let dataSource: DataSource;
let wrapper: Component; let wrapper: Component;
@ -20,7 +18,7 @@ describe('Collection component getters and setters', () => {
let secondRecord: DataRecord; let secondRecord: DataRecord;
beforeEach(() => { beforeEach(() => {
({ em, editor, dsm } = setupTestEditor()); ({ em, dsm } = setupTestEditor());
wrapper = em.getWrapper()!; wrapper = em.getWrapper()!;
dataSource = dsm.add({ dataSource = dsm.add({
id: 'my_data_source_id', id: 'my_data_source_id',
@ -60,7 +58,6 @@ describe('Collection component getters and setters', () => {
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -90,6 +87,7 @@ describe('Collection component getters and setters', () => {
test('getComponentDef should return the correct component definition', () => { test('getComponentDef should return the correct component definition', () => {
const componentDef = cmp.getComponentDef(); const componentDef = cmp.getComponentDef();
expect(componentDef.type).toBe('default'); expect(componentDef.type).toBe('default');
expect(componentDef.components).toHaveLength(1); expect(componentDef.components).toHaveLength(1);
expect(componentDef?.components?.[0].attributes?.['dataUser']).toEqual({ expect(componentDef?.components?.[0].attributes?.['dataUser']).toEqual({
@ -102,10 +100,10 @@ describe('Collection component getters and setters', () => {
test('getDataSource should return the correct data source', () => { test('getDataSource should return the correct data source', () => {
const ds = cmp.getDataSource(); const ds = cmp.getDataSource();
expect(ds).toEqual({ expect(ds).toEqual({
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}); });
}); });
@ -122,12 +120,14 @@ describe('Collection component getters and setters', () => {
test('getConfigStartIndex should handle zero as a valid start index', () => { test('getConfigStartIndex should handle zero as a valid start index', () => {
cmp.setStartIndex(0); cmp.setStartIndex(0);
expect(cmp.getConfigStartIndex()).toBe(0); expect(cmp.getConfigStartIndex()).toBe(0);
expect(cmp.getItemsCount()).toBe(3); expect(cmp.getItemsCount()).toBe(3);
}); });
test('getConfigEndIndex should handle zero as a valid end index', () => { test('getConfigEndIndex should handle zero as a valid end index', () => {
cmp.setEndIndex(0); cmp.setEndIndex(0);
expect(cmp.getConfigStartIndex()).toBe(1); expect(cmp.getConfigStartIndex()).toBe(1);
expect(cmp.getConfigEndIndex()).toBe(0); expect(cmp.getConfigEndIndex()).toBe(0);
expect(cmp.getItemsCount()).toBe(0); expect(cmp.getItemsCount()).toBe(0);
@ -153,11 +153,9 @@ describe('Collection component getters and setters', () => {
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -167,7 +165,6 @@ describe('Collection component getters and setters', () => {
dataSource: { dataSource: {
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}, },
}, },
}, },
@ -187,14 +184,13 @@ describe('Collection component getters and setters', () => {
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'firstName', path: 'firstName',
}, },
}, },
}, },
], ],
}; };
cmp.setComponentDef(newComponentDef); cmp.setComponentDef(newComponentDef);
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(2); expect(children).toHaveLength(2);
expect(children.at(0).get('type')).toBe('newType'); expect(children.at(0).get('type')).toBe('newType');
@ -205,6 +201,7 @@ describe('Collection component getters and setters', () => {
test('setStartIndex should update the start index and reflect in children', () => { test('setStartIndex should update the start index and reflect in children', () => {
cmp.setStartIndex(0); cmp.setStartIndex(0);
expect(cmp.getConfigStartIndex()).toBe(0); expect(cmp.getConfigStartIndex()).toBe(0);
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(3); expect(children).toHaveLength(3);
expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1');
@ -215,6 +212,7 @@ describe('Collection component getters and setters', () => {
test('setEndIndex should update the end index and reflect in children', () => { test('setEndIndex should update the end index and reflect in children', () => {
cmp.setEndIndex(3); cmp.setEndIndex(3);
expect(cmp.getConfigEndIndex()).toBe(3); expect(cmp.getConfigEndIndex()).toBe(3);
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(2); expect(children).toHaveLength(2);
expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user2'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user2');
@ -226,7 +224,7 @@ describe('Collection component getters and setters', () => {
id: 'new_data_source_id', id: 'new_data_source_id',
records: [ records: [
{ id: 'user4', user: 'user4', firstName: 'Name4', age: '20' }, { id: 'user4', user: 'user4', firstName: 'Name4', age: '20' },
{ id: 'user5', user: 'user5', firstName: 'Name5', age: '21' } { id: 'user5', user: 'user5', firstName: 'Name5', age: '21' },
], ],
}); });
@ -242,6 +240,7 @@ describe('Collection component getters and setters', () => {
test('setStartIndex with zero should include the first record', () => { test('setStartIndex with zero should include the first record', () => {
cmp.setStartIndex(0); cmp.setStartIndex(0);
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(3); expect(children).toHaveLength(3);
expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1');
@ -249,6 +248,7 @@ describe('Collection component getters and setters', () => {
test('setEndIndex with zero should result in no children', () => { test('setEndIndex with zero should result in no children', () => {
cmp.setEndIndex(0); cmp.setEndIndex(0);
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(0); expect(children).toHaveLength(0);
}); });
@ -257,13 +257,13 @@ describe('Collection component getters and setters', () => {
dsm.add({ dsm.add({
id: 'empty_data_source_id', id: 'empty_data_source_id',
records: [], records: [],
}); });
cmp.setDataSource({ cmp.setDataSource({
type: DataVariableType, type: DataVariableType,
path: 'empty_data_source_id', path: 'empty_data_source_id',
}); });
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(0); expect(children).toHaveLength(0);
}); });
@ -271,6 +271,7 @@ describe('Collection component getters and setters', () => {
describe('Impact on HTML output', () => { describe('Impact on HTML output', () => {
let cmp: ComponentDataCollection; let cmp: ComponentDataCollection;
beforeEach(() => { beforeEach(() => {
cmp = wrapper.components({ cmp = wrapper.components({
type: DataCollectionType, type: DataCollectionType,
@ -287,11 +288,9 @@ describe('Collection component getters and setters', () => {
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -301,7 +300,6 @@ describe('Collection component getters and setters', () => {
dataSource: { dataSource: {
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}, },
}, },
}, },
@ -310,6 +308,7 @@ describe('Collection component getters and setters', () => {
test('HTML output should reflect changes in startIndex', () => { test('HTML output should reflect changes in startIndex', () => {
cmp.setStartIndex(0); cmp.setStartIndex(0);
const html = cmp.toHTML(); const html = cmp.toHTML();
expect(html).toContain('dataUser="user1"'); expect(html).toContain('dataUser="user1"');
expect(html).toContain('dataUser="user2"'); expect(html).toContain('dataUser="user2"');
@ -318,6 +317,7 @@ describe('Collection component getters and setters', () => {
test('HTML output should reflect changes in endIndex', () => { test('HTML output should reflect changes in endIndex', () => {
cmp.setEndIndex(3); cmp.setEndIndex(3);
const html = cmp.toHTML(); const html = cmp.toHTML();
expect(html).toContain('dataUser="user2"'); expect(html).toContain('dataUser="user2"');
expect(html).toContain('dataUser="user3"'); expect(html).toContain('dataUser="user3"');
@ -328,7 +328,7 @@ describe('Collection component getters and setters', () => {
id: 'new_data_source_id', id: 'new_data_source_id',
records: [ records: [
{ id: 'user4', user: 'user4', firstName: 'Name4', age: '20' }, { id: 'user4', user: 'user4', firstName: 'Name4', age: '20' },
{ id: 'user5', user: 'user5', firstName: 'Name5', age: '21' } { id: 'user5', user: 'user5', firstName: 'Name5', age: '21' },
], ],
}); });
cmp.setDataSource({ cmp.setDataSource({
@ -342,8 +342,9 @@ describe('Collection component getters and setters', () => {
test('HTML output should be empty when endIndex is zero', () => { test('HTML output should be empty when endIndex is zero', () => {
cmp.setEndIndex(0); cmp.setEndIndex(0);
const html = cmp.toHTML(); const html = cmp.toHTML();
expect(html).not.toContain('dataUser'); expect(html).not.toContain('dataUser');
}); });
}); });
}); });

Loading…
Cancel
Save