Commit f4c0320e49cdd2aed7abc9e3718da9f9811d11dd
Exists in
master
and in
34 other branches
merge with ngforward branch. fixed helpers
Showing
24 changed files
with
617 additions
and
187 deletions
Show diff stats
@@ -0,0 +1,54 @@ | @@ -0,0 +1,54 @@ | ||
1 | +import {quickCreateComponent} from "../../spec/helpers"; | ||
2 | +import {Cms} from "./cms.component"; | ||
3 | + | ||
4 | +describe("Components", () => { | ||
5 | + describe("Cms Component", () => { | ||
6 | + | ||
7 | + let $rootScope: ng.IRootScopeService; | ||
8 | + let $q: ng.IQService; | ||
9 | + let articleServiceMock: any; | ||
10 | + let profileServiceMock: any; | ||
11 | + let $state: any; | ||
12 | + let sweetAlert: any; | ||
13 | + | ||
14 | + beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { | ||
15 | + $rootScope = _$rootScope_; | ||
16 | + $q = _$q_; | ||
17 | + })); | ||
18 | + | ||
19 | + beforeEach(() => { | ||
20 | + $state = jasmine.createSpyObj("$state", ["transitionTo"]); | ||
21 | + sweetAlert = jasmine.createSpyObj("SweetAlert", ["swal"]); | ||
22 | + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getCurrentProfile"]); | ||
23 | + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["create"]); | ||
24 | + | ||
25 | + let getCurrentProfileResponse = $q.defer(); | ||
26 | + getCurrentProfileResponse.resolve({ id: 1 }); | ||
27 | + | ||
28 | + let articleCreate = $q.defer(); | ||
29 | + articleCreate.resolve({ data: { article: { path: "path", profile: { identifier: "profile" } } } }); | ||
30 | + | ||
31 | + profileServiceMock.getCurrentProfile = jasmine.createSpy("getCurrentProfile").and.returnValue(getCurrentProfileResponse.promise); | ||
32 | + articleServiceMock.create = jasmine.createSpy("create").and.returnValue(articleCreate.promise); | ||
33 | + }); | ||
34 | + | ||
35 | + it("create an article in the current profile when save", done => { | ||
36 | + let component: Cms = new Cms(articleServiceMock, profileServiceMock, $state, sweetAlert); | ||
37 | + component.save(); | ||
38 | + $rootScope.$apply(); | ||
39 | + expect(profileServiceMock.getCurrentProfile).toHaveBeenCalled(); | ||
40 | + expect(articleServiceMock.create).toHaveBeenCalledWith(1, component.article); | ||
41 | + done(); | ||
42 | + }); | ||
43 | + | ||
44 | + it("got to the new article page and display an alert when saving sucessfully", done => { | ||
45 | + let component: Cms = new Cms(articleServiceMock, profileServiceMock, $state, sweetAlert); | ||
46 | + component.save(); | ||
47 | + $rootScope.$apply(); | ||
48 | + expect($state.transitionTo).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); | ||
49 | + expect(sweetAlert.swal).toHaveBeenCalled(); | ||
50 | + done(); | ||
51 | + }); | ||
52 | + | ||
53 | + }); | ||
54 | +}); |
src/app/cms/cms.component.ts
1 | -import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | +import {StateConfig, Component, Inject, provide} from 'ng-forward'; |
2 | import {Profile} from "./../models/interfaces"; | 2 | import {Profile} from "./../models/interfaces"; |
3 | import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; | 3 | import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; |
4 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
4 | 5 | ||
5 | @Component({ | 6 | @Component({ |
6 | selector: 'cms', | 7 | selector: 'cms', |
7 | - templateUrl: "app/cms/cms.html" | 8 | + templateUrl: "app/cms/cms.html", |
9 | + providers: [ | ||
10 | + provide('articleService', { useClass: ArticleService }), | ||
11 | + provide('profileService', { useClass: ProfileService }) | ||
12 | + ] | ||
8 | }) | 13 | }) |
9 | -@Inject(ArticleService, "noosfero", "$stateParams", "$httpParamSerializer", "$state", "SweetAlert") | 14 | +@Inject(ArticleService, ProfileService, "$state", "SweetAlert") |
10 | export class Cms { | 15 | export class Cms { |
11 | 16 | ||
12 | article: any = {}; | 17 | article: any = {}; |
13 | - profile: any; | ||
14 | 18 | ||
15 | - constructor(private ArticleService: ArticleService, private noosfero: any/* TODO convert noosferoService */, private $stateParams: ng.ui.IStateParamsService, private $httpParamSerializer: any, private $state: ng.ui.IStateService, private SweetAlert: any) { | ||
16 | - | ||
17 | - } | 19 | + constructor(private articleService: ArticleService, |
20 | + private profileService: ProfileService, | ||
21 | + private $state: ng.ui.IStateService, private SweetAlert: any) { } | ||
18 | 22 | ||
19 | save() { | 23 | save() { |
20 | - this.noosfero.currentProfile.then((profile: Profile) => { | ||
21 | - return this.ArticleService.create(profile.id, this.article); | 24 | + this.profileService.getCurrentProfile().then((profile: Profile) => { |
25 | + return this.articleService.create(profile.id, this.article); | ||
22 | }).then((response: restangular.IResponse) => { | 26 | }).then((response: restangular.IResponse) => { |
23 | this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier }); | 27 | this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier }); |
24 | this.SweetAlert.swal({ | 28 | this.SweetAlert.swal({ |
src/app/components/auth/auth_controller.ts
@@ -3,7 +3,7 @@ import {AuthService} from "./auth_service"; | @@ -3,7 +3,7 @@ import {AuthService} from "./auth_service"; | ||
3 | 3 | ||
4 | export class AuthController { | 4 | export class AuthController { |
5 | 5 | ||
6 | - static $inject = ["noosfero", "$log", "$stateParams", "AuthService"]; | 6 | + static $inject = ["$log", "$stateParams", "AuthService"]; |
7 | 7 | ||
8 | constructor( | 8 | constructor( |
9 | private noosfero: any, | 9 | private noosfero: any, |
src/app/components/navbar/navbar.spec.ts
1 | import { | 1 | import { |
2 | - createComponentFromClass, | ||
3 | - quickCreateComponent, | ||
4 | - provideEmptyObjects | 2 | +createComponentFromClass, |
3 | +quickCreateComponent, | ||
4 | +provideEmptyObjects | ||
5 | } from "./../../../spec/helpers"; | 5 | } from "./../../../spec/helpers"; |
6 | import { | 6 | import { |
7 | - Navbar | 7 | +Navbar |
8 | } from "./navbar"; | 8 | } from "./navbar"; |
9 | import { | 9 | import { |
10 | - AUTH_EVENTS | 10 | +AUTH_EVENTS |
11 | } from "./../auth"; | 11 | } from "./../auth"; |
12 | import { | 12 | import { |
13 | - User | 13 | +User |
14 | } from "./../../models/interfaces"; | 14 | } from "./../../models/interfaces"; |
15 | import { | 15 | import { |
16 | - Injectable, | ||
17 | - Provider, | ||
18 | - provide | 16 | +Injectable, |
17 | +Provider, | ||
18 | +provide | ||
19 | } from "ng-forward"; | 19 | } from "ng-forward"; |
20 | 20 | ||
21 | +import {Session, AuthService, AuthController, IAuthEvents} from "./../auth"; | ||
21 | 22 | ||
22 | describe("Components", () => { | 23 | describe("Components", () => { |
23 | 24 | ||
24 | - | ||
25 | describe("Navbar Component", () => { | 25 | describe("Navbar Component", () => { |
26 | 26 | ||
27 | let $rootScope: ng.IRootScopeService; | 27 | let $rootScope: ng.IRootScopeService; |
28 | + | ||
28 | let user = <User>{ | 29 | let user = <User>{ |
29 | id: 1, | 30 | id: 1, |
30 | login: "user" | 31 | login: "user" |
31 | }; | 32 | }; |
32 | 33 | ||
33 | - | ||
34 | let scope = { | 34 | let scope = { |
35 | eventCalledHook: () => { }, | 35 | eventCalledHook: () => { }, |
36 | $on: (eventName: string, func: Function) => { | 36 | $on: (eventName: string, func: Function) => { |
@@ -67,6 +67,7 @@ describe("Components", () => { | @@ -67,6 +67,7 @@ describe("Components", () => { | ||
67 | new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) | 67 | new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) |
68 | ]; | 68 | ]; |
69 | 69 | ||
70 | + | ||
70 | beforeEach(angular.mock.module("templates")); | 71 | beforeEach(angular.mock.module("templates")); |
71 | 72 | ||
72 | // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => { | 73 | // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => { |
@@ -76,7 +77,6 @@ describe("Components", () => { | @@ -76,7 +77,6 @@ describe("Components", () => { | ||
76 | it('should get the loggedIn user', (done: Function) => { | 77 | it('should get the loggedIn user', (done: Function) => { |
77 | 78 | ||
78 | let scope = jasmine.createSpyObj("scope", ["$on"]); | 79 | let scope = jasmine.createSpyObj("scope", ["$on"]); |
79 | - | ||
80 | let providers = [ | 80 | let providers = [ |
81 | provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), | 81 | provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), |
82 | new Provider('Session', { | 82 | new Provider('Session', { |
@@ -108,7 +108,8 @@ describe("Components", () => { | @@ -108,7 +108,8 @@ describe("Components", () => { | ||
108 | }); | 108 | }); |
109 | }); | 109 | }); |
110 | 110 | ||
111 | - it('It should open on click', (done: Function) => { | 111 | + it('should open on click', (done: Function) => { |
112 | + | ||
112 | quickCreateComponent({ | 113 | quickCreateComponent({ |
113 | providers: providers, | 114 | providers: providers, |
114 | template: "<acme-navbar></acme-navbar>", | 115 | template: "<acme-navbar></acme-navbar>", |
@@ -121,15 +122,16 @@ describe("Components", () => { | @@ -121,15 +122,16 @@ describe("Components", () => { | ||
121 | expect($modal.open).toHaveBeenCalled(); | 122 | expect($modal.open).toHaveBeenCalled(); |
122 | expect($modal.open).toHaveBeenCalledWith({ | 123 | expect($modal.open).toHaveBeenCalledWith({ |
123 | templateUrl: 'app/components/auth/login.html', | 124 | templateUrl: 'app/components/auth/login.html', |
124 | - controller: 'AuthController', | 125 | + controller: AuthController, |
125 | controllerAs: 'vm', | 126 | controllerAs: 'vm', |
126 | bindToController: true | 127 | bindToController: true |
127 | - }) | 128 | + }); |
128 | done(); | 129 | done(); |
129 | }) | 130 | }) |
130 | }); | 131 | }); |
131 | 132 | ||
132 | - it('It should logout', (done: Function) => { | 133 | + it('should logout', (done: Function) => { |
134 | + | ||
133 | quickCreateComponent({ | 135 | quickCreateComponent({ |
134 | providers: providers, | 136 | providers: providers, |
135 | template: "<acme-navbar></acme-navbar>", | 137 | template: "<acme-navbar></acme-navbar>", |
@@ -145,6 +147,38 @@ describe("Components", () => { | @@ -145,6 +147,38 @@ describe("Components", () => { | ||
145 | }); | 147 | }); |
146 | 148 | ||
147 | 149 | ||
150 | + it('should not activate user when logged in', (done: Function) => { | ||
151 | + quickCreateComponent({ | ||
152 | + providers: providers, | ||
153 | + template: "<acme-navbar></acme-navbar>", | ||
154 | + directives: [Navbar] | ||
155 | + }) | ||
156 | + .then(fixture => { | ||
157 | + let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance; | ||
158 | + spyOn(navbarComp, "openLogin"); | ||
159 | + navbarComp.activate(); | ||
160 | + expect((<any>navbarComp.openLogin).calls.count()).toBe(0); | ||
161 | + done(); | ||
162 | + }) | ||
163 | + | ||
164 | + }); | ||
165 | + | ||
166 | + it('should activate when user not logged in', (done: Function) => { | ||
167 | + user = null; | ||
168 | + quickCreateComponent({ | ||
169 | + providers: providers, | ||
170 | + template: "<acme-navbar></acme-navbar>", | ||
171 | + directives: [Navbar] | ||
172 | + }) | ||
173 | + .then(fixture => { | ||
174 | + let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance; | ||
175 | + spyOn(navbarComp, "openLogin"); | ||
176 | + navbarComp.activate(); | ||
177 | + expect(navbarComp.openLogin).toHaveBeenCalled(); | ||
178 | + done(); | ||
179 | + }) | ||
180 | + }); | ||
181 | + | ||
148 | 182 | ||
149 | // it('closes the modal the login', (done: Function) => { | 183 | // it('closes the modal the login', (done: Function) => { |
150 | // let scope = { | 184 | // let scope = { |
src/app/components/noosfero-articles/blog/blog.component.spec.ts
1 | import { | 1 | import { |
2 | + providers | ||
3 | +} from 'ng-forward/cjs/testing/providers'; | ||
4 | + | ||
5 | +import { | ||
2 | Input, | 6 | Input, |
3 | - provide, | ||
4 | Component | 7 | Component |
5 | } from 'ng-forward'; | 8 | } from 'ng-forward'; |
6 | import { | 9 | import { |
@@ -12,26 +15,16 @@ import { | @@ -12,26 +15,16 @@ import { | ||
12 | quickCreateComponent, | 15 | quickCreateComponent, |
13 | provideEmptyObjects, | 16 | provideEmptyObjects, |
14 | createProviderToValue, | 17 | createProviderToValue, |
15 | - getQService | 18 | + getAngularService, |
19 | + provideFilters | ||
16 | } from "../../../../spec/helpers.ts"; | 20 | } from "../../../../spec/helpers.ts"; |
17 | 21 | ||
18 | - | ||
19 | // this htmlTemplate will be re-used between the container components in this spec file | 22 | // this htmlTemplate will be re-used between the container components in this spec file |
20 | const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>'; | 23 | const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>'; |
21 | 24 | ||
22 | -let articleService: { | ||
23 | - getChildren: Function | ||
24 | -} = <any>{}; | ||
25 | - | ||
26 | describe("Blog Component", () => { | 25 | describe("Blog Component", () => { |
27 | 26 | ||
28 | - // the karma preprocessor html2js transform the templates html into js files which put | ||
29 | - // the templates to the templateCache into the module templates | ||
30 | - // we need to load the module templates here as the template for the | ||
31 | - // component Noosfero ArtileView will be load on our tests | ||
32 | - beforeEach(angular.mock.module("templates")); | ||
33 | - | ||
34 | - function promiseResultTemplate(response?: {}) { | 27 | + function promiseResultTemplate(response ? : {}) { |
35 | let thenFuncEmpty = (func: Function) => { | 28 | let thenFuncEmpty = (func: Function) => { |
36 | // does nothing | 29 | // does nothing |
37 | }; | 30 | }; |
@@ -50,48 +43,56 @@ describe("Blog Component", () => { | @@ -50,48 +43,56 @@ describe("Blog Component", () => { | ||
50 | } | 43 | } |
51 | } | 44 | } |
52 | 45 | ||
53 | - beforeAll(() => { | ||
54 | - // creating mock for articleService | ||
55 | - articleService = { | ||
56 | - getChildren: (article_id: number, filters: {}) => { | ||
57 | - return promiseResultTemplate(null); | ||
58 | - } | 46 | + let articleService = { |
47 | + getChildren: (article_id: number, filters: {}) => { | ||
48 | + return promiseResultTemplate(null); | ||
49 | + } | ||
50 | + }; | ||
51 | + | ||
52 | + @Component({ | ||
53 | + selector: 'test-container-component', | ||
54 | + template: htmlTemplate, | ||
55 | + directives: [ArticleBlog], | ||
56 | + providers: [ | ||
57 | + provideEmptyObjects('Restangular'), | ||
58 | + createProviderToValue('ArticleService', articleService), | ||
59 | + provideFilters('truncateFilter') | ||
60 | + ] | ||
61 | + }) | ||
62 | + class BlogContainerComponent { | ||
63 | + article = { | ||
64 | + type: 'anyArticleType' | ||
65 | + }; | ||
66 | + profile = { | ||
67 | + name: 'profile-name' | ||
59 | }; | 68 | }; |
69 | + } | ||
70 | + | ||
71 | + beforeEach(() => { | ||
72 | + | ||
73 | + // the karma preprocessor html2js transform the templates html into js files which put | ||
74 | + // the templates to the templateCache into the module templates | ||
75 | + // we need to load the module templates here as the template for the | ||
76 | + // component Noosfero ArtileView will be load on our tests | ||
77 | + angular.mock.module("templates") | ||
78 | + | ||
79 | + providers((provide: any) => { | ||
80 | + return <any > [ | ||
81 | + provide('ArticleService', { | ||
82 | + useValue: articleService | ||
83 | + }) | ||
84 | + ] | ||
85 | + }); | ||
60 | }); | 86 | }); |
61 | 87 | ||
62 | it("renders the blog content", (done: Function) => { | 88 | it("renders the blog content", (done: Function) => { |
63 | 89 | ||
64 | - // Creating a container component (ArticleContainerComponent) to include | ||
65 | - // the component under test (ArticleView) | ||
66 | - @Component({ | ||
67 | - selector: 'test-container-component', | ||
68 | - template: htmlTemplate, | ||
69 | - directives: [ArticleBlog], | ||
70 | - providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)] | ||
71 | - }) | ||
72 | - class BlogContainerComponent { | ||
73 | - article = { | ||
74 | - type: 'anyArticleType' | ||
75 | - }; | ||
76 | - profile = { | ||
77 | - name: 'profile-name' | ||
78 | - }; | ||
79 | - } | ||
80 | - | ||
81 | createComponentFromClass(BlogContainerComponent).then((fixture) => { | 90 | createComponentFromClass(BlogContainerComponent).then((fixture) => { |
82 | 91 | ||
83 | expect(fixture.debugElement.query('div.blog').length).toEqual(1); | 92 | expect(fixture.debugElement.query('div.blog').length).toEqual(1); |
84 | 93 | ||
85 | done(); | 94 | done(); |
86 | }); | 95 | }); |
87 | - | ||
88 | - | ||
89 | - | ||
90 | - }); | ||
91 | - | ||
92 | - it("get $q service", () => { | ||
93 | - let $q: ng.IQService = getQService(); | ||
94 | - console.log($q); | ||
95 | }); | 96 | }); |
96 | 97 | ||
97 | it("verify the blog data", (done: Function) => { | 98 | it("verify the blog data", (done: Function) => { |
@@ -102,44 +103,31 @@ describe("Blog Component", () => { | @@ -102,44 +103,31 @@ describe("Blog Component", () => { | ||
102 | headers: (headerName: string) => { | 103 | headers: (headerName: string) => { |
103 | return 1; | 104 | return 1; |
104 | }, | 105 | }, |
105 | - data: <any>{ | ||
106 | - articles: [] | 106 | + data: < any > { |
107 | + articles: [{ | ||
108 | + id: 1, | ||
109 | + title: 'The article test' | ||
110 | + }] | ||
107 | } | 111 | } |
108 | }); | 112 | }); |
109 | }; | 113 | }; |
110 | 114 | ||
111 | - @Component({ | ||
112 | - selector: 'test-container-component', | ||
113 | - template: htmlTemplate, | ||
114 | - directives: [ArticleBlog], | ||
115 | - providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)] | ||
116 | - }) | ||
117 | - class BlogContainerComponent { | ||
118 | - article = { | ||
119 | - type: 'anyArticleType' | ||
120 | - }; | ||
121 | - profile = { | ||
122 | - name: 'profile-name' | ||
123 | - }; | ||
124 | - } | ||
125 | - | ||
126 | createComponentFromClass(BlogContainerComponent).then((fixture) => { | 115 | createComponentFromClass(BlogContainerComponent).then((fixture) => { |
127 | 116 | ||
128 | // gets the children component of BlogContainerComponent | 117 | // gets the children component of BlogContainerComponent |
129 | let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | 118 | let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
130 | 119 | ||
131 | // check if the component property are the provided by the mocked articleService | 120 | // check if the component property are the provided by the mocked articleService |
132 | - expect((<any>articleBlog)["posts"]).toEqual([]); | ||
133 | - expect((<any>articleBlog)["totalPosts"]).toEqual(1); | ||
134 | - | 121 | + let post = { |
122 | + id: 1, | ||
123 | + title: 'The article test' | ||
124 | + }; | ||
125 | + expect(( < any > articleBlog)["posts"][0]).toEqual(jasmine.objectContaining(post)); | ||
126 | + expect(( < any > articleBlog)["totalPosts"]).toEqual(1); | ||
135 | 127 | ||
136 | - // done needs to be called (it isn't really needed, as we can read in | ||
137 | - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) | ||
138 | - // because createAsync in ng-forward is not really async, but as the intention | ||
139 | - // here is write tests in angular 2 ways, this is recommended | ||
140 | done(); | 128 | done(); |
141 | }); | 129 | }); |
142 | 130 | ||
143 | }); | 131 | }); |
144 | 132 | ||
145 | -}); | 133 | -}); |
134 | +}); | ||
146 | \ No newline at end of file | 135 | \ No newline at end of file |
src/app/components/noosfero-blocks/members-block/members-block.component.ts
@@ -13,12 +13,12 @@ export class MembersBlock { | @@ -13,12 +13,12 @@ export class MembersBlock { | ||
13 | 13 | ||
14 | members: any = []; | 14 | members: any = []; |
15 | 15 | ||
16 | - constructor(private ProfileService: ProfileService) { | 16 | + constructor(private profileService: ProfileService) { |
17 | 17 | ||
18 | } | 18 | } |
19 | 19 | ||
20 | ngOnInit() { | 20 | ngOnInit() { |
21 | - this.ProfileService.getProfileMembers(this.owner.id, { per_page: 6 }).then((response: any) => { | 21 | + this.profileService.getProfileMembers(this.owner.id, { per_page: 6 }).then((response: any) => { |
22 | this.members = response.data.people; | 22 | this.members = response.data.people; |
23 | }); | 23 | }); |
24 | } | 24 | } |
src/app/components/noosfero-blocks/profile-image/profile-image.component.spec.ts
1 | -import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; | 1 | +import {TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; |
2 | import {Pipe, Input, provide, Component} from 'ng-forward'; | 2 | import {Pipe, Input, provide, Component} from 'ng-forward'; |
3 | 3 | ||
4 | import {ProfileImageBlock} from './profile-image.component'; | 4 | import {ProfileImageBlock} from './profile-image.component'; |
5 | 5 | ||
6 | +import {ProfileService} from "./../../../../lib/ng-noosfero-api/http/profile.service"; | ||
7 | + | ||
8 | +import * as helpers from "./../../../../spec/helpers"; | ||
9 | + | ||
6 | const tcb = new TestComponentBuilder(); | 10 | const tcb = new TestComponentBuilder(); |
7 | 11 | ||
8 | const htmlTemplate: string = '<noosfero-profile-image-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-profile-image-block>'; | 12 | const htmlTemplate: string = '<noosfero-profile-image-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-profile-image-block>'; |
9 | 13 | ||
10 | 14 | ||
15 | + | ||
16 | + | ||
11 | describe("Components", () => { | 17 | describe("Components", () => { |
12 | describe("Profile Image Block Component", () => { | 18 | describe("Profile Image Block Component", () => { |
13 | 19 | ||
14 | beforeEach(angular.mock.module("templates")); | 20 | beforeEach(angular.mock.module("templates")); |
21 | + | ||
22 | + //beforeEach(angular.mock.module("restangular")); | ||
23 | + | ||
24 | + function buildServiceMock() { | ||
25 | + let profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getActivities"]); | ||
26 | + | ||
27 | + let thenObj = jasmine.createSpyObj("thenObj", ["then"]); | ||
28 | + | ||
29 | + thenObj.then = (func: Function) => { | ||
30 | + func({ | ||
31 | + data: { | ||
32 | + image: { | ||
33 | + name: 'some-thing', | ||
34 | + url: 'http://image.com' | ||
35 | + } | ||
36 | + } | ||
37 | + }) | ||
38 | + } | ||
39 | + | ||
40 | + profileServiceMock.getActivities = jasmine.createSpy("getActivities").and.returnValue(thenObj); | ||
41 | + | ||
42 | + return profileServiceMock; | ||
43 | + } | ||
15 | 44 | ||
16 | - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ProfileImageBlock] }) | 45 | + @Component( |
46 | + { | ||
47 | + selector: 'test-container-component', | ||
48 | + template: htmlTemplate, | ||
49 | + directives: [ProfileImageBlock], | ||
50 | + providers: [helpers.createProviderToValue("ProfileService", buildServiceMock())] | ||
51 | + | ||
52 | + }) | ||
17 | class BlockContainerComponent { | 53 | class BlockContainerComponent { |
18 | block = { type: 'Block' }; | 54 | block = { type: 'Block' }; |
19 | owner = { name: 'profile-name' }; | 55 | owner = { name: 'profile-name' }; |
20 | constructor() { | 56 | constructor() { |
21 | } | 57 | } |
22 | } | 58 | } |
59 | + | ||
60 | + | ||
23 | 61 | ||
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(); | 62 | + it("show image if present", () => { |
63 | + let profileServiceMock = buildServiceMock(); | ||
64 | + helpers.tcb.createAsync(BlockContainerComponent).then(fixture => { | ||
65 | + var elProfile = fixture.debugElement.componentViewChildren[0]; | ||
66 | + expect(elProfile.query('div.profile-image-block').length).toEqual(1); | ||
28 | }); | 67 | }); |
29 | }); | 68 | }); |
30 | - | ||
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 | - }); | 69 | + |
70 | + //TODO | ||
71 | + it("not show image if image is missing", () => { | ||
72 | + | ||
36 | }); | 73 | }); |
74 | + | ||
75 | + it("has link to the profile", () => { | ||
76 | + | ||
77 | + }); | ||
78 | + | ||
79 | + it("get activitities from profileService", () => { | ||
80 | + | ||
81 | + | ||
82 | + let profileServiceMock = buildServiceMock(); | ||
83 | + | ||
84 | + let profileImageBlock = new ProfileImageBlock(<any>profileServiceMock); | ||
85 | + | ||
86 | + profileImageBlock.ngOnInit(); | ||
87 | + expect(profileServiceMock.getActivities).toHaveBeenCalled(); | ||
88 | + expect(profileImageBlock.image.name).toEqual("some-thing"); | ||
89 | + }) | ||
90 | + | ||
91 | + // it("render the profile image", done => { | ||
92 | + // tcb.createAsync(BlockContainerComponent).then(fixture => { | ||
93 | + // expect(fixture.debugElement.queryAll("noosfero-profile-image").length).toEqual(1); | ||
94 | + // done(); | ||
95 | + // }); | ||
96 | + // }); | ||
97 | + // | ||
98 | + // it("render the settings link", done => { | ||
99 | + // tcb.createAsync(BlockContainerComponent).then(fixture => { | ||
100 | + // expect(fixture.debugElement.queryAll(".settings-link").length).toEqual(1); | ||
101 | + // done(); | ||
102 | + // }); | ||
103 | + // }); | ||
37 | 104 | ||
38 | }); | 105 | }); |
39 | }); | 106 | }); |
40 | \ No newline at end of file | 107 | \ No newline at end of file |
src/app/components/noosfero-blocks/profile-image/profile-image.component.ts
1 | -import {Input, Component} from "ng-forward"; | 1 | +import {Inject, Input, Component} from "ng-forward"; |
2 | +import {ProfileService} from "./../../../../lib/ng-noosfero-api/http/profile.service"; | ||
2 | 3 | ||
3 | @Component({ | 4 | @Component({ |
4 | selector: "noosfero-profile-image-block", | 5 | selector: "noosfero-profile-image-block", |
5 | templateUrl: 'app/components/noosfero-blocks/profile-image/profile-image.html', | 6 | templateUrl: 'app/components/noosfero-blocks/profile-image/profile-image.html', |
7 | + providers: [ProfileService] | ||
6 | }) | 8 | }) |
9 | +@Inject(ProfileService) | ||
7 | export class ProfileImageBlock { | 10 | export class ProfileImageBlock { |
8 | 11 | ||
9 | @Input() block: any; | 12 | @Input() block: any; |
10 | @Input() owner: any; | 13 | @Input() owner: any; |
14 | + | ||
15 | + image: any; | ||
16 | + | ||
17 | + constructor(private profileService: ProfileService) { | ||
18 | + | ||
19 | + } | ||
20 | + | ||
21 | + ngOnInit() { | ||
22 | + this.profileService.getActivities(null, {}).then((resp:any) => { | ||
23 | + this.image = resp.data.image; | ||
24 | + }) | ||
25 | + } | ||
11 | 26 | ||
12 | } | 27 | } |
src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts
@@ -13,10 +13,10 @@ export class RecentDocumentsBlock { | @@ -13,10 +13,10 @@ export class RecentDocumentsBlock { | ||
13 | 13 | ||
14 | profile: any; | 14 | profile: any; |
15 | documents: any; | 15 | documents: any; |
16 | - | 16 | + |
17 | documentsLoaded: boolean = false; | 17 | documentsLoaded: boolean = false; |
18 | 18 | ||
19 | - constructor(private ArticleService: ArticleService, private $state: any) { | 19 | + constructor(private articleService: ArticleService, private $state: any) { |
20 | } | 20 | } |
21 | 21 | ||
22 | ngOnInit() { | 22 | ngOnInit() { |
@@ -25,7 +25,7 @@ export class RecentDocumentsBlock { | @@ -25,7 +25,7 @@ export class RecentDocumentsBlock { | ||
25 | 25 | ||
26 | var limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; | 26 | var limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; |
27 | //FIXME get all text articles | 27 | //FIXME get all text articles |
28 | - this.ArticleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => { | 28 | + this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => { |
29 | this.documents = response.data.articles; | 29 | this.documents = response.data.articles; |
30 | this.documentsLoaded = true; | 30 | this.documentsLoaded = true; |
31 | }); | 31 | }); |
@@ -36,4 +36,3 @@ export class RecentDocumentsBlock { | @@ -36,4 +36,3 @@ export class RecentDocumentsBlock { | ||
36 | } | 36 | } |
37 | 37 | ||
38 | } | 38 | } |
39 | - |
src/app/components/noosfero/noosfero.service.js
@@ -1,12 +0,0 @@ | @@ -1,12 +0,0 @@ | ||
1 | -(function() { | ||
2 | - 'use strict'; | ||
3 | - | ||
4 | - angular.module('noosferoApp').factory('noosfero', function(Restangular, $q) { | ||
5 | - var currentProfile = $q.defer(); | ||
6 | - | ||
7 | - return { | ||
8 | - currentProfile: currentProfile.promise, | ||
9 | - setCurrentProfile: function(profile) { currentProfile.resolve(profile) } | ||
10 | - } | ||
11 | - }); | ||
12 | -})(); |
src/app/content-viewer/content-viewer-actions.component.ts
1 | -import {Component, Inject} from "ng-forward"; | 1 | +import {Component, Inject, provide} from "ng-forward"; |
2 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
2 | 3 | ||
3 | import {Profile} from "./../models/interfaces"; | 4 | import {Profile} from "./../models/interfaces"; |
5 | + | ||
4 | @Component({ | 6 | @Component({ |
5 | selector: "content-viewer-actions", | 7 | selector: "content-viewer-actions", |
6 | templateUrl: "app/content-viewer/navbar-actions.html", | 8 | templateUrl: "app/content-viewer/navbar-actions.html", |
9 | + providers: [provide('profileService', { useClass: ProfileService })] | ||
7 | }) | 10 | }) |
8 | -@Inject("noosfero") | 11 | +@Inject(ProfileService) |
9 | export class ContentViewerActions { | 12 | export class ContentViewerActions { |
10 | 13 | ||
11 | article: any; | 14 | article: any; |
12 | profile: any; | 15 | profile: any; |
13 | 16 | ||
14 | - constructor(noosfero: any) { | ||
15 | - noosfero.currentProfile.then((profile: Profile) => { | 17 | + constructor(profileService: ProfileService) { |
18 | + profileService.getCurrentProfile().then((profile: Profile) => { | ||
16 | this.profile = profile; | 19 | this.profile = profile; |
17 | }); | 20 | }); |
18 | } | 21 | } |
src/app/content-viewer/content-viewer.component.ts
@@ -2,17 +2,22 @@ import * as noosfero from "../models/interfaces"; | @@ -2,17 +2,22 @@ import * as noosfero from "../models/interfaces"; | ||
2 | 2 | ||
3 | 3 | ||
4 | import {ArticleView} from "../components/noosfero-articles/article/article_view"; | 4 | import {ArticleView} from "../components/noosfero-articles/article/article_view"; |
5 | -import {Input, Component, StateConfig, Inject} from "ng-forward"; | 5 | +import {Input, Component, StateConfig, Inject, provide} from "ng-forward"; |
6 | 6 | ||
7 | import {ArticleBlog} from "./../components/noosfero-articles/blog/blog.component"; | 7 | import {ArticleBlog} from "./../components/noosfero-articles/blog/blog.component"; |
8 | import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; | 8 | import {ArticleService} from "../../lib/ng-noosfero-api/http/article.service"; |
9 | +import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | ||
9 | 10 | ||
10 | @Component({ | 11 | @Component({ |
11 | selector: "content-viewer", | 12 | selector: "content-viewer", |
12 | templateUrl: "app/content-viewer/page.html", | 13 | templateUrl: "app/content-viewer/page.html", |
13 | - directives: [ArticleBlog, ArticleView] | 14 | + directives: [ArticleBlog, ArticleView], |
15 | + providers: [ | ||
16 | + provide('articleService', { useClass: ArticleService }), | ||
17 | + provide('profileService', { useClass: ProfileService }) | ||
18 | + ] | ||
14 | }) | 19 | }) |
15 | -@Inject(ArticleService, "noosfero", "$log", "$stateParams") | 20 | +@Inject(ArticleService, ProfileService, "$log", "$stateParams") |
16 | export class ContentViewer { | 21 | export class ContentViewer { |
17 | 22 | ||
18 | @Input() | 23 | @Input() |
@@ -21,14 +26,14 @@ export class ContentViewer { | @@ -21,14 +26,14 @@ export class ContentViewer { | ||
21 | @Input() | 26 | @Input() |
22 | profile: noosfero.Profile = null; | 27 | profile: noosfero.Profile = null; |
23 | 28 | ||
24 | - constructor(private ArticleService: ArticleService, private noosfero: any, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { | 29 | + constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { |
25 | this.activate(); | 30 | this.activate(); |
26 | } | 31 | } |
27 | 32 | ||
28 | activate() { | 33 | activate() { |
29 | - this.noosfero.currentProfile.then((profile: noosfero.Profile) => { | 34 | + this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { |
30 | this.profile = profile; | 35 | this.profile = profile; |
31 | - return this.ArticleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); | 36 | + return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); |
32 | }).then((response: restangular.IResponse) => { | 37 | }).then((response: restangular.IResponse) => { |
33 | this.article = response.data.article; | 38 | this.article = response.data.article; |
34 | }); | 39 | }); |
src/app/index.ts
@@ -28,7 +28,6 @@ NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS); | @@ -28,7 +28,6 @@ NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS); | ||
28 | NoosferoApp.addConfig(noosferoModuleConfig); | 28 | NoosferoApp.addConfig(noosferoModuleConfig); |
29 | NoosferoApp.run(noosferoAngularRunBlock); | 29 | NoosferoApp.run(noosferoAngularRunBlock); |
30 | 30 | ||
31 | -require("./components/noosfero/noosfero.service.js"); | ||
32 | require("./components/noosfero/profile-image/profile-image.component.js"); | 31 | require("./components/noosfero/profile-image/profile-image.component.js"); |
33 | 32 | ||
34 | NoosferoApp.addConfig(routeConfig); | 33 | NoosferoApp.addConfig(routeConfig); |
src/app/profile-info/profile-info.component.ts
1 | -import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | +import {StateConfig, Component, Inject, provide} from 'ng-forward'; |
2 | 2 | ||
3 | import {Profile} from "./../models/interfaces"; | 3 | import {Profile} from "./../models/interfaces"; |
4 | import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | 4 | import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; |
5 | 5 | ||
6 | @Component({ | 6 | @Component({ |
7 | selector: 'profile', | 7 | selector: 'profile', |
8 | - templateUrl: "app/profile-info/profile-info.html" | 8 | + templateUrl: "app/profile-info/profile-info.html", |
9 | + providers: [provide('profileService', { useClass: ProfileService })] | ||
9 | }) | 10 | }) |
10 | -@Inject(ProfileService, "noosfero") | 11 | +@Inject(ProfileService) |
11 | export class ProfileInfo { | 12 | export class ProfileInfo { |
12 | 13 | ||
13 | activities: any | 14 | activities: any |
14 | profile: any | 15 | profile: any |
15 | 16 | ||
16 | - constructor(private ProfileService: ProfileService, private noosfero: any) { | 17 | + constructor(private profileService: ProfileService) { |
17 | this.activate(); | 18 | this.activate(); |
18 | } | 19 | } |
19 | 20 | ||
20 | activate() { | 21 | activate() { |
21 | - this.noosfero.currentProfile.then((profile: Profile) => { | 22 | + this.profileService.getCurrentProfile().then((profile: Profile) => { |
22 | this.profile = profile; | 23 | this.profile = profile; |
23 | - return this.ProfileService.getActivities(this.profile.id); | 24 | + return this.profileService.getActivities(this.profile.id); |
24 | }).then((response: restangular.IResponse) => { | 25 | }).then((response: restangular.IResponse) => { |
25 | this.activities = response.data.activities; | 26 | this.activities = response.data.activities; |
26 | }); | 27 | }); |
@@ -0,0 +1,58 @@ | @@ -0,0 +1,58 @@ | ||
1 | +import {quickCreateComponent} from "../../spec/helpers"; | ||
2 | +import {ProfileHome} from "./profile-home.component"; | ||
3 | + | ||
4 | +describe("Components", () => { | ||
5 | + describe("Profile Home Component", () => { | ||
6 | + | ||
7 | + let $rootScope: ng.IRootScopeService; | ||
8 | + let $q: ng.IQService; | ||
9 | + let homePageResponse: ng.IDeferred<any>; | ||
10 | + let profileServiceMock: any; | ||
11 | + let $state: any; | ||
12 | + | ||
13 | + beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { | ||
14 | + $rootScope = _$rootScope_; | ||
15 | + $q = _$q_; | ||
16 | + })); | ||
17 | + | ||
18 | + beforeEach(() => { | ||
19 | + $state = jasmine.createSpyObj("$state", ["transitionTo"]); | ||
20 | + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getCurrentProfile", "getHomePage"]); | ||
21 | + | ||
22 | + let currentProfileResponse = $q.defer(); | ||
23 | + currentProfileResponse.resolve({ identifier: "profile" }); | ||
24 | + homePageResponse = $q.defer(); | ||
25 | + | ||
26 | + profileServiceMock.getCurrentProfile = jasmine.createSpy("getCurrentProfile").and.returnValue(currentProfileResponse.promise); | ||
27 | + profileServiceMock.getHomePage = jasmine.createSpy("getHomePage").and.returnValue(homePageResponse.promise); | ||
28 | + }); | ||
29 | + | ||
30 | + it("transition to profile homepage when there is a homepage setted", done => { | ||
31 | + homePageResponse.resolve({ data: { article: { path: "something" } } }); | ||
32 | + | ||
33 | + let component: ProfileHome = new ProfileHome(profileServiceMock, $state); | ||
34 | + $rootScope.$apply(); | ||
35 | + expect(profileServiceMock.getCurrentProfile).toHaveBeenCalled(); | ||
36 | + expect(profileServiceMock.getHomePage).toHaveBeenCalled(); | ||
37 | + | ||
38 | + expect($state.transitionTo). | ||
39 | + toHaveBeenCalledWith("main.profile.page", | ||
40 | + { page: "something", profile: "profile" }, { location: false }); | ||
41 | + done(); | ||
42 | + }); | ||
43 | + | ||
44 | + it("transition to profile info page when there is no homepage setted", done => { | ||
45 | + homePageResponse.resolve({ data: {} }); | ||
46 | + | ||
47 | + let component: ProfileHome = new ProfileHome(profileServiceMock, $state); | ||
48 | + $rootScope.$apply(); | ||
49 | + expect(profileServiceMock.getCurrentProfile).toHaveBeenCalled(); | ||
50 | + expect(profileServiceMock.getHomePage).toHaveBeenCalled(); | ||
51 | + | ||
52 | + expect($state.transitionTo). | ||
53 | + toHaveBeenCalledWith("main.profile.info", | ||
54 | + { profile: "profile" }, { location: false }); | ||
55 | + done(); | ||
56 | + }); | ||
57 | + }); | ||
58 | +}); |
src/app/profile/profile-home.component.ts
1 | -import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | +import {StateConfig, Component, Inject, provide} from 'ng-forward'; |
2 | 2 | ||
3 | import {Profile} from "./../models/interfaces"; | 3 | import {Profile} from "./../models/interfaces"; |
4 | import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; | 4 | import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; |
5 | 5 | ||
6 | @Component({ | 6 | @Component({ |
7 | selector: 'profile-home', | 7 | selector: 'profile-home', |
8 | - template: "<div></div>" | 8 | + template: "<div></div>", |
9 | + providers: [provide('profileService', { useClass: ProfileService })] | ||
9 | }) | 10 | }) |
10 | -@Inject(ProfileService, "noosfero", "$log", "$stateParams", "$scope", "$state") | 11 | +@Inject(ProfileService, "$state") |
11 | export class ProfileHome { | 12 | export class ProfileHome { |
12 | 13 | ||
13 | profile: Profile; | 14 | profile: Profile; |
14 | 15 | ||
15 | - constructor(ProfileService: ProfileService, noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) { | ||
16 | - noosfero.currentProfile.then((profile: Profile) => { | 16 | + constructor(profileService: ProfileService, $state: ng.ui.IStateService) { |
17 | + profileService.getCurrentProfile().then((profile: Profile) => { | ||
17 | this.profile = profile; | 18 | this.profile = profile; |
18 | - return ProfileService.get(this.profile.id).customGET('home_page', { fields: 'path' }); | 19 | + return profileService.getHomePage(this.profile.id, { fields: 'path' }); |
19 | }).then((response: restangular.IResponse) => { | 20 | }).then((response: restangular.IResponse) => { |
20 | if (response.data.article) { | 21 | if (response.data.article) { |
21 | $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); | 22 | $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); |
@@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
1 | +import {quickCreateComponent} from "../../spec/helpers"; | ||
2 | +import {Profile} from "./profile.component"; | ||
3 | + | ||
4 | +describe("Components", () => { | ||
5 | + describe("Profile Component", () => { | ||
6 | + | ||
7 | + let $rootScope: ng.IRootScopeService; | ||
8 | + let $q: ng.IQService; | ||
9 | + let profileServiceMock: any; | ||
10 | + let $stateParams: any; | ||
11 | + | ||
12 | + beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { | ||
13 | + $rootScope = _$rootScope_; | ||
14 | + $q = _$q_; | ||
15 | + })); | ||
16 | + | ||
17 | + beforeEach(() => { | ||
18 | + $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); | ||
19 | + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getByIdentifier", "getBoxes", "setCurrentProfile"]); | ||
20 | + | ||
21 | + let getByIdentifierResponse = $q.defer(); | ||
22 | + getByIdentifierResponse.resolve({ data: [{ id: 1 }] }); | ||
23 | + let getBoxesResponse = $q.defer(); | ||
24 | + getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); | ||
25 | + | ||
26 | + profileServiceMock.getByIdentifier = jasmine.createSpy("getByIdentifier").and.returnValue(getByIdentifierResponse.promise); | ||
27 | + profileServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); | ||
28 | + }); | ||
29 | + | ||
30 | + it("get the profile and store in profile service", done => { | ||
31 | + let component: Profile = new Profile(profileServiceMock, $stateParams); | ||
32 | + $rootScope.$apply(); | ||
33 | + expect(profileServiceMock.getByIdentifier).toHaveBeenCalled(); | ||
34 | + expect(profileServiceMock.setCurrentProfile).toHaveBeenCalled(); | ||
35 | + expect(component.profile).toEqual({ id: 1 }); | ||
36 | + done(); | ||
37 | + }); | ||
38 | + | ||
39 | + it("get the profile boxes", done => { | ||
40 | + let component: Profile = new Profile(profileServiceMock, $stateParams); | ||
41 | + $rootScope.$apply(); | ||
42 | + expect(profileServiceMock.getBoxes).toHaveBeenCalled(); | ||
43 | + expect(component.boxes).toEqual([{ id: 2 }]); | ||
44 | + done(); | ||
45 | + }); | ||
46 | + }); | ||
47 | +}); |
src/app/profile/profile.component.ts
1 | -import {StateConfig, Component, Inject} from 'ng-forward'; | 1 | +import {StateConfig, Component, Inject, provide} from 'ng-forward'; |
2 | import {ProfileInfo} from '../profile-info/profile-info.component'; | 2 | import {ProfileInfo} from '../profile-info/profile-info.component'; |
3 | import {ProfileHome} from '../profile/profile-home.component'; | 3 | import {ProfileHome} from '../profile/profile-home.component'; |
4 | import {Cms} from '../cms/cms.component'; | 4 | import {Cms} from '../cms/cms.component'; |
@@ -12,7 +12,8 @@ import * as noosferoModels from "./../models/interfaces"; | @@ -12,7 +12,8 @@ import * as noosferoModels from "./../models/interfaces"; | ||
12 | @Component({ | 12 | @Component({ |
13 | selector: 'profile', | 13 | selector: 'profile', |
14 | templateUrl: "app/profile/profile.html", | 14 | templateUrl: "app/profile/profile.html", |
15 | - directives: [NoosferoActivities] | 15 | + directives: [NoosferoActivities], |
16 | + providers: [provide('profileService', { useClass: ProfileService })] | ||
16 | }) | 17 | }) |
17 | @StateConfig([ | 18 | @StateConfig([ |
18 | { | 19 | { |
@@ -68,17 +69,17 @@ import * as noosferoModels from "./../models/interfaces"; | @@ -68,17 +69,17 @@ import * as noosferoModels from "./../models/interfaces"; | ||
68 | } | 69 | } |
69 | } | 70 | } |
70 | ]) | 71 | ]) |
71 | -@Inject(ProfileService, "noosfero", "$log", "$stateParams") | 72 | +@Inject(ProfileService, "$stateParams") |
72 | export class Profile { | 73 | export class Profile { |
73 | 74 | ||
74 | boxes: noosferoModels.Box[]; | 75 | boxes: noosferoModels.Box[]; |
75 | profile: noosferoModels.Profile; | 76 | profile: noosferoModels.Profile; |
76 | 77 | ||
77 | - constructor(ProfileService: ProfileService, noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService) { | ||
78 | - ProfileService.getByIdentifier($stateParams["profile"]).then((response: restangular.IResponse) => { | 78 | + constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService) { |
79 | + profileService.getByIdentifier($stateParams["profile"]).then((response: restangular.IResponse) => { | ||
79 | this.profile = response.data[0]; | 80 | this.profile = response.data[0]; |
80 | - noosfero.setCurrentProfile(this.profile); | ||
81 | - return ProfileService.getBoxes(this.profile.id); | 81 | + profileService.setCurrentProfile(this.profile); |
82 | + return profileService.getBoxes(this.profile.id); | ||
82 | }).then((response: restangular.IResponse) => { | 83 | }).then((response: restangular.IResponse) => { |
83 | this.boxes = response.data.boxes; | 84 | this.boxes = response.data.boxes; |
84 | }); | 85 | }); |
src/app/profile/profile.controller.spec.js
@@ -0,0 +1,76 @@ | @@ -0,0 +1,76 @@ | ||
1 | +import {Article} from "../../../app/models/interfaces"; | ||
2 | +import {ArticleService} from "./article.service"; | ||
3 | + | ||
4 | + | ||
5 | +describe("Services", () => { | ||
6 | + | ||
7 | + describe("Article Service", () => { | ||
8 | + | ||
9 | + let $httpBackend: ng.IHttpBackendService; | ||
10 | + let articleService: ArticleService; | ||
11 | + | ||
12 | + beforeEach(angular.mock.module("noosferoApp")); | ||
13 | + | ||
14 | + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ArticleService_: ArticleService) => { | ||
15 | + $httpBackend = _$httpBackend_; | ||
16 | + articleService = _ArticleService_; | ||
17 | + })); | ||
18 | + | ||
19 | + | ||
20 | + describe("Succesfull requests", () => { | ||
21 | + | ||
22 | + it("should return article children", (done) => { | ||
23 | + let articleId = 1; | ||
24 | + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children`).respond(200, { articles: [{ name: "article1" }] }); | ||
25 | + articleService.getChildren(articleId).then((response: restangular.IResponse) => { | ||
26 | + expect(response.data.articles).toEqual([{ name: "article1" }]); | ||
27 | + done(); | ||
28 | + }); | ||
29 | + $httpBackend.flush(); | ||
30 | + }); | ||
31 | + | ||
32 | + it("should get articles by profile", (done) => { | ||
33 | + let profileId = 1; | ||
34 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles`).respond(200, { articles: [{ name: "article1" }] }); | ||
35 | + articleService.getByProfile(profileId).then((response: restangular.IResponse) => { | ||
36 | + expect(response.data.articles).toEqual([{ name: "article1" }]); | ||
37 | + done(); | ||
38 | + }); | ||
39 | + $httpBackend.flush(); | ||
40 | + }); | ||
41 | + | ||
42 | + it("should get articles by profile with additional filters", (done) => { | ||
43 | + let profileId = 1; | ||
44 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles?path=test`).respond(200, { articles: [{ name: "article1" }] }); | ||
45 | + articleService.getByProfile(profileId, { path: 'test' }).then((response: restangular.IResponse) => { | ||
46 | + expect(response.data.articles).toEqual([{ name: "article1" }]); | ||
47 | + done(); | ||
48 | + }); | ||
49 | + $httpBackend.flush(); | ||
50 | + }); | ||
51 | + | ||
52 | + it("should get article children with additional filters", (done) => { | ||
53 | + let articleId = 1; | ||
54 | + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children?path=test`).respond(200, { articles: [{ name: "article1" }] }); | ||
55 | + articleService.getChildren(articleId, { path: 'test' }).then((response: restangular.IResponse) => { | ||
56 | + expect(response.data.articles).toEqual([{ name: "article1" }]); | ||
57 | + done(); | ||
58 | + }); | ||
59 | + $httpBackend.flush(); | ||
60 | + }); | ||
61 | + | ||
62 | + it("should create an article in a profile", (done) => { | ||
63 | + let profileId = 1; | ||
64 | + let article: Article = { id: null }; | ||
65 | + $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { articles: [{ id: 2 }] }); | ||
66 | + articleService.create(profileId, article).then((response: restangular.IResponse) => { | ||
67 | + expect(response.data.articles).toEqual([{ id: 2 }]); | ||
68 | + done(); | ||
69 | + }); | ||
70 | + $httpBackend.flush(); | ||
71 | + }); | ||
72 | + }); | ||
73 | + | ||
74 | + | ||
75 | + }); | ||
76 | +}); |
src/lib/ng-noosfero-api/http/article.service.ts
@@ -16,16 +16,16 @@ export class ArticleService { | @@ -16,16 +16,16 @@ export class ArticleService { | ||
16 | ); | 16 | ); |
17 | } | 17 | } |
18 | 18 | ||
19 | - get(articleId: number) { | ||
20 | - return this.Restangular.one('articles', articleId); | 19 | + getByProfile(profileId: number, params?: any) { |
20 | + return this.Restangular.one('profiles', profileId).customGET('articles', params); | ||
21 | } | 21 | } |
22 | 22 | ||
23 | - getByProfile(profileId: number, filters: any) { | ||
24 | - return this.Restangular.one('profiles', profileId).customGET('articles', filters); | 23 | + getChildren(articleId: number, params?: any) { |
24 | + return this.get(articleId).customGET('children', params); | ||
25 | } | 25 | } |
26 | 26 | ||
27 | - getChildren(articleId: number, options: any = {}) { | ||
28 | - return this.get(articleId).customGET('children', options); | 27 | + private get(articleId: number) { |
28 | + return this.Restangular.one('articles', articleId); | ||
29 | } | 29 | } |
30 | 30 | ||
31 | } | 31 | } |
@@ -0,0 +1,87 @@ | @@ -0,0 +1,87 @@ | ||
1 | +import {Profile} from "../../../app/models/interfaces"; | ||
2 | +import {ProfileService} from "./profile.service"; | ||
3 | +import {getAngularService} from "../../../spec/helpers"; | ||
4 | + | ||
5 | +describe("Services", () => { | ||
6 | + | ||
7 | + describe("Profile Service", () => { | ||
8 | + | ||
9 | + let $httpBackend: ng.IHttpBackendService; | ||
10 | + let profileService: ProfileService; | ||
11 | + let $rootScope: ng.IRootScopeService; | ||
12 | + | ||
13 | + beforeEach(angular.mock.module("noosferoApp")); | ||
14 | + | ||
15 | + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ProfileService_: ProfileService, _$rootScope_: ng.IRootScopeService) => { | ||
16 | + $httpBackend = _$httpBackend_; | ||
17 | + profileService = _ProfileService_; | ||
18 | + $rootScope = _$rootScope_; | ||
19 | + })); | ||
20 | + | ||
21 | + describe("Succesfull requests", () => { | ||
22 | + | ||
23 | + it("should return profile by its identifier", (done) => { | ||
24 | + let identifier = 'profile1'; | ||
25 | + $httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, [{ name: "profile1" }]); | ||
26 | + profileService.getByIdentifier(identifier).then((response: restangular.IResponse) => { | ||
27 | + expect(response.data[0]).toEqual({ name: "profile1" }); | ||
28 | + done(); | ||
29 | + }); | ||
30 | + $httpBackend.flush(); | ||
31 | + }); | ||
32 | + | ||
33 | + it("should return the members of a profile", (done) => { | ||
34 | + let profileId = 1; | ||
35 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/members`).respond(200, [{ name: "profile1" }]); | ||
36 | + profileService.getProfileMembers(profileId).then((response: restangular.IResponse) => { | ||
37 | + expect(response.data[0]).toEqual({ name: "profile1" }); | ||
38 | + done(); | ||
39 | + }); | ||
40 | + $httpBackend.flush(); | ||
41 | + }); | ||
42 | + | ||
43 | + it("should return the boxes of a profile", (done) => { | ||
44 | + let profileId = 1; | ||
45 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/boxes`).respond(200, [{ position: 1 }]); | ||
46 | + profileService.getBoxes(profileId).then((response: restangular.IResponse) => { | ||
47 | + expect(response.data[0]).toEqual({ position: 1 }); | ||
48 | + done(); | ||
49 | + }); | ||
50 | + $httpBackend.flush(); | ||
51 | + }); | ||
52 | + | ||
53 | + it("should return activities of a profile", (done) => { | ||
54 | + let profileId = 1; | ||
55 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/activities`).respond(200, [{ verb: "create_article" }]); | ||
56 | + profileService.getActivities(profileId).then((response: restangular.IResponse) => { | ||
57 | + expect(response.data[0]).toEqual({ verb: "create_article" }); | ||
58 | + done(); | ||
59 | + }); | ||
60 | + $httpBackend.flush(); | ||
61 | + }); | ||
62 | + | ||
63 | + it("should resolve the current profile", (done) => { | ||
64 | + let profile: Profile = { id: 1, identifier: "profile1" }; | ||
65 | + profileService.getCurrentProfile().then((currentProfile: Profile) => { | ||
66 | + expect(currentProfile).toEqual(currentProfile); | ||
67 | + done(); | ||
68 | + }); | ||
69 | + profileService.setCurrentProfile(profile); | ||
70 | + $rootScope.$apply(); | ||
71 | + }); | ||
72 | + | ||
73 | + it("should return the profile home page", (done) => { | ||
74 | + let profileId = 1; | ||
75 | + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/home_page`).respond(200, { article: { path: "/something" } }); | ||
76 | + profileService.getHomePage(profileId).then((response: restangular.IResponse) => { | ||
77 | + expect(response.data.article).toEqual({ path: "/something" }); | ||
78 | + done(); | ||
79 | + }); | ||
80 | + $httpBackend.flush(); | ||
81 | + }); | ||
82 | + | ||
83 | + }); | ||
84 | + | ||
85 | + | ||
86 | + }); | ||
87 | +}); |
src/lib/ng-noosfero-api/http/profile.service.ts
1 | import { Injectable, Inject } from "ng-forward"; | 1 | import { Injectable, Inject } from "ng-forward"; |
2 | +import {Profile} from "../../../app/models/interfaces"; | ||
2 | 3 | ||
3 | @Injectable() | 4 | @Injectable() |
4 | -@Inject("Restangular") | 5 | +@Inject("Restangular", "$q") |
5 | export class ProfileService { | 6 | export class ProfileService { |
6 | 7 | ||
7 | - constructor(private Restangular: any) { | 8 | + private _currentProfilePromise: ng.IDeferred<Profile>; |
8 | 9 | ||
10 | + constructor(private restangular: restangular.IService, $q: ng.IQService) { | ||
11 | + this._currentProfilePromise = $q.defer(); | ||
9 | } | 12 | } |
10 | 13 | ||
11 | - getActivities(profileId: number, options: any = {}) { | ||
12 | - return this.get(profileId).customGET("activities", options); | 14 | + getCurrentProfile(): ng.IPromise<Profile> { |
15 | + return this._currentProfilePromise.promise; | ||
13 | } | 16 | } |
14 | 17 | ||
15 | - get(profileId: number) { | ||
16 | - return this.Restangular.one('profiles', profileId); | 18 | + setCurrentProfile(profile: Profile) { |
19 | + this._currentProfilePromise.resolve(profile); | ||
17 | } | 20 | } |
18 | 21 | ||
19 | - getProfileMembers(profileId: number, filters: any) { | ||
20 | - return this.get(profileId).customGET("members", filters); | 22 | + getHomePage(profileId: number, params?: any) { |
23 | + return this.get(profileId).customGET("home_page", params); | ||
21 | } | 24 | } |
22 | 25 | ||
23 | - getByIdentifier(identifier: string) { | ||
24 | - return this.Restangular.one('profiles').get({ identifier: identifier }); | 26 | + getByIdentifier(identifier: string): restangular.IPromise<any> { |
27 | + return this.restangular.one('profiles').get({ identifier: identifier }); | ||
25 | } | 28 | } |
26 | 29 | ||
27 | - getBoxes(profileId: number) { | 30 | + getProfileMembers(profileId: number, params?: any): restangular.IPromise<any> { |
31 | + return this.get(profileId).customGET("members", params); | ||
32 | + } | ||
33 | + | ||
34 | + getBoxes(profileId: number): restangular.IPromise<any> { | ||
28 | return this.get(profileId).customGET('boxes'); | 35 | return this.get(profileId).customGET('boxes'); |
29 | } | 36 | } |
30 | 37 | ||
38 | + getActivities(profileId: number, params?: any): restangular.IPromise<any> { | ||
39 | + return this.get(profileId).customGET("activities", params); | ||
40 | + } | ||
41 | + | ||
42 | + get(profileId: number): restangular.IElement { | ||
43 | + return this.restangular.one('profiles', profileId); | ||
44 | + } | ||
45 | + | ||
31 | } | 46 | } |
src/spec/helpers.ts
@@ -10,7 +10,7 @@ export interface ComponentFixtureTemplate { | @@ -10,7 +10,7 @@ export interface ComponentFixtureTemplate { | ||
10 | template?: string; | 10 | template?: string; |
11 | } | 11 | } |
12 | 12 | ||
13 | -let tcb: TestComponentBuilder = new TestComponentBuilder(); | 13 | +export let tcb: TestComponentBuilder = new TestComponentBuilder(); |
14 | 14 | ||
15 | export function quickCreateComponent({ | 15 | export function quickCreateComponent({ |
16 | providers = [], | 16 | providers = [], |