Created
March 31, 2017 23:48
-
-
Save leifwells/c067172da00c79064844d9b142758d5c to your computer and use it in GitHub Desktop.
Mock objects for use when unit testing applications built with Ionic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgZone, Renderer } from '@angular/core'; | |
export class ConfigMock { | |
public get(): any { | |
return ''; | |
} | |
public getBoolean(): boolean { | |
return true; | |
} | |
public getNumber(): number { | |
return 1; | |
} | |
} | |
export class FormMock { | |
public register(): any { | |
return true; | |
} | |
} | |
export class NavParamsMock { | |
static returnParam = null; | |
public get(key): any { | |
if (NavParamsMock.returnParam) { | |
return NavParamsMock.returnParam | |
} | |
return 'default'; | |
} | |
public setParams(value){ | |
NavParamsMock.returnParam = value; | |
} | |
} | |
export class NavMock { | |
public pop(): any { | |
return new Promise(function(resolve: Function): void { | |
resolve(); | |
}); | |
} | |
public push(): any { | |
return new Promise(function(resolve: Function): void { | |
resolve(); | |
}); | |
} | |
public getActive(): any { | |
return { | |
'instance': { | |
'model': 'something', | |
}, | |
}; | |
} | |
public setRoot(): any { | |
return true; | |
} | |
} | |
export class PlatformMock { | |
public ready(): Promise<{String}> { | |
return new Promise((resolve) => { | |
resolve('READY'); | |
}); | |
} | |
public registerBackButtonAction(fn: Function, priority?: number): Function { | |
return (() => true); | |
} | |
public hasFocus(ele: HTMLElement): boolean { | |
return true; | |
} | |
public doc(): HTMLDocument { | |
return document; | |
} | |
public is(): boolean { | |
return true; | |
} | |
public getElementComputedStyle(container: any): any { | |
return { | |
paddingLeft: '10', | |
paddingTop: '10', | |
paddingRight: '10', | |
paddingBottom: '10', | |
}; | |
} | |
public onResize(callback: any) { | |
return callback; | |
} | |
public registerListener(ele: any, eventName: string, callback: any): Function { | |
return (() => true); | |
} | |
public win(): Window { | |
return window; | |
} | |
public raf(callback: any): number { | |
return 1; | |
} | |
public timeout(callback: any, timer: number): any { | |
return setTimeout(callback, timer); | |
} | |
public cancelTimeout(id: any) { | |
// do nothing | |
} | |
public getActiveElement(): any { | |
return document['activeElement']; | |
} | |
public transitionEnd() { | |
return true; | |
} | |
} | |
export class MenuMock { | |
public close(): any { | |
return new Promise((resolve: Function) => { | |
resolve(); | |
}); | |
} | |
} | |
export class SlidesMock { | |
_index: number = 0; | |
_length: number = 6; | |
plt: PlatformMock; | |
_renderer: Renderer; | |
_zone: NgZone; | |
public getActiveIndex(): number { | |
return this._index; | |
} | |
public slideNext() { | |
this._index++; | |
} | |
public slidePrevious() { | |
this._index--; | |
} | |
public isEnd(): boolean { | |
return this._index === this._length - 1; | |
} | |
public isBeginning(): boolean { | |
return this._index === 0; | |
} | |
public initEvents() { | |
return true; | |
} | |
} | |
export class ToastMock { | |
public create(): any { | |
let rtn: Object = {}; | |
rtn['present'] = (() => true); | |
return rtn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment