Commit ebb3050ef1d1c772c53106c88e936b707a5b6024
Exists in
master
and in
34 other branches
Merge branch 'ngforward' of softwarepublico.gov.br:noosfero-themes/angular-theme into ngforward
Showing
2 changed files
with
137 additions
and
119 deletions
Show diff stats
src/app/components/navbar/navbar.spec.ts
@@ -18,54 +18,55 @@ import { | @@ -18,54 +18,55 @@ import { | ||
18 | provide | 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; | ||
28 | - let user = <User>{ | ||
29 | - id: 1, | ||
30 | - login: "user" | ||
31 | - }; | ||
32 | - | 27 | + let $rootScope: ng.IRootScopeService; |
28 | + | ||
29 | + let user = <User>{ | ||
30 | + id: 1, | ||
31 | + login: "user" | ||
32 | + }; | ||
33 | + | ||
34 | + let scope = { | ||
35 | + eventCalledHook: () => { }, | ||
36 | + $on: (eventName: string, func: Function) => { | ||
37 | + this.eventCalledHook = func; | ||
38 | + } | ||
39 | + } | ||
33 | 40 | ||
34 | - let scope = { | ||
35 | - eventCalledHook: () => { }, | ||
36 | - $on: (eventName: string, func: Function) => { | ||
37 | - this.eventCalledHook = func; | 41 | + let modalInstance = { |
42 | + close: () => { } | ||
38 | } | 43 | } |
39 | - } | ||
40 | 44 | ||
41 | - let modalInstance = { | ||
42 | - close: () => { } | ||
43 | - } | 45 | + let $modal = { |
46 | + open: (args: {}) => { | ||
47 | + return modalInstance; | ||
48 | + } | ||
49 | + } | ||
44 | 50 | ||
45 | - let $modal = { | ||
46 | - open: (args: {}) => { | ||
47 | - return modalInstance; | 51 | + let authService = { |
52 | + logout: () => { } | ||
48 | } | 53 | } |
49 | - } | ||
50 | - | ||
51 | - let authService = { | ||
52 | - logout: () => { } | ||
53 | - } | ||
54 | - | ||
55 | - let stateService = jasmine.createSpyObj("$state", ["go"]); | ||
56 | - let providers = [ | ||
57 | - new Provider('moment', { useValue: {} }), | ||
58 | - new Provider('$modal', { useValue: $modal }), | ||
59 | - new Provider('AuthService', { useValue: authService }), | ||
60 | - new Provider('Session', { | ||
61 | - useValue: { | ||
62 | - currentUser: () => { return user } | ||
63 | - } | ||
64 | - }), | ||
65 | - new Provider('$scope', { useValue: scope }), | ||
66 | - new Provider('$state', { useValue: stateService }), | ||
67 | - new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) | ||
68 | - ]; | 54 | + |
55 | + let stateService = jasmine.createSpyObj("$state", ["go"]); | ||
56 | + let providers = [ | ||
57 | + new Provider('moment', { useValue: {} }), | ||
58 | + new Provider('$modal', { useValue: $modal }), | ||
59 | + new Provider('AuthService', { useValue: authService }), | ||
60 | + new Provider('Session', { | ||
61 | + useValue: { | ||
62 | + currentUser: () => { return user } | ||
63 | + } | ||
64 | + }), | ||
65 | + new Provider('$scope', { useValue: scope }), | ||
66 | + new Provider('$state', { useValue: stateService }), | ||
67 | + new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) | ||
68 | + ]; | ||
69 | + | ||
69 | 70 | ||
70 | beforeEach(angular.mock.module("templates")); | 71 | beforeEach(angular.mock.module("templates")); |
71 | 72 | ||
@@ -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>", |
@@ -119,17 +120,18 @@ describe("Components", () => { | @@ -119,17 +120,18 @@ describe("Components", () => { | ||
119 | spyOn($modal, "open"); | 120 | spyOn($modal, "open"); |
120 | navbarComp.openLogin(); | 121 | navbarComp.openLogin(); |
121 | expect($modal.open).toHaveBeenCalled(); | 122 | expect($modal.open).toHaveBeenCalled(); |
122 | - // expect($modal.open).toHaveBeenCalledWith({ | ||
123 | - // templateUrl: 'app/components/auth/login.html', | ||
124 | - // controller: 'AuthController', | ||
125 | - // controllerAs: 'vm', | ||
126 | - // bindToController: true | ||
127 | - // }) | 123 | + expect($modal.open).toHaveBeenCalledWith({ |
124 | + templateUrl: 'app/components/auth/login.html', | ||
125 | + controller: AuthController, | ||
126 | + controllerAs: 'vm', | ||
127 | + bindToController: true | ||
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(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 {providers} from 'ng-forward/cjs/testing/providers'; | ||
2 | + | ||
1 | import { | 3 | import { |
2 | Input, | 4 | Input, |
3 | - provide, | ||
4 | Component | 5 | Component |
5 | } from 'ng-forward'; | 6 | } from 'ng-forward'; |
6 | import { | 7 | import { |
@@ -12,26 +13,16 @@ import { | @@ -12,26 +13,16 @@ import { | ||
12 | quickCreateComponent, | 13 | quickCreateComponent, |
13 | provideEmptyObjects, | 14 | provideEmptyObjects, |
14 | createProviderToValue, | 15 | createProviderToValue, |
15 | - getAngularService | 16 | + getAngularService, |
17 | + provideFilters | ||
16 | } from "../../../../spec/helpers.ts"; | 18 | } from "../../../../spec/helpers.ts"; |
17 | 19 | ||
18 | - | ||
19 | // this htmlTemplate will be re-used between the container components in this spec file | 20 | // 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>'; | 21 | const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>'; |
21 | 22 | ||
22 | -let articleService: { | ||
23 | - getChildren: Function | ||
24 | -} = < any > {}; | ||
25 | - | ||
26 | describe("Blog Component", () => { | 23 | describe("Blog Component", () => { |
27 | - | ||
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 ? : {}) { | 24 | + |
25 | + function promiseResultTemplate(response?: {}) { | ||
35 | let thenFuncEmpty = (func: Function) => { | 26 | let thenFuncEmpty = (func: Function) => { |
36 | // does nothing | 27 | // does nothing |
37 | }; | 28 | }; |
@@ -50,48 +41,54 @@ describe("Blog Component", () => { | @@ -50,48 +41,54 @@ describe("Blog Component", () => { | ||
50 | } | 41 | } |
51 | } | 42 | } |
52 | 43 | ||
53 | - beforeAll(() => { | ||
54 | - // creating mock for articleService | ||
55 | - articleService = { | ||
56 | - getChildren: (article_id: number, filters: {}) => { | ||
57 | - return promiseResultTemplate(null); | ||
58 | - } | 44 | + let articleService = { |
45 | + getChildren: (article_id: number, filters: {}) => { | ||
46 | + return promiseResultTemplate(null); | ||
47 | + } | ||
48 | + }; | ||
49 | + | ||
50 | + @Component({ | ||
51 | + selector: 'test-container-component', | ||
52 | + template: htmlTemplate, | ||
53 | + directives: [ArticleBlog], | ||
54 | + providers: [ | ||
55 | + provideEmptyObjects('Restangular'), | ||
56 | + createProviderToValue('ArticleService', articleService), | ||
57 | + provideFilters('truncateFilter') | ||
58 | + ] | ||
59 | + }) | ||
60 | + class BlogContainerComponent { | ||
61 | + article = { | ||
62 | + type: 'anyArticleType' | ||
63 | + }; | ||
64 | + profile = { | ||
65 | + name: 'profile-name' | ||
59 | }; | 66 | }; |
67 | + } | ||
68 | + | ||
69 | + beforeEach(() => { | ||
70 | + | ||
71 | + // the karma preprocessor html2js transform the templates html into js files which put | ||
72 | + // the templates to the templateCache into the module templates | ||
73 | + // we need to load the module templates here as the template for the | ||
74 | + // component Noosfero ArtileView will be load on our tests | ||
75 | + angular.mock.module("templates") | ||
76 | + | ||
77 | + providers((provide: any) => { | ||
78 | + return <any>[ | ||
79 | + provide('ArticleService', { useValue: articleService }) | ||
80 | + ] | ||
81 | + }); | ||
60 | }); | 82 | }); |
61 | 83 | ||
62 | it("renders the blog content", (done: Function) => { | 84 | it("renders the blog content", (done: Function) => { |
63 | 85 | ||
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) => { | 86 | createComponentFromClass(BlogContainerComponent).then((fixture) => { |
82 | 87 | ||
83 | expect(fixture.debugElement.query('div.blog').length).toEqual(1); | 88 | expect(fixture.debugElement.query('div.blog').length).toEqual(1); |
84 | 89 | ||
85 | done(); | 90 | done(); |
86 | }); | 91 | }); |
87 | - | ||
88 | - | ||
89 | - | ||
90 | - }); | ||
91 | - | ||
92 | - it("get $q service", () => { | ||
93 | - let $q = getAngularService<ng.IQService>("$q"); | ||
94 | - console.log($q); | ||
95 | }); | 92 | }); |
96 | 93 | ||
97 | it("verify the blog data", (done: Function) => { | 94 | it("verify the blog data", (done: Function) => { |
@@ -102,44 +99,31 @@ describe("Blog Component", () => { | @@ -102,44 +99,31 @@ describe("Blog Component", () => { | ||
102 | headers: (headerName: string) => { | 99 | headers: (headerName: string) => { |
103 | return 1; | 100 | return 1; |
104 | }, | 101 | }, |
105 | - data: < any > { | ||
106 | - articles: [] | 102 | + data: <any>{ |
103 | + articles: [{ | ||
104 | + id: 1, | ||
105 | + title: 'The article test' | ||
106 | + }] | ||
107 | } | 107 | } |
108 | }); | 108 | }); |
109 | }; | 109 | }; |
110 | 110 | ||
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) => { | 111 | createComponentFromClass(BlogContainerComponent).then((fixture) => { |
127 | 112 | ||
128 | // gets the children component of BlogContainerComponent | 113 | // gets the children component of BlogContainerComponent |
129 | let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | 114 | let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
130 | 115 | ||
131 | - // 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 | - | 116 | + // check if the component property are the provided by the mocked articleService |
117 | + var post = { | ||
118 | + id: 1, | ||
119 | + title: 'The article test' | ||
120 | + }; | ||
121 | + expect((<any>articleBlog)["posts"][0]).toEqual(jasmine.objectContaining(post)); | ||
122 | + expect((<any>articleBlog)["totalPosts"]).toEqual(1); | ||
135 | 123 | ||
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(); | 124 | done(); |
141 | }); | 125 | }); |
142 | 126 | ||
143 | }); | 127 | }); |
144 | 128 | ||
145 | -}); | ||
146 | \ No newline at end of file | 129 | \ No newline at end of file |
130 | +}); |