@ -1,14 +1,23 @@
import { Component , DataSourceManager , Editor } from '../../../../../src' ;
import { Component , Components , ComponentView , DataSourceManager , Editor } from '../../../../../src' ;
import { DataConditionIfTrueType } from '../../../../../src/data_sources/model/conditional_variables/constants' ;
import { DataVariableType } from '../../../../../src/data_sources/model/DataVariable' ;
import { DataConditionType } from '../../../../../src/data_sources/model/conditional_variables/DataCondition' ;
import { AnyTypeOperation } from '../../../../../src/data_sources/model/conditional_variables/operators/AnyTypeOperator' ;
import { NumberOperation } from '../../../../../src/data_sources/model/conditional_variables/operators/NumberOperator' ;
import ComponentDataConditionView from '../../../../../src/data_sources/view/ComponentDataConditionView' ;
import ComponentWrapper from '../../../../../src/dom_components/model/ComponentWrapper' ;
import ComponentTableView from '../../../../../src/dom_components/view/ComponentTableView' ;
import ComponentTextView from '../../../../../src/dom_components/view/ComponentTextView' ;
import EditorModel from '../../../../../src/editor/model/Editor' ;
import { setupTestEditor } from '../../../../common' ;
import {
FALSE_CONDITION ,
ifFalseComponentDef ,
ifFalseText ,
ifTrueComponentDef ,
ifTrueText ,
isObjectContained ,
setupTestEditor ,
TRUE_CONDITION ,
} from '../../../../common' ;
import ComponentDataCondition from '../../../../../src/data_sources/model/conditional_variables/ComponentDataCondition' ;
describe ( 'ComponentDataCondition' , ( ) = > {
let editor : Editor ;
@ -24,60 +33,34 @@ describe('ComponentDataCondition', () => {
em . destroy ( ) ;
} ) ;
it ( 'should add a component with a condition that evaluates a component definition ' , ( ) = > {
it ( 'should add a component with a condition' , ( ) = > {
const component = cmpRoot . append ( {
type : DataConditionType ,
condition : {
left : 0 ,
operator : NumberOperation.greaterThan ,
right : - 1 ,
} ,
ifTrue : {
tagName : 'h1' ,
type : 'text' ,
content : 'some text' ,
} ,
} ) [ 0 ] ;
dataResolver : { condition : TRUE_CONDITION } ,
components : [ ifTrueComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
expect ( component ) . toBeDefined ( ) ;
expect ( component . get ( 'type' ) ) . toBe ( DataConditionType ) ;
expect ( component . getInnerHTML ( ) ) . toBe ( '<h1>some text</h1>' ) ;
const componentView = component . getView ( ) ;
expect ( componentView ) . toBeInstanceOf ( ComponentDataConditionView ) ;
expect ( componentView ? . el . textContent ) . toBe ( 'some text' ) ;
const childComponent = getFirstChild ( component ) ;
const childView = getFirstChildView ( component ) ;
expect ( childComponent ) . toBeDefined ( ) ;
expect ( childComponent . get ( 'type' ) ) . toBe ( 'text' ) ;
expect ( childComponent . getInnerHTML ( ) ) . toBe ( 'some text' ) ;
expect ( childView ) . toBeInstanceOf ( ComponentTextView ) ;
expect ( childView ? . el . innerHTML ) . toBe ( 'some text' ) ;
expect ( component . getInnerHTML ( ) ) . toContain ( ifTrueText ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifTrueText ) ;
const ifTrueContent = component . getIfTrueContent ( ) ! ;
expect ( ifTrueContent . getInnerHTML ( ) ) . toContain ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . textContent ) . toBe ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . style . display ) . toBe ( '' ) ;
} ) ;
it ( 'should add a component with a condition that evaluates a string ' , ( ) = > {
it ( 'ComponentDataCondition getIfTrueContent and getIfFalseContent ' , ( ) = > {
const component = cmpRoot . append ( {
type : DataConditionType ,
condition : {
left : 0 ,
operator : NumberOperation.greaterThan ,
right : - 1 ,
} ,
ifTrue : '<h1>some text</h1>' ,
} ) [ 0 ] ;
expect ( component ) . toBeDefined ( ) ;
expect ( component . get ( 'type' ) ) . toBe ( DataConditionType ) ;
expect ( component . getInnerHTML ( ) ) . toBe ( '<h1>some text</h1>' ) ;
const componentView = component . getView ( ) ;
expect ( componentView ) . toBeInstanceOf ( ComponentDataConditionView ) ;
expect ( componentView ? . el . textContent ) . toBe ( 'some text' ) ;
const childComponent = getFirstChild ( component ) ;
const childView = getFirstChildView ( component ) ;
expect ( childComponent ) . toBeDefined ( ) ;
expect ( childComponent . get ( 'type' ) ) . toBe ( 'text' ) ;
expect ( childComponent . getInnerHTML ( ) ) . toBe ( 'some text' ) ;
expect ( childView ) . toBeInstanceOf ( ComponentTextView ) ;
expect ( childView ? . el . innerHTML ) . toBe ( 'some text' ) ;
dataResolver : { condition : TRUE_CONDITION } ,
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
expect ( JSON . parse ( JSON . stringify ( component . getIfTrueContent ( ) ! ) ) ) . toEqual ( ifTrueComponentDef ) ;
expect ( JSON . parse ( JSON . stringify ( component . getIfFalseContent ( ) ! ) ) ) . toEqual ( ifFalseComponentDef ) ;
} ) ;
it ( 'should test component variable with data-source' , ( ) = > {
@ -92,41 +75,49 @@ describe('ComponentDataCondition', () => {
const component = cmpRoot . append ( {
type : DataConditionType ,
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
dataResolver : {
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
} ,
} ,
} ,
ifTrue : {
tagName : 'h1' ,
type : 'text' ,
content : 'Some value' ,
} ,
ifFalse : {
tagName : 'h1' ,
type : 'text' ,
content : 'False value' ,
} ,
} ) [ 0 ] ;
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
expect ( component . getInnerHTML ( ) ) . toContain ( ifTrueText ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifTrueText ) ;
const ifTrueContent = component . getIfTrueContent ( ) ! ;
expect ( ifTrueContent . getInnerHTML ( ) ) . toContain ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . textContent ) . toBe ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . style . display ) . toBe ( '' ) ;
const childComponent = getFirstChild ( component ) ;
expect ( childComponent ) . toBeDefined ( ) ;
expect ( childComponent . get ( 'type' ) ) . toBe ( 'text' ) ;
expect ( childComponent . getInnerHTML ( ) ) . toBe ( 'Some value' ) ;
expect ( component . getInnerHTML ( ) ) . not . toContain ( ifFalseText ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifFalseText ) ;
const ifFalseContent = component . getIfFalseContent ( ) ! ;
expect ( ifFalseContent . getInnerHTML ( ) ) . toContain ( ifFalseText ) ;
expect ( ifFalseContent . getEl ( ) ? . textContent ) . toBe ( ifFalseText ) ;
expect ( ifFalseContent . getEl ( ) ? . style . display ) . toBe ( 'none' ) ;
/* Test changing datasources */
changeDataSourceValue ( dsm , 'Diffirent value' ) ;
expect ( getFirstChild ( component ) . getInnerHTML ( ) ) . toBe ( 'False value' ) ;
expect ( getFirstChildView ( component ) ? . el . innerHTML ) . toBe ( 'False value' ) ;
changeDataSourceValue ( dsm , 'Name1' ) ;
expect ( getFirstChild ( component ) . getInnerHTML ( ) ) . toBe ( 'Some value' ) ;
expect ( getFirstChildView ( component ) ? . el . innerHTML ) . toBe ( 'Some value' ) ;
const WrongValue = 'Diffirent value' ;
changeDataSourceValue ( dsm , WrongValue ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifTrueText ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifFalseText ) ;
expect ( ifTrueContent . getEl ( ) ? . style . display ) . toBe ( 'none' ) ;
expect ( ifFalseContent . getEl ( ) ? . style . display ) . toBe ( '' ) ;
const CorrectValue = 'Name1' ;
changeDataSourceValue ( dsm , CorrectValue ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifTrueText ) ;
expect ( component . getEl ( ) ? . innerHTML ) . toContain ( ifFalseText ) ;
expect ( ifTrueContent . getEl ( ) ? . style . display ) . toBe ( '' ) ;
expect ( ifFalseContent . getEl ( ) ? . style . display ) . toBe ( 'none' ) ;
} ) ;
it ( 'should test a conditional component with a child that is also a conditional component' , ( ) = > {
@ -141,65 +132,54 @@ describe('ComponentDataCondition', () => {
const component = cmpRoot . append ( {
type : DataConditionType ,
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
dataResolver : {
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
} ,
} ,
} ,
ifTrue : {
tagName : 'div' ,
components : [
{
components : [
{
type : DataConditionIfTrueType ,
components : {
type : DataConditionType ,
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
dataResolver : {
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : AnyTypeOperation.equals ,
right : {
type : DataVariableType ,
path : 'ds1.right_id.right' ,
} ,
} ,
} ,
ifTrue : {
tagName : 'table' ,
type : 'table' ,
} ,
components : ifTrueComponentDef ,
} ,
] ,
} ,
} ) [ 0 ] ;
const innerComponent = getFirstChild ( getFirstChild ( component ) ) ;
const innerComponentView = getFirstChildView ( innerComponent ) ;
const innerHTML = '<table><tbody><tr class="row"><td class="cell"></td></tr></tbody></table>' ;
expect ( innerComponent . getInnerHTML ( ) ) . toBe ( innerHTML ) ;
expect ( innerComponentView ) . toBeInstanceOf ( ComponentTableView ) ;
expect ( innerComponentView ? . el . tagName ) . toBe ( 'TABLE' ) ;
} ,
ifFalseComponentDef ,
] ,
} ) [ 0 ] as ComponentDataCondition ;
const ifTrueContent = component . getIfTrueContent ( ) ! ;
expect ( ifTrueContent . getInnerHTML ( ) ) . toContain ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . textContent ) . toBe ( ifTrueText ) ;
expect ( ifTrueContent . getEl ( ) ? . style . display ) . toBe ( '' ) ;
} ) ;
it ( 'should store conditional components' , ( ) = > {
const conditionalCmptDef = {
type : DataConditionType ,
condition : {
left : 0 ,
operator : NumberOperation.greaterThan ,
right : - 1 ,
} ,
ifTrue : [
{
tagName : 'h1' ,
type : 'text' ,
content : 'some text' ,
} ,
] ,
dataResolver : { condition : FALSE_CONDITION } ,
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ;
cmpRoot . append ( conditionalCmptDef ) [ 0 ] ;
@ -208,18 +188,84 @@ describe('ComponentDataCondition', () => {
const page = projectData . pages [ 0 ] ;
const frame = page . frames [ 0 ] ;
const storageCmptDef = frame . component . components [ 0 ] ;
expect ( storageCmptDef ) . toEqual ( conditionalCmptDef ) ;
expect ( isObjectContained ( storageCmptDef , conditionalCmptDef ) ) . toBe ( true ) ;
} ) ;
} ) ;
function changeDataSourceValue ( dsm : DataSourceManager , newValue : string ) {
dsm . get ( 'ds1' ) . getRecord ( 'left_id' ) ? . set ( 'left' , newValue ) ;
}
it ( 'should dynamically display ifTrue, ifFalse components in the correct order' , ( ) = > {
const component = cmpRoot . append ( {
type : DataConditionType ,
dataResolver : { condition : TRUE_CONDITION } ,
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
const el = component . getEl ( ) ! ;
const ifTrueEl = el . childNodes [ 0 ] as any ;
const ifFalseEl = el . childNodes [ 1 ] as any ;
expect ( ifTrueEl . textContent ) . toContain ( ifTrueText ) ;
expect ( ifTrueEl . style . display ) . toBe ( '' ) ;
expect ( ifFalseEl . textContent ) . toContain ( ifFalseText ) ;
expect ( ifFalseEl . style . display ) . toBe ( 'none' ) ;
function getFirstChildView ( component : Component ) {
return getFirstChild ( component ) . getView ( ) ;
}
component . setCondition ( FALSE_CONDITION ) ;
expect ( ifTrueEl . style . display ) . toBe ( 'none' ) ;
expect ( ifTrueEl . textContent ) . toContain ( ifTrueText ) ;
expect ( ifFalseEl . style . display ) . toBe ( '' ) ;
expect ( ifFalseEl . textContent ) . toContain ( ifFalseText ) ;
} ) ;
it ( 'should dynamically update display components when data source changes' , ( ) = > {
const dataSource = {
id : 'ds1' ,
records : [ { id : 'left_id' , left : 1 } ] ,
} ;
dsm . add ( dataSource ) ;
const component = cmpRoot . append ( {
type : DataConditionType ,
dataResolver : {
condition : {
left : {
type : DataVariableType ,
path : 'ds1.left_id.left' ,
} ,
operator : NumberOperation.greaterThan ,
right : 0 ,
} ,
} ,
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
const el = component . view ! . el ! ;
const falseValue = - 1 ;
changeDataSourceValue ( dsm , falseValue ) ;
expect ( el . innerHTML ) . toContain ( ifTrueText ) ;
expect ( el . innerHTML ) . toContain ( ifFalseText ) ;
const ifTrueEl = el . childNodes [ 0 ] as any ;
const ifFalseEl = el . childNodes [ 1 ] as any ;
expect ( ifTrueEl ! . style . display ) . toBe ( 'none' ) ;
expect ( ifTrueEl . textContent ) . toContain ( ifTrueText ) ;
expect ( ifFalseEl . style . display ) . toBe ( '' ) ;
expect ( ifFalseEl . textContent ) . toContain ( ifFalseText ) ;
} ) ;
function getFirstChild ( component : Component ) {
return component . components ( ) . at ( 0 ) ;
it ( 'should update content of ifTrue, ifFalse components when condition changes' , ( ) = > {
const component = cmpRoot . append ( {
type : DataConditionType ,
dataResolver : { condition : TRUE_CONDITION } ,
components : [ ifTrueComponentDef , ifFalseComponentDef ] ,
} ) [ 0 ] as ComponentDataCondition ;
const el = component . view ! . el ;
component . setCondition ( FALSE_CONDITION ) ;
const ifTrueEl = el . childNodes [ 0 ] as any ;
const ifFalseEl = el . childNodes [ 1 ] as any ;
expect ( ifTrueEl ! . style . display ) . toBe ( 'none' ) ;
expect ( ifTrueEl . textContent ) . toContain ( ifTrueText ) ;
expect ( ifFalseEl . style . display ) . toBe ( '' ) ;
expect ( ifFalseEl . textContent ) . toContain ( ifFalseText ) ;
} ) ;
} ) ;
function changeDataSourceValue ( dsm : DataSourceManager , newValue : string | number ) {
dsm . get ( 'ds1' ) . getRecord ( 'left_id' ) ? . set ( 'left' , newValue ) ;
}