Skip to content

Commit

Permalink
Add tests for adding and removing records
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Jan 21, 2025
1 parent 374dbc4 commit 1f00f16
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,53 @@ describe('Collection component', () => {
expect(secondGrandchild.get('name')).toBe('user2');
});

test('Removing a record updates the collection component correctly', () => {
// Remove a record from the data source
dataSource.removeRecord('user1');

// Verify that the first child component is no longer present
expect(cmp.components().length).toBe(2); // Only 2 records remain

// Verify that the remaining components are updated correctly
const updatedFirstChild = cmp.components().at(0);
const updatedSecondChild = cmp.components().at(1);

expect(updatedFirstChild.get('name')).toBe("user2");
expect(updatedSecondChild.get('name')).toBe("user3");

// Verify that the grandchild components are also updated
const updatedFirstGrandchild = updatedFirstChild.components().at(0);
const updatedSecondGrandchild = updatedSecondChild.components().at(0);

expect(updatedFirstGrandchild.get('name')).toBe("user2");
expect(updatedSecondGrandchild.get('name')).toBe("user3");
});

test('Adding a record updates the collection component correctly', () => {
// Add a new record to the data source
dataSource.addRecord({ id: 'user4', user: 'user4', age: '20' });

// Verify that the collection component now has 4 children
expect(cmp.components().length).toBe(4);

// Verify that the new child component is added correctly
const newChild = cmp.components().at(3);
expect(newChild.get('name')).toBe('user4'); // user4's age

// Verify that the grandchild component is also added correctly
const newGrandchild = newChild.components().at(0);
expect(newGrandchild.get('name')).toBe('user4'); // user4's age

// Verify that existing components are unaffected
const firstChild = cmp.components().at(0);
const secondChild = cmp.components().at(1);
const thirdChild = cmp.components().at(2);

expect(firstChild.get('name')).toBe('user1');
expect(secondChild.get('name')).toBe('user2');
expect(thirdChild.get('name')).toBe('user3');
});

test('Updating the value to a static value', async () => {
firstChild.set('name', 'new_content_value');
expect(firstChild.get('name')).toBe('new_content_value');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ describe('Collection component', () => {
nestedDataSource = dsm.add({
id: 'nested_data_source_id',
records: [
{ id: 'user1', user: 'nested_user1', age: '12' },
{ id: 'user2', user: 'nested_user2', age: '14' },
{ id: 'user3', user: 'nested_user3', age: '16' },
{ id: 'nested_user1', user: 'nested_user1', age: '12' },
{ id: 'nested_user2', user: 'nested_user2', age: '14' },
{ id: 'nested_user3', user: 'nested_user3', age: '16' },
],
});

firstRecord = dataSource.getRecord('user1')!;
secondRecord = dataSource.getRecord('user2')!;
firstNestedRecord = nestedDataSource.getRecord('user1')!;
secondNestedRecord = nestedDataSource.getRecord('user2')!;
firstNestedRecord = nestedDataSource.getRecord('nested_user1')!;
secondNestedRecord = nestedDataSource.getRecord('nested_user2')!;
});

afterEach(() => {
Expand Down Expand Up @@ -311,4 +311,120 @@ describe('Collection component', () => {

expect(firstChild.get('name')).toBe('nested_user1');
});

describe('Nested Collection Component with Parent and Nested Data Sources', () => {
let parentCollection: Component;
let nestedCollection: Component;

beforeEach(() => {
// Initialize the parent and nested collections
parentCollection = wrapper.components({
type: CollectionComponentType,
collectionDef: {
componentDef: {
type: CollectionComponentType,
name: {
type: CollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'parent_collection',
path: 'user',
},
collectionDef: {
componentDef: {
type: 'default',
name: {
type: CollectionVariableType,
variableType: DataCollectionStateVariableType.currentItem,
collectionId: 'nested_collection',
path: 'user',
},
},
collectionConfig: {
collectionId: 'nested_collection',
dataSource: {
type: DataVariableType,
path: 'nested_data_source_id',
},
},
},
},
collectionConfig: {
collectionId: 'parent_collection',
dataSource: {
type: DataVariableType,
path: 'my_data_source_id',
},
},
},
})[0];

nestedCollection = parentCollection.components().at(0);
});

test('Removing a record from the parent data source updates the parent collection correctly', () => {
// Verify initial state
expect(parentCollection.components().length).toBe(2); // 2 parent records initially

// Remove a record from the parent data source
dataSource.removeRecord('user1');

// Verify that the parent collection updates correctly
expect(parentCollection.components().length).toBe(1); // Only 1 parent record remains
expect(parentCollection.components().at(0).get('name')).toBe('user2'); // Verify updated name

// Verify that the nested collection is unaffected
expect(nestedCollection.components().length).toBe(3); // Nested records remain the same
expect(nestedCollection.components().at(0).get('name')).toBe('nested_user1'); // Verify nested name
});

test('Adding a record to the parent data source updates the parent collection correctly', () => {
// Verify initial state
expect(parentCollection.components().length).toBe(2); // 2 parent records initially

// Add a new record to the parent data source
dataSource.addRecord({ id: 'user3', user: 'user3', age: '16' });

// Verify that the parent collection updates correctly
expect(parentCollection.components().length).toBe(3); // 3 parent records now
expect(parentCollection.components().at(2).get('name')).toBe('user3'); // Verify new name

// Verify that the nested collection is unaffected
expect(nestedCollection.components().length).toBe(3); // Nested records remain the same
expect(nestedCollection.components().at(0).get('name')).toBe('nested_user1'); // Verify nested name
expect(parentCollection.components().at(2).components().at(0).get('name')).toBe('nested_user1'); // Verify nested name
});

test('Removing a record from the nested data source updates the nested collection correctly', () => {
// Verify initial state
expect(nestedCollection.components().length).toBe(3); // 3 nested records initially

// Remove a record from the nested data source
nestedDataSource.removeRecord('nested_user1');

// Verify that the nested collection updates correctly
expect(nestedCollection.components().length).toBe(2); // Only 2 nested records remain
expect(nestedCollection.components().at(0).get('name')).toBe('nested_user2'); // Verify updated name
expect(nestedCollection.components().at(1).get('name')).toBe('nested_user3'); // Verify updated name
});

test('Adding a record to the nested data source updates the nested collection correctly', () => {
// Verify initial state
expect(nestedCollection.components().length).toBe(3); // 3 nested records initially
expect(nestedCollection.components().at(0).get('name')).toBe('nested_user1'); // Verify initial name
expect(nestedCollection.components().at(1).get('name')).toBe('nested_user2'); // Verify initial name
expect(nestedCollection.components().at(2).get('name')).toBe('nested_user3'); // Verify initial name

// Add a new record to the nested data source
nestedDataSource.addRecord({ id: 'user4', user: 'nested_user4', age: '18' });

// Verify that the nested collection updates correctly
expect(nestedCollection.components().length).toBe(4); // 4 nested records now
expect(nestedCollection.components().at(3).get('name')).toBe('nested_user4'); // Verify new name

// Verify existing records are unaffected
expect(nestedCollection.components().at(0).get('name')).toBe('nested_user1'); // Verify existing name
expect(nestedCollection.components().at(1).get('name')).toBe('nested_user2'); // Verify existing name
expect(nestedCollection.components().at(2).get('name')).toBe('nested_user3'); // Verify existing name
});
});
});

0 comments on commit 1f00f16

Please sign in to comment.