Commit e83eb73f5b8b3b171be4633dabc319d142abb33a

Authored by ABNER SILVA DE OLIVEIRA
1 parent 232ead7e
Exists in master and in 1 other branch dev-fixes

grouping the specs in groups [Services, Components, ...]

src/app/components/auth/auth_controller.spec.ts
1 1 import {AuthController} from "./auth_controller";
2 2 import {AuthService} from "./auth_service";
3 3  
4   -describe("AuthController", () => {
  4 +describe("Controllers", () => {
5 5  
6   - it("calls authenticate on AuthService when login called", () => {
7 6  
8   - // creating a Mock AuthService
9   - let AuthServiceMock: AuthService = jasmine.createSpyObj("AuthService", ["login"]);
  7 + describe("AuthController", () => {
10 8  
11   - // pass AuthServiceMock into the constructor
12   - let authController = new AuthController(null, null, null, AuthServiceMock);
  9 + it("calls authenticate on AuthService when login called", () => {
13 10  
14   - // setup of authController -> set the credentials instance property
15   - let credentials = { username: "username", password: "password" };
  11 + // creating a Mock AuthService
  12 + let AuthServiceMock: AuthService = jasmine.createSpyObj("AuthService", ["login"]);
16 13  
17   - authController.credentials = credentials;
  14 + // pass AuthServiceMock into the constructor
  15 + let authController = new AuthController(null, null, null, AuthServiceMock);
18 16  
19   - // calls the authController login method
20   - authController.login();
  17 + // setup of authController -> set the credentials instance property
  18 + let credentials = { username: "username", password: "password" };
21 19  
22   - // checks if the method login of the injected AuthService has been called
23   - expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials);
  20 + authController.credentials = credentials;
24 21  
25   - });
26   -
  22 + // calls the authController login method
  23 + authController.login();
  24 +
  25 + // checks if the method login of the injected AuthService has been called
  26 + expect(AuthServiceMock.login).toHaveBeenCalledWith(credentials);
  27 +
  28 + });
27 29  
28   -});
  30 +
  31 +
  32 + });
  33 +});
29 34 \ No newline at end of file
... ...
src/app/components/auth/auth_service.spec.ts
... ... @@ -3,75 +3,78 @@
3 3 import {AuthService, AUTH_EVENTS} from "./";
4 4 import {User, Credentials} from "./../../models/interfaces";
5 5  
6   -describe("Auth Service", () => {
  6 +describe("Services", () => {
7 7  
8   - let $httpBackend: ng.IHttpBackendService;
9   - let authService: AuthService;
10   - let credentials: Credentials;
11   - let $rootScope: ng.IRootScopeService;
12   - let user: User;
13 8  
14   - beforeEach(angular.mock.module("noosferoApp"));
  9 + describe("Auth Service", () => {
15 10  
16   - beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => {
17   - $httpBackend = _$httpBackend_;
18   - authService = _AuthService_;
19   - $rootScope = _$rootScope_;
  11 + let $httpBackend: ng.IHttpBackendService;
  12 + let authService: AuthService;
  13 + let credentials: Credentials;
  14 + let $rootScope: ng.IRootScopeService;
  15 + let user: User;
20 16  
21   - user = <User>{
22   - id: 1,
23   - login: "user"
24   - };
25   - }));
  17 + beforeEach(angular.mock.module("noosferoApp"));
26 18  
  19 + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => {
  20 + $httpBackend = _$httpBackend_;
  21 + authService = _AuthService_;
  22 + $rootScope = _$rootScope_;
27 23  
28   - describe("Succesffull login", () => {
  24 + user = <User>{
  25 + id: 1,
  26 + login: "user"
  27 + };
  28 + }));
29 29  
30   - beforeEach(() => {
31   - credentials = { username: "user", password: "password" };
32 30  
33   - $httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user });
34   - });
  31 + describe("Succesffull login", () => {
  32 +
  33 + beforeEach(() => {
  34 + credentials = { username: "user", password: "password" };
35 35  
36   - it("should return loggedUser", (done) => {
37   - authService.login(credentials).then((loggedUser) => {
38   - expect(loggedUser).toBeDefined();
39   - done();
  36 + $httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user });
40 37 });
41   - $httpBackend.flush();
42   - expect($httpBackend.verifyNoOutstandingRequest());
43   - });
44 38  
  39 + it("should return loggedUser", (done) => {
  40 + authService.login(credentials).then((loggedUser) => {
  41 + expect(loggedUser).toBeDefined();
  42 + done();
  43 + });
  44 + $httpBackend.flush();
  45 + expect($httpBackend.verifyNoOutstandingRequest());
  46 + });
  47 +
  48 +
  49 + it("should emit event loggin successful with user logged data", () => {
  50 +
  51 + authService.login(credentials);
45 52  
46   - it("should emit event loggin successful with user logged data", () => {
  53 + let eventEmmited: boolean = false;
  54 + $rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => {
  55 + eventEmmited = true;
  56 + expect(userThroughEvent).toEqual(user)
  57 + });
47 58  
48   - authService.login(credentials);
  59 + $httpBackend.flush();
49 60  
50   - let eventEmmited: boolean = false;
51   - $rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => {
52   - eventEmmited = true;
53   - expect(userThroughEvent).toEqual(user)
  61 + expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!");
54 62 });
55 63  
56   - $httpBackend.flush();
  64 + it("should return the current logged in user", () => {
  65 + authService.login(credentials);
  66 + $httpBackend.flush();
  67 + let actual: User = authService.currentUser();
  68 + expect(actual).toEqual(user, "The returned user must be present");
  69 + });
57 70  
58   - expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!");
59   - });
60   -
61   - it("should return the current logged in user", () => {
62   - authService.login(credentials);
63   - $httpBackend.flush();
64   - let actual: User = authService.currentUser();
65   - expect(actual).toEqual(user, "The returned user must be present");
  71 + it("should not return the current user after logout", () => {
  72 + authService.logout();
  73 + let actual: any = authService.currentUser();
  74 + expect(actual).toEqual(undefined, "The returned user must not be defined");
  75 + });
66 76 });
67 77  
68   - it("should not return the current user after logout", () => {
69   - authService.logout();
70   - let actual: any = authService.currentUser();
71   - expect(actual).toEqual(undefined, "The returned user must not be defined");
72   - });
73   - });
74   -
75 78  
76   -});
77   -
78 79 \ No newline at end of file
  80 + });
  81 +});
