Browse Source

Update data collection tests

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

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

@ -3,6 +3,7 @@ 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';
@ -27,6 +28,7 @@ describe('Collection component getters and setters', () => {
{ id: 'user1', user: 'user1', firstName: 'Name1', age: '12' }, { id: 'user1', user: 'user1', firstName: 'Name1', age: '12' },
{ id: 'user2', user: 'user2', firstName: 'Name2', age: '14' }, { id: 'user2', user: 'user2', firstName: 'Name2', age: '14' },
{ id: 'user3', user: 'user3', firstName: 'Name3', age: '16' }, { id: 'user3', user: 'user3', firstName: 'Name3', age: '16' },
], ],
}); });
@ -52,14 +54,16 @@ describe('Collection component getters and setters', () => {
type: 'default', type: 'default',
tagName: 'div', tagName: 'div',
attributes: { attributes: {
'data-user': { dataUser: {
type: DataCollectionVariableType, type: DataCollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -69,6 +73,7 @@ describe('Collection component getters and setters', () => {
dataSource: { dataSource: {
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}, },
}, },
}, },
@ -77,25 +82,29 @@ describe('Collection component getters and setters', () => {
test('getItemsCount should return the correct number of items', () => { test('getItemsCount should return the correct number of items', () => {
expect(cmp.getItemsCount()).toBe(2); expect(cmp.getItemsCount()).toBe(2);
}); });
test('getConfigStartIndex should return the correct start index', () => { test('getConfigStartIndex should return the correct start index', () => {
expect(cmp.getConfigStartIndex()).toBe(1); expect(cmp.getConfigStartIndex()).toBe(1);
}); });
test('getConfigEndIndex should return the correct end index', () => { test('getConfigEndIndex should return the correct end index', () => {
expect(cmp.getConfigEndIndex()).toBe(2); expect(cmp.getConfigEndIndex()).toBe(2);
}); });
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.attributes?.['data-user']).toEqual({ expect(componentDef.attributes?.['dataUser']).toEqual({
type: DataCollectionVariableType, type: DataCollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}); });
}); });
@ -104,11 +113,13 @@ describe('Collection component getters and setters', () => {
expect(ds).toEqual({ expect(ds).toEqual({
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}); });
}); });
test('getCollectionId should return the correct collection ID', () => { test('getCollectionId should return the correct collection ID', () => {
expect(cmp.getCollectionId()).toBe('my_collection'); expect(cmp.getCollectionId()).toBe('my_collection');
}); });
test('getItemsCount should return 0 when no records are present', () => { test('getItemsCount should return 0 when no records are present', () => {
@ -116,25 +127,28 @@ describe('Collection component getters and setters', () => {
dataSource.removeRecord('user2'); dataSource.removeRecord('user2');
dataSource.removeRecord('user3'); dataSource.removeRecord('user3');
expect(cmp.getItemsCount()).toBe(0); expect(cmp.getItemsCount()).toBe(0);
}); });
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.getConfigEndIndex()).toBe(0); expect(cmp.getConfigEndIndex()).toBe(0);
expect(cmp.getItemsCount()).toBe(0); expect(cmp.getItemsCount()).toBe(0);
}); });
}); });
describe('Setters', () => { describe('Setters', () => {
let cmp: ComponentDataCollection; let cmp: ComponentDataCollection;
beforeEach(() => { beforeEach(() => {
cmp = wrapper.components({ cmp = wrapper.components({
type: DataCollectionType, type: DataCollectionType,
@ -146,14 +160,16 @@ describe('Collection component getters and setters', () => {
type: 'default', type: 'default',
tagName: 'div', tagName: 'div',
attributes: { attributes: {
'data-user': { dataUser: {
type: DataCollectionVariableType, type: DataCollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -163,6 +179,7 @@ describe('Collection component getters and setters', () => {
dataSource: { dataSource: {
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}, },
}, },
}, },
@ -182,13 +199,14 @@ 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');
@ -199,72 +217,72 @@ 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).getAttributes()['data-user']).toBe('user1'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1');
expect(children.at(1).getAttributes()['data-user']).toBe('user2'); expect(children.at(1).components().at(0).getAttributes()['dataUser']).toBe('user2');
expect(children.at(2).getAttributes()['data-user']).toBe('user3'); expect(children.at(2).components().at(0).getAttributes()['dataUser']).toBe('user3');
}); });
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).getAttributes()['data-user']).toBe('user2'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user2');
expect(children.at(1).getAttributes()['data-user']).toBe('user3'); expect(children.at(1).components().at(0).getAttributes()['dataUser']).toBe('user3');
}); });
test('setDataSource should update the data source and reflect in children', () => { test('setDataSource should update the data source and reflect in children', () => {
dsm.add({ dsm.add({
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' },
],
}); });
cmp.setDataSource({ cmp.setDataSource({
type: DataVariableType, type: DataVariableType,
path: 'new_data_source_id', path: 'new_data_source_id',
});
});
const children = cmp.components(); const children = cmp.components();
expect(children).toHaveLength(1); // Only one record in the new data source expect(children).toHaveLength(1);
expect(children.at(0).getAttributes()['data-user']).toBe('user4'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user4');
}); });
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); // Includes all records expect(children).toHaveLength(3);
expect(children.at(0).getAttributes()['data-user']).toBe('user1'); expect(children.at(0).components().at(0).getAttributes()['dataUser']).toBe('user1');
}); });
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); // No records included expect(children).toHaveLength(0);
}); });
test('setDataSource with an empty data source should result in no children', () => { test('setDataSource with an empty data source should result in no children', () => {
const emptyDataSource = 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); // No records in the data source expect(children).toHaveLength(0);
}); });
}); });
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,
@ -276,14 +294,16 @@ describe('Collection component getters and setters', () => {
type: 'default', type: 'default',
tagName: 'div', tagName: 'div',
attributes: { attributes: {
'data-user': { dataUser: {
type: DataCollectionVariableType, type: DataCollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem, variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'my_collection', collectionId: 'my_collection',
path: 'user', path: 'user',
}, },
}, },
}, },
], ],
}, },
collectionConfig: { collectionConfig: {
@ -293,6 +313,7 @@ describe('Collection component getters and setters', () => {
dataSource: { dataSource: {
type: DataVariableType, type: DataVariableType,
path: 'my_data_source_id', path: 'my_data_source_id',
}, },
}, },
}, },
@ -302,38 +323,39 @@ 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('data-user="user1"'); expect(html).toContain('dataUser="user1"');
expect(html).toContain('data-user="user2"'); expect(html).toContain('dataUser="user2"');
expect(html).toContain('data-user="user3"'); expect(html).toContain('dataUser="user3"');
}); });
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('data-user="user2"'); expect(html).toContain('dataUser="user2"');
expect(html).toContain('data-user="user3"'); expect(html).toContain('dataUser="user3"');
}); });
test('HTML output should reflect changes in dataSource', () => { test('HTML output should reflect changes in dataSource', () => {
dsm.add({ dsm.add({
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' },
],
}); });
cmp.setDataSource({ cmp.setDataSource({
type: DataVariableType, type: DataVariableType,
path: 'new_data_source_id', path: 'new_data_source_id',
});
});
const html = cmp.toHTML(); const html = cmp.toHTML();
expect(html).toContain('data-user="user4"'); expect(html).toContain('dataUser="user4"');
}); });
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('data-user'); expect(html).not.toContain('dataUser');
}); });
}); });
}); });
Loading…
Cancel
Save