aodb/app/infrastructure/ComponentHelper.ts
Martin Donnelly afe73b5baa upload
2016-12-22 00:00:06 +00:00

73 lines
2.2 KiB
TypeScript

class BaseComponentTest {
public $compile: ng.ICompileService;
public $rootScope: ng.IRootScopeService;
public $httpBackend: ng.IHttpBackendService;
public $templateCache: ng.ITemplateCacheService;
constructor() {
angular.mock.inject(($compile, $rootScope, $httpBackend, $templateCache) => {
this.$compile = $compile;
this.$rootScope = $rootScope;
this.$httpBackend = $httpBackend;
this.$templateCache = $templateCache;
});
}
}
export class ComponentTest extends BaseComponentTest {
public templateURL: string;
public componentName: string;
public flushOnInit: boolean;
constructor(componentName: string, templateURL: string) {
super();
this.componentName = componentName;
this.templateURL = templateURL;
this.flushOnInit = true;
this.$httpBackend.expectGET(this.templateURL)
.respond(201, this.$templateCache.get(this.templateURL));
}
public compile<T>(args: any = null, customCompileString: string = null): T {
let actCompName = this.componentName.replace(/([a-z])([A-Z])/g, '$1:$2');
let scope: any = this.$rootScope.$new();
let dir: any;
if (args) {
console.log(args);
angular.extend(this.$rootScope, args);
if (customCompileString) {
dir = this.$compile(angular.element(customCompileString))(scope);
} else {
console.log(args);
dir = this.$compile(angular.element(`<${actCompName}></${actCompName}>`))(scope);
console.log(dir);
}
scope.$digest();
if (this.flushOnInit) {
this.$httpBackend.flush();
}
} else {
dir = this.$compile(angular.element(`<${actCompName}></${actCompName}>`))(scope);
scope.$digest();
if (this.flushOnInit) {
this.$httpBackend.flush();
}
}
if (dir.controller(this.componentName) === undefined) {
console.log(this.componentName);
console.log(dir);
} else {
console.log(dir);
}
return dir.controller(this.componentName);
}
}