From e83eb73f5b8b3b171be4633dabc319d142abb33a Mon Sep 17 00:00:00 2001 From: ABNER SILVA DE OLIVEIRA Date: Fri, 4 Mar 2016 18:07:26 -0300 Subject: [PATCH] grouping the specs in groups [Services, Components, ...] --- src/app/components/auth/auth_controller.spec.ts | 37 +++++++++++++++++++++---------------- src/app/components/auth/auth_service.spec.ts | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- src/app/components/auth/auth_service.ts | 2 +- src/app/components/auth/session_spec.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/app/components/navbar/navbar.spec.ts | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------- src/app/components/noosfero-articles/article/article_view.spec.ts | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------- src/app/components/noosfero-blocks/block.component.spec.ts | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------- src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts | 84 ++++++++++++++++++++++++++++++++++++++++++++---------------------------------------- src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts | 57 +++++++++++++++++++++++++++++---------------------------- src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts | 70 ++++++++++++++++++++++++++++++++++++---------------------------------- src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts | 40 +++++++++++++++++++++------------------- src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts | 70 ++++++++++++++++++++++++++++++++++++---------------------------------- src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts | 4 ++++ src/spec/helpers.ts | 30 ++++++++++++++++++++++++++++-- 14 files changed, 543 insertions(+), 419 deletions(-) create mode 100644 src/app/components/auth/session_spec.ts diff --git a/src/app/components/auth/auth_controller.spec.ts b/src/app/components/auth/auth_controller.spec.ts index 04572c7..fce0828 100644 --- a/src/app/components/auth/auth_controller.spec.ts +++ b/src/app/components/auth/auth_controller.spec.ts @@ -1,28 +1,33 @@ import {AuthController} from "./auth_controller"; import {AuthService} from "./auth_service"; -describe("AuthController", () => { +describe("Controllers", () => { - it("calls authenticate on AuthService when login called", () => { - // creating a Mock AuthService - let AuthServiceMock: AuthService = jasmine.createSpyObj("AuthService", ["login"]); + describe("AuthController", () => { - // pass AuthServiceMock into the constructor - let authController = new AuthController(null, null, null, AuthServiceMock); + it("calls authenticate on AuthService when login called", () => { - // setup of authController -> set the credentials instance property - let credentials = { username: "username", password: "password" }; + // creating a Mock AuthService + let AuthServiceMock: AuthService = jasmine.createSpyObj("AuthService", ["login"]); - authController.credentials = credentials; + // pass AuthServiceMock into the constructor + let authController = new AuthController(null, null, null, AuthServiceMock); - // calls the authController login method - authController.login(); + // setup of authController -> set the credentials instance property + let credentials = { username: "username", password: "password" }; - // checks if the method login of the injected AuthService has been called - expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials); + authController.credentials = credentials; - }); - + // calls the authController login method + authController.login(); + + // checks if the method login of the injected AuthService has been called + expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials); + + }); -}); + + + }); +}); \ No newline at end of file diff --git a/src/app/components/auth/auth_service.spec.ts b/src/app/components/auth/auth_service.spec.ts index 5a10ed3..bbaa147 100644 --- a/src/app/components/auth/auth_service.spec.ts +++ b/src/app/components/auth/auth_service.spec.ts @@ -3,75 +3,78 @@ import {AuthService, AUTH_EVENTS} from "./"; import {User, Credentials} from "./../../models/interfaces"; -describe("Auth Service", () => { +describe("Services", () => { - let $httpBackend: ng.IHttpBackendService; - let authService: AuthService; - let credentials: Credentials; - let $rootScope: ng.IRootScopeService; - let user: User; - beforeEach(angular.mock.module("noosferoApp")); + describe("Auth Service", () => { - beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => { - $httpBackend = _$httpBackend_; - authService = _AuthService_; - $rootScope = _$rootScope_; + let $httpBackend: ng.IHttpBackendService; + let authService: AuthService; + let credentials: Credentials; + let $rootScope: ng.IRootScopeService; + let user: User; - user = { - id: 1, - login: "user" - }; - })); + beforeEach(angular.mock.module("noosferoApp")); + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => { + $httpBackend = _$httpBackend_; + authService = _AuthService_; + $rootScope = _$rootScope_; - describe("Succesffull login", () => { + user = { + id: 1, + login: "user" + }; + })); - beforeEach(() => { - credentials = { username: "user", password: "password" }; - $httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user }); - }); + describe("Succesffull login", () => { + + beforeEach(() => { + credentials = { username: "user", password: "password" }; - it("should return loggedUser", (done) => { - authService.login(credentials).then((loggedUser) => { - expect(loggedUser).toBeDefined(); - done(); + $httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user }); }); - $httpBackend.flush(); - expect($httpBackend.verifyNoOutstandingRequest()); - }); + it("should return loggedUser", (done) => { + authService.login(credentials).then((loggedUser) => { + expect(loggedUser).toBeDefined(); + done(); + }); + $httpBackend.flush(); + expect($httpBackend.verifyNoOutstandingRequest()); + }); + + + it("should emit event loggin successful with user logged data", () => { + + authService.login(credentials); - it("should emit event loggin successful with user logged data", () => { + let eventEmmited: boolean = false; + $rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => { + eventEmmited = true; + expect(userThroughEvent).toEqual(user) + }); - authService.login(credentials); + $httpBackend.flush(); - let eventEmmited: boolean = false; - $rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => { - eventEmmited = true; - expect(userThroughEvent).toEqual(user) + expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!"); }); - $httpBackend.flush(); + it("should return the current logged in user", () => { + authService.login(credentials); + $httpBackend.flush(); + let actual: User = authService.currentUser(); + expect(actual).toEqual(user, "The returned user must be present"); + }); - expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!"); - }); - - it("should return the current logged in user", () => { - authService.login(credentials); - $httpBackend.flush(); - let actual: User = authService.currentUser(); - expect(actual).toEqual(user, "The returned user must be present"); + it("should not return the current user after logout", () => { + authService.logout(); + let actual: any = authService.currentUser(); + expect(actual).toEqual(undefined, "The returned user must not be defined"); + }); }); - it("should not return the current user after logout", () => { - authService.logout(); - let actual: any = authService.currentUser(); - expect(actual).toEqual(undefined, "The returned user must not be defined"); - }); - }); - -}); - \ No newline at end of file + }); +}); \ No newline at end of file diff --git a/src/app/components/auth/auth_service.ts b/src/app/components/auth/auth_service.ts index 95fad2d..2b3cbc3 100644 --- a/src/app/components/auth/auth_service.ts +++ b/src/app/components/auth/auth_service.ts @@ -55,7 +55,7 @@ export class AuthService { public isAuthenticated() { return !!this.session.currentUser(); } - + public currentUser(): User { return this.session.currentUser(); } diff --git a/src/app/components/auth/session_spec.ts b/src/app/components/auth/session_spec.ts new file mode 100644 index 0000000..30d0fb5 --- /dev/null +++ b/src/app/components/auth/session_spec.ts @@ -0,0 +1,49 @@ +import {Component} from "ng-forward"; +import {Session} from "./"; +import {fixtures, createComponentFromClass, createProviderToValue} from "./../../../spec/helpers"; +import {UserResponse, User, INoosferoLocalStorage} from "./../../models/interfaces"; + + +describe("Services", () => { + + + describe("Session Service", () => { + + let $localStorage: INoosferoLocalStorage = null; + let $log: any; + + beforeEach(() => { + $localStorage = { currentUser: null }; + $log = jasmine.createSpyObj('$log', ['debug']); + }); + + it("method 'create()' saves the current user on $localstorage service", () => { + let session = new Session($localStorage, $log); + let userResponse = { + user: fixtures.user + }; + session.create(userResponse); + expect($localStorage.currentUser).toEqual(userResponse.user); + }); + + it("method 'destroy()' clean the currentUser on $localstorage", () => { + let session = new Session($localStorage, $log); + let userResponse = { + user: fixtures.user + }; + $localStorage.currentUser = fixtures.user; + session.destroy(); + expect($localStorage.currentUser).toBeUndefined(); + }); + + it("method 'currentUser()' returns the user recorded on $localstorage service", () => { + let session = new Session($localStorage, $log); + let userResponse = { + user: fixtures.user + }; + $localStorage.currentUser = fixtures.user; + expect(session.currentUser()).toEqual($localStorage.currentUser); + }); + }); + +}); \ No newline at end of file diff --git a/src/app/components/navbar/navbar.spec.ts b/src/app/components/navbar/navbar.spec.ts index 299ef8b..20ecbed 100644 --- a/src/app/components/navbar/navbar.spec.ts +++ b/src/app/components/navbar/navbar.spec.ts @@ -1,54 +1,76 @@ -import {createComponentFromClass, quickCreateComponent} from "./../../../spec/helpers"; -import {Navbar} from "./"; -import {AUTH_EVENTS} from "./../auth"; -import {User} from "./../../models/interfaces"; -import {Injectable, Provider, provide} from "ng-forward"; +import { +createComponentFromClass, +quickCreateComponent, +provideEmptyObjects +} from "./../../../spec/helpers"; +import { +Navbar +} from "./"; +import { +AUTH_EVENTS +} from "./../auth"; +import { +User +} from "./../../models/interfaces"; +import { +Injectable, +Provider, +provide +} from "ng-forward"; +describe("Components", () => { -describe("Navbar Component", () => { - let $rootScope: ng.IRootScopeService; - let user = { - id: 1, - login: "user" - }; + describe("Navbar Component", () => { - beforeEach(angular.mock.module("templates")); + let $rootScope: ng.IRootScopeService; + let user = { + id: 1, + login: "user" + }; - // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => { - // $rootScope = _$rootScope_; - // })); + beforeEach(angular.mock.module("templates")); - it('should get the loggedIn user', (done: Function) => { + // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => { + // $rootScope = _$rootScope_; + // })); - let scope = jasmine.createSpyObj("scope", ["$on"]); + it('should get the loggedIn user', (done: Function) => { - let providers = [ + let scope = jasmine.createSpyObj("scope", ["$on"]); + let providers = [ + provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), + new Provider('Session', { + useValue: { + currentUser: () => { + return user; + } + } + }), + new Provider('$scope', { + useValue: scope + }), + new Provider('AUTH_EVENTS', { + useValue: { + AUTH_EVENTS + } + }) + ]; - new Provider('moment', { useValue: {} }), - new Provider('$modal', { useValue: {} }), - new Provider('AuthService', { useValue: {} }), - new Provider('Session', { useValue: { - currentUser: () => { return user; } - } }), - new Provider('$scope', { useValue: scope }), - new Provider('$state', { useValue: {} }), - new Provider('AUTH_EVENTS', { useValue: {AUTH_EVENTS} }) - ]; - - quickCreateComponent({ - providers: providers, - template: "", - directives: [Navbar] - }).then(fixture => { - let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; - expect(navbarInstance).toBeDefined(); - expect(navbarInstance["currentUser"]).toEqual(user) - done(); + quickCreateComponent({ + providers: providers, + template: "", + directives: [Navbar] + }).then(fixture => { + let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; + expect(navbarInstance).toBeDefined(); + expect(navbarInstance["currentUser"]).toEqual(user) + done(); + }); }); - }); + }); }); \ No newline at end of file diff --git a/src/app/components/noosfero-articles/article/article_view.spec.ts b/src/app/components/noosfero-articles/article/article_view.spec.ts index 9e2b9c7..f793be7 100644 --- a/src/app/components/noosfero-articles/article/article_view.spec.ts +++ b/src/app/components/noosfero-articles/article/article_view.spec.ts @@ -8,98 +8,101 @@ import {createComponentFromClass, quickCreateComponent} from "../../../../spec/h const htmlTemplate: string = ''; -describe("ArticleView Component", () => { - - // 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 Noosfero ArtileView will be load on our tests - beforeEach(angular.mock.module("templates")); - - it("renders the default component when no specific component is found", (done: Function) => { - // Creating a container component (ArticleContainerComponent) to include - // the component under test (ArticleView) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) - class ArticleContainerComponent { - article = { type: 'anyArticleType' }; - profile = { name: 'profile-name' }; - constructor() { +describe("Components", () => { + + describe("ArticleView Component", () => { + + // 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 Noosfero ArtileView will be load on our tests + beforeEach(angular.mock.module("templates")); + + it("renders the default component when no specific component is found", (done: Function) => { + // Creating a container component (ArticleContainerComponent) to include + // the component under test (ArticleView) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) + class ArticleContainerComponent { + article = { type: 'anyArticleType' }; + profile = { name: 'profile-name' }; + constructor() { + } } - } - createComponentFromClass(ArticleContainerComponent).then((fixture) => { - // and here we can inspect and run the test assertions + createComponentFromClass(ArticleContainerComponent).then((fixture) => { + // and here we can inspect and run the test assertions - // gets the children component of ArticleContainerComponent - let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; + // gets the children component of ArticleContainerComponent + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; - // and checks if the article View rendered was the Default Article View - expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype); + // and checks if the article View rendered was the Default Article View + expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype); - // 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(); - }); + // 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("receives the article and profile as inputs", (done: Function) => { + it("receives the article and profile as inputs", (done: Function) => { - // Creating a container component (ArticleContainerComponent) to include - // the component under test (ArticleView) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) - class ArticleContainerComponent { - article = { type: 'anyArticleType' }; - profile = { name: 'profile-name' }; - constructor() { + // Creating a container component (ArticleContainerComponent) to include + // the component under test (ArticleView) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) + class ArticleContainerComponent { + article = { type: 'anyArticleType' }; + profile = { name: 'profile-name' }; + constructor() { + } } - } - - // uses the TestComponentBuilder instance to initialize the component - createComponentFromClass(ArticleContainerComponent).then((fixture) => { - // and here we can inspect and run the test assertions - let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; - - // assure the article object inside the ArticleView matches - // the provided through the parent component - expect(articleView.article.type).toEqual("anyArticleType"); - expect(articleView.profile.name).toEqual("profile-name"); - - // 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(); + + // uses the TestComponentBuilder instance to initialize the component + createComponentFromClass(ArticleContainerComponent).then((fixture) => { + // and here we can inspect and run the test assertions + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; + + // assure the article object inside the ArticleView matches + // the provided through the parent component + expect(articleView.article.type).toEqual("anyArticleType"); + expect(articleView.profile.name).toEqual("profile-name"); + + // 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 article view 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 (ArticleView) - // *** 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 TinyMceArticleView { - @Input() article: any; - @Input() profile: any; - } - - // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] }) - class CustomArticleType { - article = { type: 'TinyMceArticle' }; - profile = { name: 'profile-name' }; - constructor() { + it("renders a article view 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 (ArticleView) + // *** 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 TinyMceArticleView { + @Input() article: any; + @Input() profile: any; } - } - createComponentFromClass(CustomArticleType).then(fixture => { - let myComponent: CustomArticleType = fixture.componentInstance; - expect(myComponent.article.type).toEqual("TinyMceArticle"); - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); - done(); + + // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] }) + class CustomArticleType { + article = { type: 'TinyMceArticle' }; + profile = { name: 'profile-name' }; + constructor() { + } + } + createComponentFromClass(CustomArticleType).then(fixture => { + let myComponent: CustomArticleType = fixture.componentInstance; + expect(myComponent.article.type).toEqual("TinyMceArticle"); + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); + done(); + }); }); - }); + }); }); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/block.component.spec.ts b/src/app/components/noosfero-blocks/block.component.spec.ts index ea12533..842ae01 100644 --- a/src/app/components/noosfero-blocks/block.component.spec.ts +++ b/src/app/components/noosfero-blocks/block.component.spec.ts @@ -7,84 +7,85 @@ const tcb = new TestComponentBuilder(); const htmlTemplate: string = ''; - -describe("Block Component", () => { - - // 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 Block will be load on our tests - beforeEach(angular.mock.module("templates")); - - it("receives the block and the owner as inputs", done => { - - // Creating a container component (BlockContainerComponent) to include - // the component under test (Block) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] }) - class BlockContainerComponent { - block = { type: 'Block' }; - owner = { name: 'profile-name' }; - constructor() { +describe("Components", () => { + describe("Block Component", () => { + + // 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 Block will be load on our tests + beforeEach(angular.mock.module("templates")); + + it("receives the block and the owner as inputs", done => { + + // Creating a container component (BlockContainerComponent) to include + // the component under test (Block) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] }) + class BlockContainerComponent { + block = { type: 'Block' }; + owner = { name: 'profile-name' }; + constructor() { + } } - } - - // uses the TestComponentBuilder instance to initialize the component - tcb - .createAsync(BlockContainerComponent).then(fixture => { - // and here we can inspect and run the test assertions - let myComponent: Block = fixture.componentInstance; - - // assure the block object inside the Block matches - // the provided through the parent component - expect(myComponent.block.type).toEqual("Block"); - expect(myComponent.owner.name).toEqual("profile-name"); - done(); - }); - }); - - it("renders a component which matches to the block type", done => { - // CustomBlock component created to check if it will be used - // when a block with type 'CustomBlock' is provided to the noosfero-block (Block) - // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider - @Component({ selector: 'noosfero-custom-block', template: "

My Custom Block

" }) - class CustomBlock { - @Input() block: any; - @Input() owner: any; - } - - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block, CustomBlock] }) - class CustomBlockType { - block = { type: 'CustomBlock' }; - owner = { name: 'profile-name' }; - constructor() { + // uses the TestComponentBuilder instance to initialize the component + tcb + .createAsync(BlockContainerComponent).then(fixture => { + // and here we can inspect and run the test assertions + let myComponent: Block = fixture.componentInstance; + + // assure the block object inside the Block matches + // the provided through the parent component + expect(myComponent.block.type).toEqual("Block"); + expect(myComponent.owner.name).toEqual("profile-name"); + done(); + }); + }); + + + it("renders a component which matches to the block type", done => { + // CustomBlock component created to check if it will be used + // when a block with type 'CustomBlock' is provided to the noosfero-block (Block) + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider + @Component({ selector: 'noosfero-custom-block', template: "

My Custom Block

" }) + class CustomBlock { + @Input() block: any; + @Input() owner: any; } - } - tcb - .createAsync(CustomBlockType).then(fixture => { - let myComponent: CustomBlockType = fixture.componentInstance; - expect(myComponent.block.type).toEqual("CustomBlock"); - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("My Custom Block"); - done(); - }); - }); - - it("renders the default block when hasn't defined a block type", done => { - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] }) - class CustomBlockType { - block: any = { type: null }; - owner: any = { name: 'profile-name' }; - constructor() { + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block, CustomBlock] }) + class CustomBlockType { + block = { type: 'CustomBlock' }; + owner = { name: 'profile-name' }; + constructor() { + } } - } - tcb - .createAsync(CustomBlockType).then(fixture => { - let myComponent: CustomBlockType = fixture.componentInstance; - expect(myComponent.block.type).toBeNull(); - expect(!!fixture.debugElement.nativeElement.querySelector("noosfero-default-block")).toBeTruthy(); - done(); - }); - }); + tcb + .createAsync(CustomBlockType).then(fixture => { + let myComponent: CustomBlockType = fixture.componentInstance; + expect(myComponent.block.type).toEqual("CustomBlock"); + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("My Custom Block"); + done(); + }); + }); + + + it("renders the default block when hasn't defined a block type", done => { + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] }) + class CustomBlockType { + block: any = { type: null }; + owner: any = { name: 'profile-name' }; + constructor() { + } + } + tcb + .createAsync(CustomBlockType).then(fixture => { + let myComponent: CustomBlockType = fixture.componentInstance; + expect(myComponent.block.type).toBeNull(); + expect(!!fixture.debugElement.nativeElement.querySelector("noosfero-default-block")).toBeTruthy(); + done(); + }); + }); -}); + }); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts b/src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts index ea13d21..3283172 100644 --- a/src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts +++ b/src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts @@ -9,53 +9,57 @@ const tcb = new TestComponentBuilder(); const htmlTemplate: string = ''; -describe("Link List Block Component", () => { +describe("Components", () => { - beforeEach(angular.mock.module("templates")); + describe("Link List Block Component", () => { - it("receives the block and the owner as inputs", done => { + beforeEach(angular.mock.module("templates")); - // Creating a container component (BlockContainerComponent) to include - // the component under test (Block) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] }) - class BlockContainerComponent { - block = { type: 'Block' }; - owner = { name: 'profile-name' }; - constructor() { + it("receives the block and the owner as inputs", done => { + + // Creating a container component (BlockContainerComponent) to include + // the component under test (Block) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] }) + class BlockContainerComponent { + block = { type: 'Block' }; + owner = { name: 'profile-name' }; + constructor() { + } } - } - - // uses the TestComponentBuilder instance to initialize the component - //.overrideView(LinkListBlock, { template: 'asdasdasd', pipes: [NoosferoTemplate] }) - tcb.createAsync(BlockContainerComponent).then(fixture => { - // and here we can inspect and run the test assertions - let myComponent: LinkListBlock = fixture.componentInstance; - - // assure the block object inside the Block matches - // the provided through the parent component - expect(myComponent.block.type).toEqual("Block"); - expect(myComponent.owner.name).toEqual("profile-name"); - done(); + + // uses the TestComponentBuilder instance to initialize the component + //.overrideView(LinkListBlock, { template: 'asdasdasd', pipes: [NoosferoTemplate] }) + tcb.createAsync(BlockContainerComponent).then(fixture => { + // and here we can inspect and run the test assertions + let myComponent: LinkListBlock = fixture.componentInstance; + + // assure the block object inside the Block matches + // the provided through the parent component + expect(myComponent.block.type).toEqual("Block"); + expect(myComponent.owner.name).toEqual("profile-name"); + done(); + }); }); - }); - it("display links stored in block settings", done => { - - @Component({ - selector: 'test-container-component', - template: htmlTemplate, - directives: [LinkListBlock], - providers: provideFilters("noosferoTemplateFilter") - }) - class CustomBlockType { - block: any = { settings: { links: [{ name: 'link1', address: 'address1' }, { name: 'link2', address: 'address2' }] } }; - owner: any = { name: 'profile-name' }; - } - tcb.createAsync(CustomBlockType).then(fixture => { - expect(fixture.debugElement.queryAll(".link-list-block a").length).toEqual(2); - done(); + it("display links stored in block settings", done => { + + @Component({ + selector: 'test-container-component', + template: htmlTemplate, + directives: [LinkListBlock], + providers: provideFilters("noosferoTemplateFilter") + }) + class CustomBlockType { + block: any = { settings: { links: [{ name: 'link1', address: 'address1' }, { name: 'link2', address: 'address2' }] } }; + owner: any = { name: 'profile-name' }; + } + tcb.createAsync(CustomBlockType).then(fixture => { + expect(fixture.debugElement.queryAll(".link-list-block a").length).toEqual(2); + done(); + }); }); + }); -}); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts b/src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts index 356168f..fc51131 100644 --- a/src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts +++ b/src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts @@ -8,33 +8,34 @@ const tcb = new TestComponentBuilder(); const htmlTemplate: string = ''; - -describe("Main Block Component", () => { - - // 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 Block will be load on our tests - beforeEach(angular.mock.module("templates")); - - it("check if the main block has a tag with ui-view attribute", done => { - - // Creating a container component (BlockContainerComponent) to include - // the component under test (Block) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MainBlock] }) - class BlockContainerComponent { - } - - // uses the TestComponentBuilder instance to initialize the component - tcb.createAsync(BlockContainerComponent).then(fixture => { - // and here we can inspect and run the test assertions - //let myComponent: MainBlock = fixture.componentInstance; - - // assure the block object inside the Block matches - // the provided through the parent component - expect(fixture.debugElement.queryAll('[ui-view="mainBlockContent"]').length).toEqual(1) - done(); +describe("Components", () => { + describe("Main Block Component", () => { + + // 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 Block will be load on our tests + beforeEach(angular.mock.module("templates")); + + it("check if the main block has a tag with ui-view attribute", done => { + + // Creating a container component (BlockContainerComponent) to include + // the component under test (Block) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MainBlock] }) + class BlockContainerComponent { + } + + // uses the TestComponentBuilder instance to initialize the component + tcb.createAsync(BlockContainerComponent).then(fixture => { + // and here we can inspect and run the test assertions + //let myComponent: MainBlock = fixture.componentInstance; + + // assure the block object inside the Block matches + // the provided through the parent component + expect(fixture.debugElement.queryAll('[ui-view="mainBlockContent"]').length).toEqual(1) + done(); + }); }); - }); -}); + }); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts b/src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts index 9886427..a621154 100644 --- a/src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts +++ b/src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts @@ -7,45 +7,47 @@ const htmlTemplate: string = ' { - - beforeEach(angular.mock.module("templates")); - - let state = jasmine.createSpyObj("state", ["go"]); - let providers = [ - new Provider('truncateFilter', { useValue: () => { } }), - new Provider('stripTagsFilter', { useValue: () => { } }), - new Provider('$state', { useValue: state }), - new Provider('ProfileService', { - useValue: { - getProfileMembers: (profileId: number, filters: any): any => { - return Promise.resolve({ data: { people: [{ identifier: "person1" }] } }); +describe("Components", () => { + describe("Members Block Component", () => { + + beforeEach(angular.mock.module("templates")); + + let state = jasmine.createSpyObj("state", ["go"]); + let providers = [ + new Provider('truncateFilter', { useValue: () => { } }), + new Provider('stripTagsFilter', { useValue: () => { } }), + new Provider('$state', { useValue: state }), + new Provider('ProfileService', { + useValue: { + getProfileMembers: (profileId: number, filters: any): any => { + return Promise.resolve({ data: { people: [{ identifier: "person1" }] } }); + } } + }), + ]; + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MembersBlock], providers: providers }) + class BlockContainerComponent { + block = { type: 'Block', settings: {} }; + owner = { name: 'profile-name' }; + constructor() { } - }), - ]; - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MembersBlock], providers: providers }) - class BlockContainerComponent { - block = { type: 'Block', settings: {} }; - owner = { name: 'profile-name' }; - constructor() { } - } - it("get members of the block owner", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - let block: MembersBlock = fixture.debugElement.componentViewChildren[0].componentInstance; - expect(block.members).toEqual([{ identifier: "person1" }]); - done(); + it("get members of the block owner", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + let block: MembersBlock = fixture.debugElement.componentViewChildren[0].componentInstance; + expect(block.members).toEqual([{ identifier: "person1" }]); + done(); + }); }); - }); - it("render the profile image for each member", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - fixture.debugElement.getLocal("$rootScope").$apply(); - expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1); - done(); + it("render the profile image for each member", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + fixture.debugElement.getLocal("$rootScope").$apply(); + expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1); + done(); + }); }); - }); -}); + }); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts b/src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts index f94465c..24d8436 100644 --- a/src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts +++ b/src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts @@ -8,30 +8,32 @@ const tcb = new TestComponentBuilder(); const htmlTemplate: string = ''; -describe("Profile Image Block Component", () => { +describe("Components", () => { + describe("Profile Image Block Component", () => { - beforeEach(angular.mock.module("templates")); + beforeEach(angular.mock.module("templates")); - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ProfileImageBlock] }) - class BlockContainerComponent { - block = { type: 'Block' }; - owner = { name: 'profile-name' }; - constructor() { + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ProfileImageBlock] }) + class BlockContainerComponent { + block = { type: 'Block' }; + owner = { name: 'profile-name' }; + constructor() { + } } - } - it("render the profile image", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1); - done(); + it("render the profile image", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1); + done(); + }); }); - }); - it("render the settings link", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - expect(fixture.debugElement.queryAll(".settings-link").length).toEqual(1); - done(); + it("render the settings link", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + expect(fixture.debugElement.queryAll(".settings-link").length).toEqual(1); + done(); + }); }); - }); -}); + }); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts index f69545b..eadbead 100644 --- a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts +++ b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts @@ -7,45 +7,47 @@ const htmlTemplate: string = ' { - - beforeEach(angular.mock.module("templates")); - - let state = jasmine.createSpyObj("state", ["go"]); - let providers = [ - new Provider('$state', { useValue: state }), - new Provider('ArticleService', { - useValue: { - getByProfile: (profileId: number, filters: any): any => { - return Promise.resolve({ data: { articles: [{ name: "article1" }] } }); +describe("Components", () => { + describe("Recent Documents Block Component", () => { + + beforeEach(angular.mock.module("templates")); + + let state = jasmine.createSpyObj("state", ["go"]); + let providers = [ + new Provider('$state', { useValue: state }), + new Provider('ArticleService', { + useValue: { + getByProfile: (profileId: number, filters: any): any => { + return Promise.resolve({ data: { articles: [{ name: "article1" }] } }); + } } + }), + ].concat(provideFilters("truncateFilter", "stripTagsFilter")); + + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [RecentDocumentsBlock], providers: providers }) + class BlockContainerComponent { + block = { type: 'Block', settings: {} }; + owner = { name: 'profile-name' }; + constructor() { } - }), - ].concat(provideFilters("truncateFilter", "stripTagsFilter")); - - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [RecentDocumentsBlock], providers: providers }) - class BlockContainerComponent { - block = { type: 'Block', settings: {} }; - owner = { name: 'profile-name' }; - constructor() { } - } - it("get recent documents from the article service", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance; - expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); - done(); + it("get recent documents from the article service", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance; + expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); + done(); + }); }); - }); - it("go to article page when open a document", done => { - tcb.createAsync(BlockContainerComponent).then(fixture => { - let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance; - recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } }); - expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" }); - done(); + it("go to article page when open a document", done => { + tcb.createAsync(BlockContainerComponent).then(fixture => { + let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance; + recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } }); + expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" }); + done(); + }); }); - }); -}); + }); +}); \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts index 401f720..5ae64ce 100644 --- a/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts +++ b/src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts @@ -13,6 +13,8 @@ export class RecentDocumentsBlock { profile: any; documents: any; + + documentsLoaded: boolean = false; constructor(private ArticleService: ArticleService, private $state: any) { } @@ -25,6 +27,7 @@ export class RecentDocumentsBlock { //FIXME get all text articles this.ArticleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => { this.documents = response.data.articles; + this.documentsLoaded = true; }); } @@ -33,3 +36,4 @@ export class RecentDocumentsBlock { } } + diff --git a/src/spec/helpers.ts b/src/spec/helpers.ts index fc9eb91..898ded6 100644 --- a/src/spec/helpers.ts +++ b/src/spec/helpers.ts @@ -2,7 +2,7 @@ import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; import {quickFixture} from 'ng-forward/cjs/tests/utils'; import {Provider, Input, provide, Component} from 'ng-forward'; - +import {User, Person} from "./../app/models/interfaces"; export interface ComponentFixtureTemplate { @@ -29,10 +29,36 @@ export function createComponentFromClass(yourClass: ngClass) { return tcb.createAsync(yourClass); } +export function createProviderToValue(name: string, value: any) { + return new Provider(name, { useValue: value }); +} + +export function provideEmptyObjects(...providedNames: string[]) { + let providers: Provider[] = []; + for (let name of providedNames) { + providers.push(createProviderToValue(name, {})); + } + return providers; +} + export function provideFilters(...filters: string[]) { let providers: Provider[] = []; - for (var filter of filters) { + for (let filter of filters) { providers.push(new Provider(filter, { useValue: () => { } })); } return providers; } + +export var fixtures = { + user: { + id: 1, + login: 'user', + email: 'user@company.com', + person: { + id: 1, + identifier: 'user' + }, + private_token: 'token', + userRole: 'admin' + } +}; \ No newline at end of file -- libgit2 0.21.2