79 82 \ No newline at end of file
... ...
src/app/components/auth/auth_service.ts
... ... @@ -55,7 +55,7 @@ export class AuthService {
55 55 public isAuthenticated() {
56 56 return !!this.session.currentUser();
57 57 }
58   -
  58 +
59 59 public currentUser(): User {
60 60 return this.session.currentUser();
61 61 }
... ...
src/app/components/auth/session_spec.ts 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +import {Component} from "ng-forward";
  2 +import {Session} from "./";
  3 +import {fixtures, createComponentFromClass, createProviderToValue} from "./../../../spec/helpers";
  4 +import {UserResponse, User, INoosferoLocalStorage} from "./../../models/interfaces";
  5 +
  6 +
  7 +describe("Services", () => {
  8 +
  9 +
  10 + describe("Session Service", () => {
  11 +
  12 + let $localStorage: INoosferoLocalStorage = null;
  13 + let $log: any;
  14 +
  15 + beforeEach(() => {
  16 + $localStorage = <INoosferoLocalStorage>{ currentUser: null };
  17 + $log = jasmine.createSpyObj('$log', ['debug']);
  18 + });
  19 +
  20 + it("method 'create()' saves the current user on $localstorage service", () => {
  21 + let session = new Session($localStorage, $log);
  22 + let userResponse = <UserResponse>{
  23 + user: fixtures.user
  24 + };
  25 + session.create(userResponse);
  26 + expect($localStorage.currentUser).toEqual(userResponse.user);
  27 + });
  28 +
  29 + it("method 'destroy()' clean the currentUser on $localstorage", () => {
  30 + let session = new Session($localStorage, $log);
  31 + let userResponse = <UserResponse>{
  32 + user: fixtures.user
  33 + };
  34 + $localStorage.currentUser = fixtures.user;
  35 + session.destroy();
  36 + expect($localStorage.currentUser).toBeUndefined();
  37 + });
  38 +
  39 + it("method 'currentUser()' returns the user recorded on $localstorage service", () => {
  40 + let session = new Session($localStorage, $log);
  41 + let userResponse = <UserResponse>{
  42 + user: fixtures.user
  43 + };
  44 + $localStorage.currentUser = fixtures.user;
  45 + expect(session.currentUser()).toEqual($localStorage.currentUser);
  46 + });
  47 + });
  48 +
  49 +});
0 50 \ No newline at end of file
... ...
src/app/components/navbar/navbar.spec.ts
1   -import {createComponentFromClass, quickCreateComponent} from "./../../../spec/helpers";
2   -import {Navbar} from "./";
3   -import {AUTH_EVENTS} from "./../auth";
4   -import {User} from "./../../models/interfaces";
5   -import {Injectable, Provider, provide} from "ng-forward";
  1 +import {
  2 +createComponentFromClass,
  3 +quickCreateComponent,
  4 +provideEmptyObjects
  5 +} from "./../../../spec/helpers";
  6 +import {
  7 +Navbar
  8 +} from "./";
  9 +import {
  10 +AUTH_EVENTS
  11 +} from "./../auth";
  12 +import {
  13 +User
  14 +} from "./../../models/interfaces";
  15 +import {
  16 +Injectable,
  17 +Provider,
  18 +provide
  19 +} from "ng-forward";
6 20  
7 21  
  22 +describe("Components", () => {
8 23  
9   -describe("Navbar Component", () => {
10 24  
11   - let $rootScope: ng.IRootScopeService;
12   - let user = <User>{
13   - id: 1,
14   - login: "user"
15   - };
  25 + describe("Navbar Component", () => {
16 26  
17   - beforeEach(angular.mock.module("templates"));
  27 + let $rootScope: ng.IRootScopeService;
  28 + let user = <User>{
  29 + id: 1,
  30 + login: "user"
  31 + };
18 32  
19   - // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => {
20   - // $rootScope = _$rootScope_;
21   - // }));
  33 + beforeEach(angular.mock.module("templates"));
22 34  
23   - it('should get the loggedIn user', (done: Function) => {
  35 + // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => {
  36 + // $rootScope = _$rootScope_;
  37 + // }));
24 38  
25   - let scope = jasmine.createSpyObj("scope", ["$on"]);
  39 + it('should get the loggedIn user', (done: Function) => {
26 40  
27   - let providers = [
  41 + let scope = jasmine.createSpyObj("scope", ["$on"]);
28 42  
  43 + let providers = [
  44 + provideEmptyObjects('moment', '$modal', 'AuthService', '$state'),
  45 + new Provider('Session', {
  46 + useValue: {
  47 + currentUser: () => {
  48 + return user;
  49 + }
  50 + }
  51 + }),
  52 + new Provider('$scope', {
  53 + useValue: scope
  54 + }),
  55 + new Provider('AUTH_EVENTS', {
  56 + useValue: {
  57 + AUTH_EVENTS
  58 + }
  59 + })
  60 + ];
29 61  
30   - new Provider('moment', { useValue: {} }),
31   - new Provider('$modal', { useValue: {} }),
32   - new Provider('AuthService', { useValue: {} }),
33   - new Provider('Session', { useValue: {
34   - currentUser: () => { return user; }
35   - } }),
36   - new Provider('$scope', { useValue: scope }),
37   - new Provider('$state', { useValue: {} }),
38   - new Provider('AUTH_EVENTS', { useValue: {AUTH_EVENTS} })
39   - ];
40 62  
41   -
42   - quickCreateComponent({
43   - providers: providers,
44   - template: "<acme-navbar></acme-navbar>",
45   - directives: [Navbar]
46   - }).then(fixture => {
47   - let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance;
48   - expect(navbarInstance).toBeDefined();
49   - expect(navbarInstance["currentUser"]).toEqual(user)
50   - done();
  63 + quickCreateComponent({
  64 + providers: providers,
  65 + template: "<acme-navbar></acme-navbar>",
  66 + directives: [Navbar]
  67 + }).then(fixture => {
  68 + let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance;
  69 + expect(navbarInstance).toBeDefined();
  70 + expect(navbarInstance["currentUser"]).toEqual(user)
  71 + done();
  72 + });
51 73 });
52   - });
53 74  
  75 + });
54 76 });
55 77 \ No newline at end of file
... ...
src/app/components/noosfero-articles/article/article_view.spec.ts
... ... @@ -8,98 +8,101 @@ import {createComponentFromClass, quickCreateComponent} from &quot;../../../../spec/h
8 8 const htmlTemplate: string = '<noosfero-article [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-article>';
9 9  
10 10  
11   -describe("ArticleView Component", () => {
12   -
13   - // the karma preprocessor html2js transform the templates html into js files which put
14   - // the templates to the templateCache into the module templates
15   - // we need to load the module templates here as the template for the
16   - // component Noosfero ArtileView will be load on our tests
17   - beforeEach(angular.mock.module("templates"));
18   -
19   - it("renders the default component when no specific component is found", (done: Function) => {
20   - // Creating a container component (ArticleContainerComponent) to include
21   - // the component under test (ArticleView)
22   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
23   - class ArticleContainerComponent {
24   - article = { type: 'anyArticleType' };
25   - profile = { name: 'profile-name' };
26   - constructor() {
  11 +describe("Components", () => {
  12 +
  13 + describe("ArticleView Component", () => {
  14 +
  15 + // the karma preprocessor html2js transform the templates html into js files which put
  16 + // the templates to the templateCache into the module templates
  17 + // we need to load the module templates here as the template for the
  18 + // component Noosfero ArtileView will be load on our tests
  19 + beforeEach(angular.mock.module("templates"));
  20 +
  21 + it("renders the default component when no specific component is found", (done: Function) => {
  22 + // Creating a container component (ArticleContainerComponent) to include
  23 + // the component under test (ArticleView)
  24 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
  25 + class ArticleContainerComponent {
  26 + article = { type: 'anyArticleType' };
  27 + profile = { name: 'profile-name' };
  28 + constructor() {
  29 + }
27 30 }
28   - }
29 31  
30   - createComponentFromClass(ArticleContainerComponent).then((fixture) => {
31   - // and here we can inspect and run the test assertions
  32 + createComponentFromClass(ArticleContainerComponent).then((fixture) => {
  33 + // and here we can inspect and run the test assertions
32 34  
33   - // gets the children component of ArticleContainerComponent
34   - let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
  35 + // gets the children component of ArticleContainerComponent
  36 + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
35 37  
36   - // and checks if the article View rendered was the Default Article View
37   - expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype);
  38 + // and checks if the article View rendered was the Default Article View
  39 + expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype);
38 40  
39   - // done needs to be called (it isn't really needed, as we can read in
40   - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
41   - // because createAsync in ng-forward is not really async, but as the intention
42   - // here is write tests in angular 2 ways, this is recommended
43   - done();
44   - });
  41 + // done needs to be called (it isn't really needed, as we can read in
  42 + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
  43 + // because createAsync in ng-forward is not really async, but as the intention
  44 + // here is write tests in angular 2 ways, this is recommended
  45 + done();
  46 + });
45 47  
46   - });
  48 + });
47 49  
48   - it("receives the article and profile as inputs", (done: Function) => {
  50 + it("receives the article and profile as inputs", (done: Function) => {
49 51  
50   - // Creating a container component (ArticleContainerComponent) to include
51   - // the component under test (ArticleView)
52   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
53   - class ArticleContainerComponent {
54   - article = { type: 'anyArticleType' };
55   - profile = { name: 'profile-name' };
56   - constructor() {
  52 + // Creating a container component (ArticleContainerComponent) to include
  53 + // the component under test (ArticleView)
  54 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
  55 + class ArticleContainerComponent {
  56 + article = { type: 'anyArticleType' };
  57 + profile = { name: 'profile-name' };
  58 + constructor() {
  59 + }
57 60 }
58   - }
59   -
60   - // uses the TestComponentBuilder instance to initialize the component
61   - createComponentFromClass(ArticleContainerComponent).then((fixture) => {
62   - // and here we can inspect and run the test assertions
63   - let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
64   -
65   - // assure the article object inside the ArticleView matches
66   - // the provided through the parent component
67   - expect(articleView.article.type).toEqual("anyArticleType");
68   - expect(articleView.profile.name).toEqual("profile-name");
69   -
70   - // done needs to be called (it isn't really needed, as we can read in
71   - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
72   - // because createAsync in ng-forward is not really async, but as the intention
73   - // here is write tests in angular 2 ways, this is recommended
74   - done();
  61 +
  62 + // uses the TestComponentBuilder instance to initialize the component
  63 + createComponentFromClass(ArticleContainerComponent).then((fixture) => {
  64 + // and here we can inspect and run the test assertions
  65 + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
  66 +
  67 + // assure the article object inside the ArticleView matches
  68 + // the provided through the parent component
  69 + expect(articleView.article.type).toEqual("anyArticleType");
  70 + expect(articleView.profile.name).toEqual("profile-name");
  71 +
  72 + // done needs to be called (it isn't really needed, as we can read in
  73 + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
  74 + // because createAsync in ng-forward is not really async, but as the intention
  75 + // here is write tests in angular 2 ways, this is recommended
  76 + done();
  77 + });
75 78 });
76   - });
77 79  
78 80  
79   - it("renders a article view which matches to the article type", done => {
80   - // NoosferoTinyMceArticle component created to check if it will be used
81   - // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleView)
82   - // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
83   - @Component({ selector: 'noosfero-tiny-mce-article', template: "<h1>TinyMceArticle</h1>" })
84   - class TinyMceArticleView {
85   - @Input() article: any;
86   - @Input() profile: any;
87   - }
88   -
89   - // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle
90   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] })
91   - class CustomArticleType {
92   - article = { type: 'TinyMceArticle' };
93   - profile = { name: 'profile-name' };
94   - constructor() {
  81 + it("renders a article view which matches to the article type", done => {
  82 + // NoosferoTinyMceArticle component created to check if it will be used
  83 + // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleView)
  84 + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
  85 + @Component({ selector: 'noosfero-tiny-mce-article', template: "<h1>TinyMceArticle</h1>" })
  86 + class TinyMceArticleView {
  87 + @Input() article: any;
  88 + @Input() profile: any;
95 89 }
96   - }
97   - createComponentFromClass(CustomArticleType).then(fixture => {
98   - let myComponent: CustomArticleType = fixture.componentInstance;
99   - expect(myComponent.article.type).toEqual("TinyMceArticle");
100   - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle");
101   - done();
  90 +
  91 + // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle
  92 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] })
  93 + class CustomArticleType {
  94 + article = { type: 'TinyMceArticle' };
  95 + profile = { name: 'profile-name' };
  96 + constructor() {
  97 + }
  98 + }
  99 + createComponentFromClass(CustomArticleType).then(fixture => {
  100 + let myComponent: CustomArticleType = fixture.componentInstance;
  101 + expect(myComponent.article.type).toEqual("TinyMceArticle");
  102 + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle");
  103 + done();
  104 + });
102 105 });
103   - });
104 106  
  107 + });
105 108 });
106 109 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/block.component.spec.ts
... ... @@ -7,84 +7,85 @@ const tcb = new TestComponentBuilder();
7 7  
8 8 const htmlTemplate: string = '<noosfero-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-block>';
9 9  
10   -
11   -describe("Block Component", () => {
12   -
13   - // the karma preprocessor html2js transform the templates html into js files which put
14   - // the templates to the templateCache into the module templates
15   - // we need to load the module templates here as the template for the
16   - // component Block will be load on our tests
17   - beforeEach(angular.mock.module("templates"));
18   -
19   - it("receives the block and the owner as inputs", done => {
20   -
21   - // Creating a container component (BlockContainerComponent) to include
22   - // the component under test (Block)
23   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] })
24   - class BlockContainerComponent {
25   - block = { type: 'Block' };
26   - owner = { name: 'profile-name' };
27   - constructor() {
  10 +describe("Components", () => {
  11 + describe("Block Component", () => {
  12 +
  13 + // the karma preprocessor html2js transform the templates html into js files which put
  14 + // the templates to the templateCache into the module templates
  15 + // we need to load the module templates here as the template for the
  16 + // component Block will be load on our tests
  17 + beforeEach(angular.mock.module("templates"));
  18 +
  19 + it("receives the block and the owner as inputs", done => {
  20 +
  21 + // Creating a container component (BlockContainerComponent) to include
  22 + // the component under test (Block)
  23 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] })
  24 + class BlockContainerComponent {
  25 + block = { type: 'Block' };
  26 + owner = { name: 'profile-name' };
  27 + constructor() {
  28 + }
28 29 }
29   - }
30   -
31   - // uses the TestComponentBuilder instance to initialize the component
32   - tcb
33   - .createAsync(BlockContainerComponent).then(fixture => {
34   - // and here we can inspect and run the test assertions
35   - let myComponent: Block = fixture.componentInstance;
36   -
37   - // assure the block object inside the Block matches
38   - // the provided through the parent component
39   - expect(myComponent.block.type).toEqual("Block");
40   - expect(myComponent.owner.name).toEqual("profile-name");
41   - done();
42   - });
43   - });
44 30  
45   -
46   - it("renders a component which matches to the block type", done => {
47   - // CustomBlock component created to check if it will be used
48   - // when a block with type 'CustomBlock' is provided to the noosfero-block (Block)
49   - // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
50   - @Component({ selector: 'noosfero-custom-block', template: "<h1>My Custom Block</h1>" })
51   - class CustomBlock {
52   - @Input() block: any;
53   - @Input() owner: any;
54   - }
55   -
56   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block, CustomBlock] })
57   - class CustomBlockType {
58   - block = { type: 'CustomBlock' };
59   - owner = { name: 'profile-name' };
60   - constructor() {
  31 + // uses the TestComponentBuilder instance to initialize the component
  32 + tcb
  33 + .createAsync(BlockContainerComponent).then(fixture => {
  34 + // and here we can inspect and run the test assertions
  35 + let myComponent: Block = fixture.componentInstance;
  36 +
  37 + // assure the block object inside the Block matches
  38 + // the provided through the parent component
  39 + expect(myComponent.block.type).toEqual("Block");
  40 + expect(myComponent.owner.name).toEqual("profile-name");
  41 + done();
  42 + });
  43 + });
  44 +
  45 +
  46 + it("renders a component which matches to the block type", done => {
  47 + // CustomBlock component created to check if it will be used
  48 + // when a block with type 'CustomBlock' is provided to the noosfero-block (Block)
  49 + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
  50 + @Component({ selector: 'noosfero-custom-block', template: "<h1>My Custom Block</h1>" })
  51 + class CustomBlock {
  52 + @Input() block: any;
  53 + @Input() owner: any;
61 54 }
62   - }
63   - tcb
64   - .createAsync(CustomBlockType).then(fixture => {
65   - let myComponent: CustomBlockType = fixture.componentInstance;
66   - expect(myComponent.block.type).toEqual("CustomBlock");
67   - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("My Custom Block");
68   - done();
69   - });
70   - });
71   -
72 55  
73   - it("renders the default block when hasn't defined a block type", done => {
74   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] })
75   - class CustomBlockType {
76   - block: any = { type: null };
77   - owner: any = { name: 'profile-name' };
78   - constructor() {
  56 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block, CustomBlock] })
  57 + class CustomBlockType {
  58 + block = { type: 'CustomBlock' };
  59 + owner = { name: 'profile-name' };
  60 + constructor() {
  61 + }
79 62 }
80   - }
81   - tcb
82   - .createAsync(CustomBlockType).then(fixture => {
83   - let myComponent: CustomBlockType = fixture.componentInstance;
84   - expect(myComponent.block.type).toBeNull();
85   - expect(!!fixture.debugElement.nativeElement.querySelector("noosfero-default-block")).toBeTruthy();
86   - done();
87   - });
88   - });
  63 + tcb
  64 + .createAsync(CustomBlockType).then(fixture => {
  65 + let myComponent: CustomBlockType = fixture.componentInstance;
  66 + expect(myComponent.block.type).toEqual("CustomBlock");
  67 + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("My Custom Block");
  68 + done();
  69 + });
  70 + });
  71 +
  72 +
  73 + it("renders the default block when hasn't defined a block type", done => {
  74 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] })
  75 + class CustomBlockType {
  76 + block: any = { type: null };
  77 + owner: any = { name: 'profile-name' };
  78 + constructor() {
  79 + }
  80 + }
  81 + tcb
  82 + .createAsync(CustomBlockType).then(fixture => {
  83 + let myComponent: CustomBlockType = fixture.componentInstance;
  84 + expect(myComponent.block.type).toBeNull();
  85 + expect(!!fixture.debugElement.nativeElement.querySelector("noosfero-default-block")).toBeTruthy();
  86 + done();
  87 + });
  88 + });
