Commit a5f80da7da1408da9f2f2ef58feeff9aaf08d42b

Authored by Victor Costa
1 parent 29d769ad

Add tests for article editor

src/app/article/cms/article-editor/article-editor.component.spec.ts 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +import {ArticleEditorComponent} from './article-editor.component';
  2 +import {BasicEditorComponent} from "../basic-editor/basic-editor.component";
  3 +import {ComponentTestHelper, createClass} from '../../../../spec/component-test-helper';
  4 +import * as helpers from "../../../../spec/helpers";
  5 +
  6 +const htmlTemplate: string = '<article-editor [article]="ctrl.article"></article-editor>';
  7 +
  8 +describe("Components", () => {
  9 + describe("Article Editor Component", () => {
  10 +
  11 + let helper: ComponentTestHelper<ArticleEditorComponent>;
  12 + beforeEach(angular.mock.module("templates"));
  13 +
  14 + beforeEach((done) => {
  15 + let properties = { article: { type: "TextArticle" } };
  16 + let cls = createClass({
  17 + template: htmlTemplate,
  18 + directives: [ArticleEditorComponent, BasicEditorComponent],
  19 + properties: properties
  20 + });
  21 + helper = new ComponentTestHelper<ArticleEditorComponent>(cls, done);
  22 + });
  23 +
  24 + it("replace element with article basic editor when type is TextArticle", () => {
  25 + expect(helper.find("article-basic-editor").length).toEqual(1);
  26 + });
  27 + });
  28 +});
... ...
src/app/article/cms/article-editor/article-editor.component.ts
... ... @@ -16,10 +16,10 @@ export class ArticleEditorComponent {
16 16 private $compile: ng.ICompileService) { }
17 17  
18 18 ngOnInit() {
19   - let articleType = this.article.type.replace(/::/, '');
  19 + let articleType = this.article && this.article.type ? this.article.type.replace(/::/, '') : "TextArticle";
20 20 let specificDirective = `${articleType.charAt(0).toLowerCase()}${articleType.substring(1)}Editor`;
21 21 let directiveName = "article-basic-editor";
22   - if (this.$injector.has(specificDirective + 'Directive')) {
  22 + if (specificDirective !== "articleEditor" && this.$injector.has(specificDirective + 'Directive')) {
23 23 directiveName = specificDirective.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
24 24 }
25 25 this.$element.replaceWith(this.$compile('<' + directiveName + ' [article]="ctrl.article"></' + directiveName + '>')(this.$scope));
... ...
src/spec/component-test-helper.ts
... ... @@ -88,10 +88,11 @@ export class ComponentTestHelper&lt;T extends any&gt; {
88 88 this.debugElement = fixture.debugElement;
89 89 this.component = <T>this.debugElement.componentViewChildren[0].componentInstance;
90 90 let mockObj = new this.mockComponent();
91   - Object.keys(mockObj).forEach((key: any) => {
92   - (<any>this.component)[key] = <any>mockObj[key];
93   - });
94   -
  91 + if (this.component) {
  92 + Object.keys(mockObj).forEach((key: any) => {
  93 + (<any>this.component)[key] = <any>mockObj[key];
  94 + });
  95 + }
95 96 }).then(() => {
96 97 // Force the resolution of components and sync
97 98 done();
... ... @@ -99,9 +100,9 @@ export class ComponentTestHelper&lt;T extends any&gt; {
99 100 }
100 101  
101 102 changeProperties(properties: any) {
102   - Object.keys(properties).forEach((key: any) => {
103   - this.component[key] = properties[key];
104   - });
  103 + Object.keys(properties).forEach((key: any) => {
  104 + this.component[key] = properties[key];
  105 + });
105 106 }
106 107  
107 108 /**
... ...