body-state-classes.service.spec.ts
7.92 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
202
203
import * as helpers from '../../../spec/helpers';
import {BodyStateClassesService} from "./body-state-classes.service";
import {AuthService} from "./../../login/auth.service";
import {AuthEvents} from "./../../login/auth-events";
import {EventEmitter} from 'ng-forward';
import {DesignModeService} from './../../admin/layout-edit/designMode.service';
import {INoosferoLocalStorage} from "./../../shared/models/interfaces";
describe("BodyStateClasses Service", () => {
let currentStateName = "main";
let bodyStateClasseService: BodyStateClassesService;
let $localStorage = <INoosferoLocalStorage>{ currentUser: null, settings: { designMode: false } };
let $rootScope: ng.IRootScopeService = <any>{},
$document: ng.IDocumentService = <any>{},
$state: ng.ui.IStateService = <any>{
current: {
name: currentStateName
}
},
authService: any = helpers.mocks.authService,
bodyEl: { className: string },
bodyElJq: any,
designModeService = new DesignModeService($localStorage);
let getService = (): BodyStateClassesService => {
return new BodyStateClassesService($rootScope, $document, $state, authService, designModeService);
};
beforeEach(() => {
authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
bodyEl = { className: "" };
bodyElJq = [bodyEl];
});
it("should add the class noosfero-user-logged to the body element if the user is authenticated", () => {
authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
$rootScope.$on = jasmine.createSpy("$on");
let service = getService();
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
service["bodyElement"] = bodyElJq;
service.start();
expect(bodyElJq.addClass).toHaveBeenCalledWith(BodyStateClassesService.USER_LOGGED_CLASSNAME);
});
it("should add the class noosfero-route-[currentStateName] the body element", () => {
$rootScope.$on = jasmine.createSpy("$on");
let service = getService();
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
service["bodyElement"] = bodyElJq;
service.start();
let stateClassName = BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + currentStateName;
expect(bodyElJq.addClass).toHaveBeenCalledWith(stateClassName);
});
it("should capture loginSuccess event and add noosfero-user-logged class to the body element", () => {
let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(false);
let service = getService();
service["bodyElement"] = bodyElJq;
// triggers the service start
service.start();
// check if the the body element addClass was not called yet,
// because the user is not authenticated
expect(bodyElJq.addClass).not.toHaveBeenCalledWith(userLoggedClassName);
// emit the event loginSuccess
authService.loginSuccess.next(null);
// and check now if the addClass was called passing the userLogged class
// to the body Element
expect(bodyElJq.addClass).toHaveBeenCalledWith(userLoggedClassName);
});
it("should capture logoutSuccess event and remove noosfero-user-logged class from the body element", () => {
let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
let service = getService();
service["bodyElement"] = bodyElJq;
// triggers the service start
service.start();
// check if the the body element addClass was called
// because the user is already authenticated
expect(bodyElJq.addClass).toHaveBeenCalledWith(userLoggedClassName);
// emit the event logoutSuccess
authService.logoutSuccess.next(null);
// and check now if the removeClass was called passing the userLogged class
// to the body Element
expect(bodyElJq.removeClass).toHaveBeenCalledWith(userLoggedClassName);
});
it("should capture $stateChangeSuccess event and switch route class in the body element", () => {
let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(false);
$rootScope = <any>helpers.mocks.scopeWithEvents();
bodyElJq.addClass = (className: string) => {
bodyEl.className = className;
};
bodyElJq.removeClass = jasmine.createSpy("removeClass");
let service = getService();
service["bodyElement"] = bodyElJq;
// triggers the service start
service.start();
// checks if the bodyEl has a class indicating the currentState
expect(bodyEl.className).toEqual(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + currentStateName);
// emit the event $stateChangeSuccess
$rootScope.$emit("$stateChangeSuccess", null, { name: "new-route" });
// and check now if the bodyEl has a class indicating the new state route
expect(bodyEl.className).toEqual(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + "new-route");
});
it("add a css class theme skin to body element", () => {
let service = getService();
let skinClass: string = 'skin-test';
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
service["bodyElement"] = bodyElJq;
service.start({
skin: skinClass
});
expect(bodyElJq.addClass).toHaveBeenCalledWith(skinClass);
});
it("add a css class to content wrapper element", () => {
let service = getService();
let contentWrapperMock = jasmine.createSpyObj("contentWrapperMock", ["addClass", "removeClass"]);
service["getContentWrapper"] = jasmine.createSpy("getContentWrapper").and.returnValue(contentWrapperMock);
service.addContentClass(true);
expect(contentWrapperMock.addClass).toHaveBeenCalledWith(BodyStateClassesService.CONTENT_WRAPPER_FULL);
});
it("remove a css class from content wrapper element", () => {
let service = getService();
let contentWrapperMock = jasmine.createSpyObj("contentWrapperMock", ["addClass", "removeClass"]);
service["getContentWrapper"] = jasmine.createSpy("getContentWrapper").and.returnValue(contentWrapperMock);
service.addContentClass(false);
expect(contentWrapperMock.removeClass).toHaveBeenCalledWith(BodyStateClassesService.CONTENT_WRAPPER_FULL);
});
it("should add the class noosfero-design-on when designMode is changed to true", () => {
let fnOnToggle: Function = null;
designModeService.onToggle = <any> {
subscribe: (fn: Function) => {
fnOnToggle = fn;
},
next: (value: boolean) => {
fnOnToggle.apply(designModeService, [value]);
}
};
let service = getService();
bodyElJq.addClass = jasmine.createSpy("addClass");
bodyElJq.removeClass = jasmine.createSpy("removeClass");
service["bodyElement"] = bodyElJq;
service.start();
debugger;
designModeService.setInDesignMode(true);
expect(bodyElJq.addClass).toHaveBeenCalledWith(BodyStateClassesService.DESIGN_MODE_ON_CLASSNAME);
});
});