Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clicker/src/test.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
90 lines (82 sloc)
3.09 KB
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
// This file is required by karma.conf.js and loads recursively all the .spec and framework files | |
import 'zone.js/dist/long-stack-trace-zone'; | |
import 'zone.js/dist/proxy.js'; | |
import 'zone.js/dist/sync-test'; | |
import 'zone.js/dist/jasmine-patch'; | |
import 'zone.js/dist/async-test'; | |
import 'zone.js/dist/fake-async-test'; | |
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | |
import { getTestBed, TestBed } from '@angular/core/testing'; | |
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; | |
import { TranslateModule, TranslateService } from '@ngx-translate/core'; | |
import { | |
App, | |
Config, | |
DeepLinker, | |
Form, | |
IonicModule, | |
Keyboard, | |
DomController, | |
MenuController, | |
NavController, | |
Platform, | |
} from 'ionic-angular'; | |
import { ConfigMock, PlatformMock } from 'ionic-mocks'; | |
import { ClickersServiceMock } from './services/clickers.mock'; | |
import { ClickersService } from './services'; | |
import { TranslateServiceMock } from './services/translate.mock'; | |
import { TranslatePipeMock } from './pipes/translate.pipe.mock'; | |
declare const require: any; | |
// First, initialize the Angular testing environment. | |
getTestBed().initTestEnvironment( | |
BrowserDynamicTestingModule, | |
platformBrowserDynamicTesting(), | |
); | |
// Then we find all the tests. | |
const context: any = require.context('./', true, /\.spec\.ts$/); | |
// And load the modules. | |
context.keys().map(context); | |
export class TestUtils { | |
public static beforeEachCompiler(components: Array<any>): Promise<{fixture: any, instance: any}> { | |
return TestUtils.configureIonicTestingModule(components) | |
.compileComponents().then(() => { | |
let fixture: any = TestBed.createComponent(components[0]); | |
return { | |
fixture: fixture, | |
instance: fixture.debugElement.componentInstance, | |
}; | |
}); | |
} | |
public static configureIonicTestingModule(components: Array<any>): typeof TestBed { | |
return TestBed.configureTestingModule({ | |
declarations: [ | |
...components, | |
TranslatePipeMock, | |
], | |
providers: [ | |
App, Form, Keyboard, DomController, MenuController, NavController, | |
{provide: Platform, useFactory: () => PlatformMock.instance()}, | |
{provide: Config, useFactory: () => ConfigMock.instance()}, | |
{provide: DeepLinker, useFactory: () => ConfigMock.instance()}, | |
{provide: ClickersService, useClass: ClickersServiceMock}, | |
{provide: TranslateService, useClass: TranslateServiceMock}, | |
], | |
imports: [ | |
FormsModule, | |
IonicModule, | |
ReactiveFormsModule, | |
TranslateModule, | |
], | |
}); | |
} | |
// http://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript | |
public static eventFire(el: any, etype: string): void { | |
if (el.fireEvent) { | |
el.fireEvent('on' + etype); | |
} else { | |
let evObj: any = document.createEvent('Events'); | |
evObj.initEvent(etype, true, false); | |
el.dispatchEvent(evObj); | |
} | |
} | |
} |