@ -21,17 +21,18 @@ describe('Collection component', () => {
let wrapper : Component ;
let firstRecord : DataRecord ;
let secondRecord : DataRecord ;
const records = [
{ id : 'user1' , user : 'user1' , firstName : 'Name1' , age : '12' } ,
{ id : 'user2' , user : 'user2' , firstName : 'Name2' , age : '14' } ,
{ id : 'user3' , user : 'user3' , firstName : 'Name3' , age : '16' } ,
] ;
beforeEach ( ( ) = > {
( { em , editor , dsm } = setupTestEditor ( ) ) ;
wrapper = em . getWrapper ( ) ! ;
dataSource = dsm . add ( {
id : 'my_data_source_id' ,
records : [
{ id : 'user1' , user : 'user1' , firstName : 'Name1' , age : '12' } ,
{ id : 'user2' , user : 'user2' , firstName : 'Name2' , age : '14' } ,
{ id : 'user3' , user : 'user3' , firstName : 'Name3' , age : '16' } ,
] ,
records ,
} ) ;
firstRecord = dataSource . getRecord ( 'user1' ) ! ;
@ -917,80 +918,126 @@ describe('Collection component', () => {
} ) ;
} ) ;
describe ( 'Diffirent Collection variable types' , ( ) = > {
const stateVariableTests = [
{ variableType : DataCollectionStateType.currentIndex , expectedValues : [ 0 , 1 , 2 ] } ,
{ variableType : DataCollectionStateType.startIndex , expectedValues : [ 0 , 0 , 0 ] } ,
{ variableType : DataCollectionStateType.endIndex , expectedValues : [ 2 , 2 , 2 ] } ,
{
variableType : DataCollectionStateType.collectionId ,
describe ( 'State Variable Comprehensive Tests' , ( ) = > {
const stateVariableTests = {
[ DataCollectionStateType . currentIndex ] : {
expectedValues : [ 0 , 1 , 2 ] ,
expectedObjectPathValue : [ 0 , 1 , 2 , 3 ] ,
} ,
[ DataCollectionStateType . startIndex ] : {
expectedValues : [ 0 , 0 , 0 ] ,
expectedObjectPathValue : [ 0 , 0 , 0 , 0 ] ,
} ,
[ DataCollectionStateType . endIndex ] : {
expectedValues : [ 2 , 2 , 2 ] ,
expectedObjectPathValue : [ 3 , 3 , 3 , 3 ] ,
} ,
[ DataCollectionStateType . currentKey ] : {
expectedValues : [ 0 , 1 , 2 ] ,
expectedObjectPathValue : [ 'id' , 'user' , 'firstName' , 'age' ] ,
} ,
[ DataCollectionStateType . currentItem ] : {
expectedValues : null ,
expectedObjectPathValue : [ 'user1' , 'user1' , 'Name1' , '12' ] ,
} ,
[ DataCollectionStateType . collectionId ] : {
expectedValues : [ 'my_collection' , 'my_collection' , 'my_collection' ] ,
expectedObjectPathValue : [ 'my_collection' , 'my_collection' , 'my_collection' , 'my_collection' ] ,
} ,
{ variableType : DataCollectionStateType.totalItems , expectedValues : [ 3 , 3 , 3 ] } ,
{ variableType : DataCollectionStateType.remainingItems , expectedValues : [ 2 , 1 , 0 ] } ,
] ;
[ DataCollectionStateType . totalItems ] : {
expectedValues : [ 3 , 3 , 3 ] ,
expectedObjectPathValue : [ 4 , 4 , 4 , 4 ] ,
} ,
[ DataCollectionStateType . remainingItems ] : {
expectedValues : [ 2 , 1 , 0 ] ,
expectedObjectPathValue : [ 3 , 2 , 1 , 0 ] ,
} ,
} ;
stateVariableTests . forEach ( ( { variableType , expectedValues } ) = > {
test ( ` Variable type: ${ variableType } ` , ( ) = > {
const cmpDef = {
type : DataCollectionType ,
const createCollectionCmpDef = ( variableType : string , collectionId : string , dataSourcePath : string ) = > {
return {
type : DataCollectionType ,
components : {
type : DataCollectionItemType ,
components : {
type : DataCollectionItemType ,
components : {
type : 'default' ,
name : {
type : 'default' ,
name : {
type : DataVariableType ,
variableType : variableType ,
collectionId : collectionId ,
} ,
attributes : {
custom_attribute : {
type : DataVariableType ,
variableType : variableType ,
collectionId : 'my_collection' ,
collectionId : collectionId ,
} ,
attributes : {
custom_attribute : {
} ,
traits : [
{
name : 'attribute_trait' ,
value : {
type : DataVariableType ,
variableType : variableType ,
collectionId : 'my_collection' ,
collectionId : collectionId ,
} ,
} ,
traits : [
{
name : 'attribute_trait' ,
value : {
type : DataVariableType ,
variableType : variableType ,
collectionId : 'my_collection' ,
} ,
} ,
{
name : 'property_trait' ,
changeProp : true ,
value : {
type : DataVariableType ,
variableType : variableType ,
collectionId : 'my_collection' ,
} ,
{
name : 'property_trait' ,
changeProp : true ,
value : {
type : DataVariableType ,
variableType : variableType ,
collectionId : collectionId ,
} ,
] ,
} ,
} ,
] ,
} ,
dataResolver : {
collectionId : 'my_collection' ,
dataSource : {
type : DataVariableType ,
path : 'my_data_source_id' ,
} ,
} ,
dataResolver : {
collectionId : collectionId ,
dataSource : {
type : DataVariableType ,
path : dataSourcePath ,
} ,
} as ComponentDataCollectionProps ;
} ,
} ;
} ;
const performStateVariableAssertions = (
cmp : ComponentDataCollection ,
expectedAssertValues : ( string | number ) [ ] | null ,
) = > {
if ( ! expectedAssertValues ) return ;
const children = cmp . components ( ) ;
children . each ( ( child , index ) = > {
const content = child . components ( ) . at ( 0 ) ;
expect ( content . get ( 'name' ) ) . toBe ( expectedAssertValues [ index ] ) ;
expect ( content . get ( 'property_trait' ) ) . toBe ( expectedAssertValues [ index ] ) ;
expect ( content . getAttributes ( ) [ 'custom_attribute' ] ) . toBe ( expectedAssertValues [ index ] ) ;
expect ( content . getAttributes ( ) [ 'attribute_trait' ] ) . toBe ( expectedAssertValues [ index ] ) ;
} ) ;
} ;
Object . entries ( stateVariableTests ) . forEach ( ( [ variableType , { expectedValues , expectedObjectPathValue } ] ) = > {
test ( ` Variable type: ${ variableType } - Standard Path ` , ( ) = > {
const cmpDef = createCollectionCmpDef (
variableType ,
'my_collection' ,
'my_data_source_id' ,
) as ComponentDataCollectionProps ;
const cmp = wrapper . components ( cmpDef ) [ 0 ] as unknown as ComponentDataCollection ;
performStateVariableAssertions ( cmp , expectedValues ) ;
} ) ;
expect ( cmp . getItemsCount ( ) ) . toBe ( 3 ) ;
const children = cmp . components ( ) ;
children . each ( ( child , index ) = > {
const content = child . components ( ) . at ( 0 ) ;
expect ( content . get ( 'name' ) ) . toBe ( expectedValues [ index ] ) ;
expect ( content . get ( 'property_trait' ) ) . toBe ( expectedValues [ index ] ) ;
expect ( content . getAttributes ( ) [ 'custom_attribute' ] ) . toBe ( expectedValues [ index ] ) ;
expect ( content . getAttributes ( ) [ 'attribute_trait' ] ) . toBe ( expectedValues [ index ] ) ;
} ) ;
test ( ` Variable type: ${ variableType } - Object Path (my_data_source_id.user1) ` , ( ) = > {
const cmpDef = createCollectionCmpDef (
variableType ,
'my_collection' ,
'my_data_source_id.user1' ,
) as ComponentDataCollectionProps ;
const cmp = wrapper . components ( cmpDef ) [ 0 ] as unknown as ComponentDataCollection ;
performStateVariableAssertions ( cmp , expectedObjectPathValue ) ;
} ) ;
} ) ;
} ) ;