89 89  
90   -});
  90 + });
  91 +});
91 92 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/link-list/link-list.component.spec.ts
... ... @@ -9,53 +9,57 @@ const tcb = new TestComponentBuilder();
9 9 const htmlTemplate: string = '<noosfero-link-list-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-link-list-block>';
10 10  
11 11  
12   -describe("Link List Block Component", () => {
  12 +describe("Components", () => {
13 13  
14   - beforeEach(angular.mock.module("templates"));
  14 + describe("Link List Block Component", () => {
15 15  
16   - it("receives the block and the owner as inputs", done => {
  16 + beforeEach(angular.mock.module("templates"));
17 17  
18   - // Creating a container component (BlockContainerComponent) to include
19   - // the component under test (Block)
20   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] })
21   - class BlockContainerComponent {
22   - block = { type: 'Block' };
23   - owner = { name: 'profile-name' };
24   - constructor() {
  18 + it("receives the block and the owner as inputs", done => {
  19 +
  20 + // Creating a container component (BlockContainerComponent) to include
  21 + // the component under test (Block)
  22 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [LinkListBlock] })
  23 + class BlockContainerComponent {
  24 + block = { type: 'Block' };
  25 + owner = { name: 'profile-name' };
  26 + constructor() {
  27 + }
25 28 }
26   - }
27   -
28   - // uses the TestComponentBuilder instance to initialize the component
29   - //.overrideView(LinkListBlock, { template: 'asdasdasd', pipes: [NoosferoTemplate] })
30   - tcb.createAsync(BlockContainerComponent).then(fixture => {
31   - // and here we can inspect and run the test assertions
32   - let myComponent: LinkListBlock = fixture.componentInstance;
33   -
34   - // assure the block object inside the Block matches
35   - // the provided through the parent component
36   - expect(myComponent.block.type).toEqual("Block");
37   - expect(myComponent.owner.name).toEqual("profile-name");
38   - done();
  29 +
  30 + // uses the TestComponentBuilder instance to initialize the component
  31 + //.overrideView(LinkListBlock, { template: 'asdasdasd', pipes: [NoosferoTemplate] })
  32 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  33 + // and here we can inspect and run the test assertions
  34 + let myComponent: LinkListBlock = fixture.componentInstance;
  35 +
  36 + // assure the block object inside the Block matches
  37 + // the provided through the parent component
  38 + expect(myComponent.block.type).toEqual("Block");
  39 + expect(myComponent.owner.name).toEqual("profile-name");
  40 + done();
  41 + });
39 42 });
40   - });
41 43  
42 44  
43   - it("display links stored in block settings", done => {
44   -
45   - @Component({
46   - selector: 'test-container-component',
47   - template: htmlTemplate,
48   - directives: [LinkListBlock],
49   - providers: provideFilters("noosferoTemplateFilter")
50   - })
51   - class CustomBlockType {
52   - block: any = { settings: { links: [{ name: 'link1', address: 'address1' }, { name: 'link2', address: 'address2' }] } };
53   - owner: any = { name: 'profile-name' };
54   - }
55   - tcb.createAsync(CustomBlockType).then(fixture => {
56   - expect(fixture.debugElement.queryAll(".link-list-block a").length).toEqual(2);
57   - done();
  45 + it("display links stored in block settings", done => {
  46 +
  47 + @Component({
  48 + selector: 'test-container-component',
  49 + template: htmlTemplate,
  50 + directives: [LinkListBlock],
  51 + providers: provideFilters("noosferoTemplateFilter")
  52 + })
  53 + class CustomBlockType {
  54 + block: any = { settings: { links: [{ name: 'link1', address: 'address1' }, { name: 'link2', address: 'address2' }] } };
  55 + owner: any = { name: 'profile-name' };
  56 + }
  57 + tcb.createAsync(CustomBlockType).then(fixture => {
  58 + expect(fixture.debugElement.queryAll(".link-list-block a").length).toEqual(2);
  59 + done();
  60 + });
58 61 });
  62 +
59 63 });
60 64  
61 65 -});
  66 +});
