import { ColorByName } from "@itwin/core-common" ; import { ArrayValue , BasePropertyEditorParams , ButtonGroupEditorParams , ColorEditorParams , CustomFormattedNumberParams , DisplayMessageType , ImageCheckBoxParams , MessagePresenter , MessageSeverity , ParseResults , Primitives , PrimitiveValue , PropertyDescription , PropertyEditorInfo , PropertyEditorParamTypes , PropertyRecord , PropertyValue , PropertyValueFormat , StandardEditorNames , StandardTypeNames , StructValue , UiAdmin , } from "@itwin/appui-abstract" ; import { AsyncValueProcessingResult , ColumnDescription , CompositeFilterDescriptorCollection , DataControllerBase , FilterableTable , TableFilterDescriptorCollection } from "@itwin/components-react" ; import { UiIModelComponents } from "../imodel-components-react/UiIModelComponents" ; internal export class TestUtils { private static _uiIModelComponentsInitialized = false ; public static async initializeUiIModelComponents ( ) { if ( ! TestUtils . _uiIModelComponentsInitialized ) { await UiIModelComponents . initialize ( ) ; TestUtils . _uiIModelComponentsInitialized = true ; const mp = { displayMessage : ( _severity , _briefMessage , _detailedMessage ? , _messageType ? ) : void => { } , displayInputFieldMessage : ( _inputField , _severity , _briefMessage , _detailedMessage ? ) : void => { } , closeInputFieldMessage : ( ) : void => { } } ; UiAdmin . messagePresenter = mp ; } } public static terminateUiIModelComponents ( ) { UiIModelComponents . terminate ( ) ; TestUtils . _uiIModelComponentsInitialized = false ; } /** Waits until all async operations finish */ public static async flushAsyncOperations ( ) { return new Promise ( ( resolve ) => setTimeout ( resolve ) ) ; } public static createPropertyRecord ( value , column , typename ) { const v = { valueFormat : PropertyValueFormat . Primitive , value , displayValue : value } ; const pd = { typename , name : column . key , displayLabel : column . label } ; column . propertyDescription = pd ; return new PropertyRecord ( v , pd ) ; } public static createPrimitiveStringProperty ( name , rawValue , displayValue = rawValue . toString ( ) , editorInfo ? , autoExpand ? ) { const value = { displayValue , value : rawValue , valueFormat : PropertyValueFormat . Primitive } ; const description = { displayLabel : name , name , typename : StandardTypeNames . String } ; if ( editorInfo ) description . editor = editorInfo ; const property = new PropertyRecord ( value , description ) ; property . isReadonly = false ; property . autoExpand = autoExpand ; if ( property . autoExpand === undefined ) delete property . autoExpand ; return property ; } public static createMultilineTextPropertyRecord ( name , value ) { const record = TestUtils . createPrimitiveStringProperty ( name , value ) ; record . property . renderer = { name : "multiline" } ; return record ; } public static createArrayProperty ( name , items ? , autoExpand ? ) { if ( ! items ) items = [ ] ; const value = { items , valueFormat : PropertyValueFormat . Array , itemsTypeName : items . length !== 0 ? items [ 0 ] . property . typename : "string" } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Array } ; const property = new PropertyRecord ( value , description ) ; property . isReadonly = false ; property . autoExpand = autoExpand ; return property ; } public static createStructProperty ( name , members ? , autoExpand ? ) { if ( ! members ) members = { } ; const value = { members , valueFormat : PropertyValueFormat . Struct } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Struct } ; const property = new PropertyRecord ( value , description ) ; property . isReadonly = false ; property . autoExpand = autoExpand ; return property ; } public static createEnumStringProperty ( name , index , column ? ) { const value = { displayValue : "" , value : index , valueFormat : PropertyValueFormat . Primitive } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Enum } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; propertyRecord . property . enum = { choices : [ { label : "Yellow" , value : "yellow" } , { label : "Red" , value : "red" } , { label : "Green" , value : "green" } , ] , isStrict : false } ; if ( column ) column . propertyDescription = description ; return propertyRecord ; } public static createEnumProperty ( name , index , column ? ) { const value = { displayValue : name , value : index , valueFormat : PropertyValueFormat . Primitive } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Enum } ; const getChoices = async ( ) => { return [ { label : "Yellow" , value : 0 } , { label : "Red" , value : 1 } , { label : "Green" , value : 2 } , { label : "Blue" , value : 3 } , ] ; } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; propertyRecord . property . enum = { choices : getChoices ( ) , isStrict : false } ; if ( column ) column . propertyDescription = description ; return propertyRecord ; } public static blueEnumValueIsEnabled = true ; public static toggleBlueEnumValueEnabled ( ) { TestUtils . blueEnumValueIsEnabled = ! TestUtils . blueEnumValueIsEnabled ; } public static addEnumButtonGroupEditorSpecification ( propertyRecord ) { propertyRecord . property . editor = { name : "enum-buttongroup" , params : [ { type : PropertyEditorParamTypes . ButtonGroupData , buttons : [ { iconSpec : "icon-yellow" } , { iconSpec : "icon-red" } , { iconSpec : "icon-green" } , { iconSpec : "icon-blue" , isEnabledFunction : ( ) => TestUtils . blueEnumValueIsEnabled } , ] } as ButtonGroupEditorParams , ] } ; } public static createBooleanProperty ( name , booleanValue , editor ? ) { const value = { displayValue : "" , value : booleanValue , valueFormat : PropertyValueFormat . Primitive } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Boolean , editor : editor ? { name : editor } : undefined } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; return propertyRecord ; } public static createImageCheckBoxProperty ( name , booleanValue ) { const value = { displayValue : "" , value : booleanValue , valueFormat : PropertyValueFormat . Primitive } ; const description = { displayLabel : name , name , typename : StandardTypeNames . Boolean } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . property . editor = { name : "image-check-box" , params : [ { type : PropertyEditorParamTypes . CheckBoxImages , imageOff : "icon-visibility-hide-2" , imageOn : "icon-visibility" } as ImageCheckBoxParams , ] } ; propertyRecord . isReadonly = false ; return propertyRecord ; } public static createColorProperty ( propertyName , colorValue ) { const value = { displayValue : "" , value : colorValue , valueFormat : PropertyValueFormat . Primitive } ; const description = { name : propertyName , displayLabel : propertyName , typename : StandardTypeNames . Number , editor : { name : "color-picker" , params : [ { type : PropertyEditorParamTypes . ColorData , colorValues : [ ColorByName . blue as number , ColorByName . red as number , ColorByName . green as number , ColorByName . yellow as number , ColorByName . black as number , ColorByName . gray as number , ColorByName . purple as number , ColorByName . pink as number , ] , numColumns : 2 } as ColorEditorParams , ] } } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; return propertyRecord ; } public static createWeightProperty ( propertyName , weight ) { const value = { displayValue : "" , value : weight , valueFormat : PropertyValueFormat . Primitive } ; const description = { name : propertyName , displayLabel : propertyName , typename : StandardTypeNames . Number , editor : { name : StandardEditorNames . WeightPicker } } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; return propertyRecord ; } private static _formatLength = ( numberValue ) : string => numberValue . toFixed ( 2 ) ; public static createCustomNumberProperty ( propertyName , numVal , displayVal ? , editorParams ? ) { const value = { displayValue : displayVal , value : numVal , valueFormat : PropertyValueFormat . Primitive } ; const description = { name : propertyName , displayLabel : propertyName , typename : StandardTypeNames . Number , editor : { name : StandardEditorNames . NumberCustom , params : [ { type : PropertyEditorParamTypes . CustomFormattedNumber , formatFunction : TestUtils . _formatLength , parseFunction : ( stringValue ) : ParseResults => { const rtnValue = Number . parseFloat ( stringValue ) ; if ( Number . isNaN ( rtnValue ) ) { return { parseError : `Unable to parse ${ stringValue } into a valid length` } ; } else { return { value : rtnValue } ; } } } as CustomFormattedNumberParams , ] } } ; if ( editorParams ) { editorParams . forEach ( ( params ) => { description . editor ! . params ! . push ( params ) ; } ) ; } const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; return propertyRecord ; } public static createNumericProperty ( propertyName , numericValue , editorName , editorParams ? ) { const value = { displayValue : "" , value : numericValue , valueFormat : PropertyValueFormat . Primitive } ; const description = { name : propertyName , displayLabel : propertyName , typename : StandardTypeNames . Number , editor : { name : editorName , params : editorParams } } ; const propertyRecord = new PropertyRecord ( value , description ) ; propertyRecord . isReadonly = false ; return propertyRecord ; } public static createNavigationProperty ( name , value , displayValue ? ) { const property = TestUtils . createPrimitiveStringProperty ( name , "" , displayValue ) ; property . property . typename = StandardTypeNames . Navigation ; ( property . value as PrimitiveValue ) . value = value ; return property ; } public static createURIProperty ( name , value , displayValue ? ) { const property = TestUtils . createPrimitiveStringProperty ( name , value , displayValue ) ; property . property . typename = StandardTypeNames . URL ; return property ; } } internal export class TestFilterableTable implements FilterableTable { private _filterDescriptors = new TableFilterDescriptorCollection ( ) ; private _columnDescriptions : ColumnDescription [ ] ; constructor ( colDescriptions ) { this . _columnDescriptions = colDescriptions ; } /** Gets the description of a column within the table. */ public getColumnDescription ( columnKey ) { return this . _columnDescriptions . find ( ( v ) => v . key === columnKey ) ; } /** Gets the filter descriptors for the table. */ public get filterDescriptors ( ) : CompositeFilterDescriptorCollection { return this . _filterDescriptors ; } /** Gets ECExpression to get property display value. */ public getPropertyDisplayValueExpression ( property ) { return property ; } } internal export class MineDataController extends DataControllerBase { public override async validateValue ( _newValue , _record ) { return { encounteredError : true , errorMessage : { severity : MessageSeverity . Error , briefMessage : "Test" } } ; } } export default TestUtils ;