diff --git a/karma.conf.js b/karma.conf.js index 937e002..813fa06 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -9,7 +9,7 @@ var _ = require('lodash'); var wiredep = require('wiredep'); var pathSrcHtml = [ - path.join(conf.paths.src, '/**/*.html') + path.join('./src/app/**/*.html') ]; var glob = require("glob"); @@ -75,7 +75,7 @@ module.exports = function (config) { ngHtml2JsPreprocessor: { stripPrefix: conf.paths.src + '/', - moduleName: 'angular' + moduleName: 'templates' }, @@ -132,7 +132,7 @@ module.exports = function (config) { // 'src/**/*.js': ['sourcemap'], // 'src/**/*.[sS]pec.ts': ['sourcemap'] // }; - + pathSrcHtml.forEach(function (path) { configuration.preprocessors[path] = ['ng-html2js']; }); diff --git a/package.json b/package.json index 8b416fa..8005830 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "moment": "^2.11.2" }, "scripts": { - "test": "webpack -w & karma start", + "test": "concurrently \"webpack -w\" \"karma start\"", "postinstall": "npm install -g bower && bower install", "start": "concurrently \"webpack -w\" \"gulp serve\"" }, diff --git a/src/app/components/noosfero-articles/article/article.directive.spec.ts b/src/app/components/noosfero-articles/article/article.directive.spec.ts index 6e2fd5c..f45d554 100644 --- a/src/app/components/noosfero-articles/article/article.directive.spec.ts +++ b/src/app/components/noosfero-articles/article/article.directive.spec.ts @@ -7,39 +7,62 @@ import {ArticleDirective} from './article.directive'; // In ng2 you inject tcb. const tcb = new TestComponentBuilder(); +// this htmlTemplate will be re-used between the container components in this spec file +const htmlTemplate: string = ''; -let html = ''; -// Create a component to include your testing component -@Component({ selector: 'my-test', template: html, directives: [ArticleDirective] }) -class TestArticleDirective { - article = { type: 'TinyMceArticle' }; - profile = { name: 'profile-name' }; - constructor() { - } -} +describe("Article Directive", () => { + // the karma preprocessor html2js transform the templates html into js files which put + // the templates to the templateCache into the module templates + // we need to load the module templates here as the template for the + // component NoosferoArtileDirective will be load on our tests + beforeEach(angular.mock.module("templates")); -describe("Article Directive", () => { it("receives the article and profile as inputs", done => { + + // Creating a container component (ArticleContainerComponent) to include + // the component under test (ArticleDirective) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleDirective] }) + class ArticleContainerComponent { + article = { type: 'anyArticleType' }; + profile = { name: 'profile-name' }; + constructor() { + } + } + + // uses the TestComponentBuilder instance to initialize the component tcb - .createAsync(TestArticleDirective).then(fixture => { + .createAsync(ArticleContainerComponent).then(fixture => { + // and here we can inspect and run the test assertions let myComponent: ArticleDirective = fixture.componentInstance; - expect(myComponent.article.type).toEqual("TinyMceArticle"); + + // assure the article object inside the ArticleDirective matches + // the provided through the parent component + expect(myComponent.article.type).toEqual("anyArticleType"); expect(myComponent.profile.name).toEqual("profile-name"); - console.log(fixture.debugElement); + + // done needs to be called (it isn't really needed, as we can read in + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) + // because createAsync in ng-forward is not really async, but as the intention + // here is write tests in angular 2 ways, this is recommended done(); }); }); - it("renders a directive corresponding to the article type", done => { - - @Component({ selector: 'noosfero-TinyMceArticle', template: "

custom component

" }) - class CustomArticleComponent { + it("renders a directive which matches to the article type", done => { + // NoosferoTinyMceArticle component created to check if it will be used + // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleDirective) + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider + @Component({ selector: 'noosfero-tiny-mce-article', template: "

TinyMceArticle

" }) + class NoosferoTinyMceArticle { + @Input() article: any; + @Input() profile: any; } - @Component({ selector: 'custom-article-type-test', template: html, directives: [ArticleDirective, CustomArticleComponent] }) + // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleDirective, NoosferoTinyMceArticle] }) class CustomArticleType { article = { type: 'TinyMceArticle' }; profile = { name: 'profile-name' }; @@ -50,8 +73,7 @@ describe("Article Directive", () => { .createAsync(CustomArticleType).then(fixture => { let myComponent: CustomArticleType = fixture.componentInstance; expect(myComponent.article.type).toEqual("TinyMceArticle"); - expect(myComponent.profile.name).toEqual("profile-name"); - console.log(fixture.debugElement); + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); done(); }); }); diff --git a/src/app/components/noosfero-articles/article/article.directive.ts b/src/app/components/noosfero-articles/article/article.directive.ts index 4331be5..212d23e 100644 --- a/src/app/components/noosfero-articles/article/article.directive.ts +++ b/src/app/components/noosfero-articles/article/article.directive.ts @@ -11,7 +11,7 @@ export class ArticleView { @Input() profile: any; constructor() { - console.log("ARTICLE VIEW"); + } } -- libgit2 0.21.2