62 67 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/main-block/main-block.component.spec.ts
... ... @@ -8,33 +8,34 @@ const tcb = new TestComponentBuilder();
8 8  
9 9 const htmlTemplate: string = '<noosfero-main-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-main-block>';
10 10  
11   -
12   -describe("Main Block Component", () => {
13   -
14   - // the karma preprocessor html2js transform the templates html into js files which put
15   - // the templates to the templateCache into the module templates
16   - // we need to load the module templates here as the template for the
17   - // component Block will be load on our tests
18   - beforeEach(angular.mock.module("templates"));
19   -
20   - it("check if the main block has a tag with ui-view attribute", done => {
21   -
22   - // Creating a container component (BlockContainerComponent) to include
23   - // the component under test (Block)
24   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MainBlock] })
25   - class BlockContainerComponent {
26   - }
27   -
28   - // uses the TestComponentBuilder instance to initialize the component
29   - tcb.createAsync(BlockContainerComponent).then(fixture => {
30   - // and here we can inspect and run the test assertions
31   - //let myComponent: MainBlock = fixture.componentInstance;
32   -
33   - // assure the block object inside the Block matches
34   - // the provided through the parent component
35   - expect(fixture.debugElement.queryAll('[ui-view="mainBlockContent"]').length).toEqual(1)
36   - done();
  11 +describe("Components", () => {
  12 + describe("Main Block Component", () => {
  13 +
  14 + // the karma preprocessor html2js transform the templates html into js files which put
  15 + // the templates to the templateCache into the module templates
  16 + // we need to load the module templates here as the template for the
  17 + // component Block will be load on our tests
  18 + beforeEach(angular.mock.module("templates"));
  19 +
  20 + it("check if the main block has a tag with ui-view attribute", done => {
  21 +
  22 + // Creating a container component (BlockContainerComponent) to include
  23 + // the component under test (Block)
  24 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MainBlock] })
  25 + class BlockContainerComponent {
  26 + }
  27 +
  28 + // uses the TestComponentBuilder instance to initialize the component
  29 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  30 + // and here we can inspect and run the test assertions
  31 + //let myComponent: MainBlock = fixture.componentInstance;
  32 +
  33 + // assure the block object inside the Block matches
  34 + // the provided through the parent component
  35 + expect(fixture.debugElement.queryAll('[ui-view="mainBlockContent"]').length).toEqual(1)
  36 + done();
  37 + });
37 38 });
38   - });
39 39  
40   -});
  40 + });
  41 +});
