import { XrmMockGenerator } from "../../xrm-mock-generator" ; import { ItemCollectionMock } from "../collection/itemcollection/itemcollection.mock" ; export class EntityMock implements Xrm . Entity { public id : string ; public entityName : string ; public primaryValue : string ; public attributes : ItemCollectionMock < Xrm . Attributes . Attribute > ; public saveEventHandlers : Xrm . Events . ContextSensitiveHandler [ ] ; constructor ( components ? ) { components = components || { } ; this . id = components . id || "{deadbeef-dead-beef-dead-beefdeadbeaf}" ; this . entityName = components . entityName || "contact" ; this . primaryValue = components . primaryValue || "Default Contact" ; this . attributes = components . attributes || new ItemCollectionMock ( ) ; this . saveEventHandlers = [ ] ; } public addOnPostSave ( handler ) { throw new Error ( "Method not implemented." ) ; } public addOnSave ( handler ) { this . saveEventHandlers . push ( handler ) ; } public getEntityName ( ) { return this . entityName ; } public getDataXml ( ) { throw new Error ( ( "getDataXml not implemented" ) ) ; } public getEntityReference ( ) { return { entityType : this . entityName , id : this . id , name : this . primaryValue , } ; } public getId ( ) { return this . id ; } public getIsDirty ( ) { let isDirty = false ; if ( this . attributes ) { for ( let i = 0 ; i < this . attributes . getLength ( ) ; i ++ ) { const attr = this . attributes . get ( i ) ; if ( attr . getIsDirty ( ) ) { isDirty = true ; break ; } } } return isDirty ; } public getPrimaryAttributeValue ( ) { return this . primaryValue ; } public isValid ( ) { throw new Error ( "isValid not implemented." ) ; } public removeOnSave ( handler ) { const index = this . saveEventHandlers . indexOf ( handler ) ; this . saveEventHandlers . splice ( index ) ; } public save ( saveMode ? ) { const context = this . getSaveContext ( saveMode ) ; for ( const handler of this . saveEventHandlers ) { const index = this . saveEventHandlers . indexOf ( handler ) ; context . getDepth = ( ) => index ; handler ( context ) ; } } private getSaveContext ( saveMode ) { return { getContext : ( ) : Xrm . GlobalContext => { return XrmMockGenerator . context ; } , getDepth : null , getEventArgs : ( ) : Xrm . Events . SaveEventArguments => { return this . getSaveEventArgs ( saveMode ) ; } , getEventSource : ( ) : Xrm . Attributes . Attribute | Xrm . Controls . Control | Xrm . Entity => { throw new Error ( "getEventSource not implemented." ) ; } , getFormContext : ( ) : Xrm . FormContext => { throw new Error ( "getFormContext not implemented." ) ; } , getSharedVariable : ( ) : any => { throw new Error ( "getSharedVariable not implemented." ) ; } , setSharedVariable : ( ) : void => { throw new Error ( "setSharedVariable not implemented." ) ; } , } ; } private getSaveEventArgs ( saveMode ) { return { getSaveMode : ( ) => { let mode ; if ( saveMode == null ) { mode = XrmEnum . SaveMode . Save ; } else if ( saveMode === "saveandclose" ) { mode = XrmEnum . SaveMode . SaveAndClose ; } else if ( saveMode === "saveandnew" ) { mode = XrmEnum . SaveMode . SaveAndNew ; } else { mode = saveMode ; } return mode ; } , isDefaultPrevented : ( ) => false , preventDefault : ( ) : void => { throw new Error ( "preventDefault not implemented." ) ; } , preventDefaultOnError : ( ) : void => { throw new Error ( "preventDefaultOnError not implemented." ) ; } } ; } } export interface IEntityComponents { id ? : string ; entityName ? : string ; primaryValue ? : string ; attributes ? : ItemCollectionMock < Xrm . Attributes . Attribute > ; }