import { Component , Directive , EventEmitter } from '@angular/core' ; import { SiteService } from '../common/services/site.service' ; export function Mock ( opts = { } ) { return ( target , propertyKey , descriptor ) => { console . log ( descriptor ) ; return descriptor ; } ; } export function MockComponent ( options , spies = [ ] ) { const metadata = { selector : options . selector , template : options . template || '' , inputs : options . inputs , outputs : options . outputs , } ; const component = class _ { } ; if ( options . outputs ) { for ( const output of options . outputs ) { component . prototype [ output ] = new EventEmitter < any > ( ) ; } } for ( const spy of spies ) { component . prototype [ spy ] = jasmine . createSpy ( spy ) ; } return Component ( metadata ) ( component ) ; } export function MockDirective ( options , spies = [ ] ) { const metadata = { selector : options . selector , inputs : options . inputs , outputs : options . outputs , } ; const directive = class _ { } ; if ( options . outputs ) { for ( const output of options . outputs ) { directive . prototype [ output ] = new EventEmitter < any > ( ) ; } } for ( const spy of spies ) { directive . prototype [ spy ] = jasmine . createSpy ( spy ) ; } return Directive ( metadata ) ( directive ) ; } export function MockService ( obj , config = null ) { const spies = { } ; const props = Object . getOwnPropertyNames ( obj . prototype ) . filter ( key => { return ( Object . getOwnPropertyDescriptor ( obj . prototype , key ) . get || Object . getOwnPropertyDescriptor ( obj . prototype , key ) . set ) ; } ) ; const keys = Object . keys ( obj . prototype ) . filter ( key => props . indexOf ( key ) === - 1 ) ; const has = ( config && config . has ) || [ ] ; for ( const prop of [ ... props , ... has ] ) { const property = { get : ( ) => false , set : ( ) => { } , } ; if ( config && config . props && config . props [ prop ] ) { if ( config . props [ prop ] . get ) { property . get = config . props [ prop ] . get ; } if ( config . props [ prop ] . set ) { property . set = config . props [ prop ] . set ; } } spies [ prop ] = null ; Object . defineProperty ( spies , prop , property ) ; } for ( const key of keys ) { let value = null ; if ( config && config [ key ] ) { value = config [ key ] ; } if ( typeof value === 'function' ) { spies [ key ] = jasmine . createSpy ( key ) . and . callFake ( value ) ; } else { spies [ key ] = jasmine . createSpy ( key ) . and . returnValue ( value ) ; } } return new Proxy ( { ... spies , _config : config } , { get : ( target , prop ) => { if ( target . hasOwnProperty ( prop . toString ( ) ) ) { return target [ prop ] ; } else if ( prop . toString ( ) === '$quoted$' ) { return [ ] ; } if ( target [ '_config' ] && target [ '_config' ] [ prop ] ) { target [ prop ] = jasmine . createSpy ( prop . toString ( ) ) . and . returnValue ( target [ '_config' ] [ prop ] ) ; return target [ prop ] ; } return null ; } , set : ( target , prop , value , receiver ) => { if ( prop . toString ( ) . endsWith ( '$' ) ) { target [ prop ] = value ; return true ; } target [ prop ] = jasmine . createSpy ( prop . toString ( ) ) . and . returnValue ( value ) ; return true ; } , } ) ; }