41 42 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/members-block/members-block.component.spec.ts
... ... @@ -7,45 +7,47 @@ const htmlTemplate: string = &#39;&lt;noosfero-members-block [block]=&quot;ctrl.block&quot; [owne
7 7  
8 8 const tcb = new TestComponentBuilder();
9 9  
10   -describe("Members Block Component", () => {
11   -
12   - beforeEach(angular.mock.module("templates"));
13   -
14   - let state = jasmine.createSpyObj("state", ["go"]);
15   - let providers = [
16   - new Provider('truncateFilter', { useValue: () => { } }),
17   - new Provider('stripTagsFilter', { useValue: () => { } }),
18   - new Provider('$state', { useValue: state }),
19   - new Provider('ProfileService', {
20   - useValue: {
21   - getProfileMembers: (profileId: number, filters: any): any => {
22   - return Promise.resolve({ data: { people: [{ identifier: "person1" }] } });
  10 +describe("Components", () => {
  11 + describe("Members Block Component", () => {
  12 +
  13 + beforeEach(angular.mock.module("templates"));
  14 +
  15 + let state = jasmine.createSpyObj("state", ["go"]);
  16 + let providers = [
  17 + new Provider('truncateFilter', { useValue: () => { } }),
  18 + new Provider('stripTagsFilter', { useValue: () => { } }),
  19 + new Provider('$state', { useValue: state }),
  20 + new Provider('ProfileService', {
  21 + useValue: {
  22 + getProfileMembers: (profileId: number, filters: any): any => {
  23 + return Promise.resolve({ data: { people: [{ identifier: "person1" }] } });
  24 + }
23 25 }
  26 + }),
  27 + ];
  28 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MembersBlock], providers: providers })
  29 + class BlockContainerComponent {
  30 + block = { type: 'Block', settings: {} };
  31 + owner = { name: 'profile-name' };
  32 + constructor() {
24 33 }
25   - }),
26   - ];
27   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [MembersBlock], providers: providers })
28   - class BlockContainerComponent {
29   - block = { type: 'Block', settings: {} };
30   - owner = { name: 'profile-name' };
31   - constructor() {
32 34 }
33   - }
34 35  
35   - it("get members of the block owner", done => {
36   - tcb.createAsync(BlockContainerComponent).then(fixture => {
37   - let block: MembersBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
38   - expect(block.members).toEqual([{ identifier: "person1" }]);
39   - done();
  36 + it("get members of the block owner", done => {
  37 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  38 + let block: MembersBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
  39 + expect(block.members).toEqual([{ identifier: "person1" }]);
  40 + done();
  41 + });
40 42 });
41   - });
42 43  
43   - it("render the profile image for each member", done => {
44   - tcb.createAsync(BlockContainerComponent).then(fixture => {
45   - fixture.debugElement.getLocal("$rootScope").$apply();
46   - expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1);
47   - done();
  44 + it("render the profile image for each member", done => {
  45 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  46 + fixture.debugElement.getLocal("$rootScope").$apply();
  47 + expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1);
  48 + done();
  49 + });
48 50 });
49   - });
50 51  
51   -});
  52 + });
  53 +});
