navbar.spec.ts
7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as helpers from "./../../../spec/helpers";
import {Navbar} from "./navbar";
import {Injectable, Provider, provide, EventEmitter} from "ng-forward";
import {ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder';
import {SessionService, AuthService, AuthController, AuthEvents} from "./../../login";
import events from 'ng-forward/cjs/events/events';
import {DesignModeService} from '../../admin/layout-edit/designMode.service';
describe("Components", () => {
describe("Navbar Component", () => {
let user: noosfero.User = null;
let scope: any;
let $rootScope: ng.IRootScopeService;
let modalInstance: any;
let $modal: any;
let authService: any;
let stateService: any;
let sessionService: SessionService;
let designModeService: DesignModeService;
let provideFunc = provide;
// before Each -> loading mocks on locals variables
beforeEach(() => {
user = <noosfero.User>{
id: 1,
login: "user"
};
scope = helpers.mocks.scopeWithEvents;
modalInstance = helpers.mocks.modalInstance;
$modal = helpers.mocks.$modal;
authService = helpers.mocks.authService;
stateService = jasmine.createSpyObj("$state", ["go"]);
sessionService = <any>helpers.mocks.sessionWithCurrentUser(user);
designModeService = helpers.mocks.designModeService;
});
// loading the templates
beforeEach(angular.mock.module("templates"));
// this function allow build the fixture of the container component
// and is reused in each test
// The main idea behing not prebuild it on a general beforeEach block is
// to allow tests configure the mock services accordilly their own needs
let buildComponent = (): Promise<ComponentFixture> => {
return helpers.quickCreateComponent({
providers: [
provide('$uibModal', {
useValue: $modal
}),
provide(AuthService, {
useValue: authService
}),
helpers.provideEmptyObjects('moment'),
provide('$state', {
useValue: stateService
}),
provide('SessionService', {
useValue: sessionService
}),
provide('AuthEvents', {
useValue: {
AuthEvents
}
}),
provide('EnvironmentService', {
useValue: {
getCurrentEnvironment: () => { return { id: 1, name: 'Nosofero' }; }
}
}),
provide('TranslatorService', {
useValue: helpers.mocks.translatorService
}),
provide('DesignModeService', {
useValue: helpers.mocks.designModeService
})
].concat(helpers.provideFilters("translateFilter")),
directives: [Navbar],
template: '<acme-navbar></acme-navbar>'
});
};
it('should get the loggedIn user', (done: Function) => {
buildComponent().then((fixture: ComponentFixture) => {
let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance;
expect(navbarInstance).toBeDefined();
expect(navbarInstance["currentUser"]).toEqual(user);
done();
});
});
it('should open on click', (done: Function) => {
spyOn($modal, "open");
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
navbarComp.openLogin();
expect($modal.open).toHaveBeenCalled();
expect($modal.open).toHaveBeenCalledWith({
templateUrl: 'app/login/login.html',
controller: AuthController,
controllerAs: 'vm',
bindToController: true
});
done();
});
});
it('should logout', (done: Function) => {
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
spyOn(authService, "logout");
try {
navbarComp.logout();
expect(authService.logout).toHaveBeenCalled();
done();
} catch (e) {
console.error(e);
fail(e.message);
done();
}
});
});
it('should not activate user when logged in', (done: Function) => {
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
spyOn(navbarComp, "openLogin");
navbarComp.activate();
expect((<any>navbarComp.openLogin).calls.count()).toBe(0);
done();
});
});
it('should activate when user not logged in', (done: Function) => {
spyOn(sessionService, 'currentUser').and.returnValue(null);
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
spyOn(navbarComp, "openLogin");
navbarComp.activate();
expect(navbarComp.openLogin).toHaveBeenCalled();
done();
});
});
it('closes the modal after login', (done: Function) => {
modalInstance = {};
modalInstance.close = jasmine.createSpy("close");
$modal.open = () => {
return modalInstance;
};
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
navbarComp.openLogin();
let successEvent: string = AuthEvents[AuthEvents.loginSuccess];
(<any>navbarComp.authService)[successEvent].next(user);
expect(modalInstance.close).toHaveBeenCalled();
done();
});
});
it('updates current user on logout', (done: Function) => {
buildComponent().then((fixture: ComponentFixture) => {
let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
// init navbar currentUser with some user
navbarComp["currentUser"] = user;
// changes the current User to return null,
// and emmit the 'logoutSuccess' event
// just what happens when user logsout
sessionService.currentUser = () => { return null; };
let successEvent: string = AuthEvents[AuthEvents.logoutSuccess];
(<any>navbarComp.authService)[successEvent].next(user);
done();
expect(navbarComp["currentUser"]).toBeNull();
});
});
});
});