52 54 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts
... ... @@ -8,30 +8,32 @@ const tcb = new TestComponentBuilder();
8 8 const htmlTemplate: string = '<noosfero-profile-image-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-profile-image-block>';
9 9  
10 10  
11   -describe("Profile Image Block Component", () => {
  11 +describe("Components", () => {
  12 + describe("Profile Image Block Component", () => {
12 13  
13   - beforeEach(angular.mock.module("templates"));
  14 + beforeEach(angular.mock.module("templates"));
14 15  
15   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ProfileImageBlock] })
16   - class BlockContainerComponent {
17   - block = { type: 'Block' };
18   - owner = { name: 'profile-name' };
19   - constructor() {
  16 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ProfileImageBlock] })
  17 + class BlockContainerComponent {
  18 + block = { type: 'Block' };
  19 + owner = { name: 'profile-name' };
  20 + constructor() {
  21 + }
20 22 }
21   - }
22 23  
23   - it("render the profile image", done => {
24   - tcb.createAsync(BlockContainerComponent).then(fixture => {
25   - expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1);
26   - done();
  24 + it("render the profile image", done => {
  25 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  26 + expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1);
  27 + done();
  28 + });
27 29 });
28   - });
29 30  
30   - it("render the settings link", done => {
31   - tcb.createAsync(BlockContainerComponent).then(fixture => {
32   - expect(fixture.debugElement.queryAll(".settings-link").length).toEqual(1);
33   - done();
  31 + it("render the settings link", done => {
  32 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  33 + expect(fixture.debugElement.queryAll(".settings-link").length).toEqual(1);
  34 + done();
  35 + });
34 36 });
35   - });
36 37  
37   -});
  38 + });
  39 +});
38 40 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/recent-documents/recent-documents.component.spec.ts
... ... @@ -7,45 +7,47 @@ const htmlTemplate: string = &#39;&lt;noosfero-recent-documents-block [block]=&quot;ctrl.blo
7 7  
8 8 const tcb = new TestComponentBuilder();
9 9  
10   -describe("Recent Documents Block Component", () => {
11   -
12   - beforeEach(angular.mock.module("templates"));
13   -
14   - let state = jasmine.createSpyObj("state", ["go"]);
15   - let providers = [
16   - new Provider('$state', { useValue: state }),
17   - new Provider('ArticleService', {
18   - useValue: {
19   - getByProfile: (profileId: number, filters: any): any => {
20   - return Promise.resolve({ data: { articles: [{ name: "article1" }] } });
  10 +describe("Components", () => {
  11 + describe("Recent Documents Block Component", () => {
  12 +
  13 + beforeEach(angular.mock.module("templates"));
  14 +
  15 + let state = jasmine.createSpyObj("state", ["go"]);
  16 + let providers = [
  17 + new Provider('$state', { useValue: state }),
  18 + new Provider('ArticleService', {
  19 + useValue: {
  20 + getByProfile: (profileId: number, filters: any): any => {
  21 + return Promise.resolve({ data: { articles: [{ name: "article1" }] } });
  22 + }
21 23 }
  24 + }),
  25 + ].concat(provideFilters("truncateFilter", "stripTagsFilter"));
  26 +
  27 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [RecentDocumentsBlock], providers: providers })
  28 + class BlockContainerComponent {
  29 + block = { type: 'Block', settings: {} };
  30 + owner = { name: 'profile-name' };
  31 + constructor() {
22 32 }
23   - }),
24   - ].concat(provideFilters("truncateFilter", "stripTagsFilter"));
25   -
26   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [RecentDocumentsBlock], providers: providers })
27   - class BlockContainerComponent {
28   - block = { type: 'Block', settings: {} };
29   - owner = { name: 'profile-name' };
30   - constructor() {
31 33 }
32   - }
33 34  
34   - it("get recent documents from the article service", done => {
35   - tcb.createAsync(BlockContainerComponent).then(fixture => {
36   - let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
37   - expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]);
38   - done();
  35 + it("get recent documents from the article service", done => {
  36 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  37 + let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
  38 + expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]);
  39 + done();
  40 + });
39 41 });
40   - });
41 42  
42   - it("go to article page when open a document", done => {
43   - tcb.createAsync(BlockContainerComponent).then(fixture => {
44   - let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
45   - recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } });
46   - expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" });
47   - done();
  43 + it("go to article page when open a document", done => {
  44 + tcb.createAsync(BlockContainerComponent).then(fixture => {
  45 + let recentDocumentsBlock: RecentDocumentsBlock = fixture.debugElement.componentViewChildren[0].componentInstance;
  46 + recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } });
  47 + expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" });
  48 + done();
  49 + });
48 50 });
49   - });
50 51  
51   -});
  52 + });
  53 +});
52 54 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts
... ... @@ -13,6 +13,8 @@ export class RecentDocumentsBlock {
13 13  
14 14 profile: any;
15 15 documents: any;
  16 +
  17 + documentsLoaded: boolean = false;
16 18  
17 19 constructor(private ArticleService: ArticleService, private $state: any) {
18 20 }
... ... @@ -25,6 +27,7 @@ export class RecentDocumentsBlock {
25 27 //FIXME get all text articles
26 28 this.ArticleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => {
27 29 this.documents = response.data.articles;
  30 + this.documentsLoaded = true;
28 31 });
29 32 }
30 33  
... ... @@ -33,3 +36,4 @@ export class RecentDocumentsBlock {
33 36 }
34 37  
35 38 }
  39 +
... ...
src/spec/helpers.ts
... ... @@ -2,7 +2,7 @@
2 2 import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder';
3 3 import {quickFixture} from 'ng-forward/cjs/tests/utils';
4 4 import {Provider, Input, provide, Component} from 'ng-forward';
5   -
  5 +import {User, Person} from "./../app/models/interfaces";
6 6  
7 7  
8 8 export interface ComponentFixtureTemplate {
... ... @@ -29,10 +29,36 @@ export function createComponentFromClass(yourClass: ngClass) {
29 29 return tcb.createAsync(yourClass);
30 30 }
31 31  
  32 +export function createProviderToValue(name: string, value: any) {
  33 + return new Provider(name, { useValue: value });
  34 +}
  35 +
  36 +export function provideEmptyObjects(...providedNames: string[]) {
  37 + let providers: Provider[] = [];
  38 + for (let name of providedNames) {
  39 + providers.push(createProviderToValue(name, {}));
  40 + }
  41 + return providers;
  42 +}
  43 +
32 44 export function provideFilters(...filters: string[]) {
33 45 let providers: Provider[] = [];
34   - for (var filter of filters) {
  46 + for (let filter of filters) {
35 47 providers.push(new Provider(filter, { useValue: () => { } }));
36 48 }
37 49 return providers;
38 50 }
  51 +
  52 +export var fixtures = {
  53 + user: {
  54 + id: 1,
  55 + login: 'user',
  56 + email: 'user@company.com',
  57 + person: <Person>{
  58 + id: 1,
  59 + identifier: 'user'
  60 + },
  61 + private_token: 'token',
  62 + userRole: 'admin'
  63 + }
  64 +};
39 65 \ No newline at end of file
... ...