body-state-classes.service.spec.ts
5.43 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
import * as helpers from '../../../spec/helpers';
import {BodyStateClassesService} from "./body-state-classes.service";
import {AuthService} from "./../../login/auth.service";
import {AUTH_EVENTS} from "./../../login/auth-events";
describe("BodyStateClasses Service", () => {
let currentStateName = "main";
let bodyStateClasseService: BodyStateClassesService;
let $rootScope: ng.IRootScopeService = <any>{},
$document: ng.IDocumentService = <any>{},
$state: ng.ui.IStateService = <any>{
current: {
name: currentStateName
}
},
authService: AuthService,
bodyEl: { className: string },
bodyElJq: any;
let getService = (): BodyStateClassesService => {
return new BodyStateClassesService($rootScope, $document, $state, authService);
};
beforeEach(() => {
authService = <any>{};
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;
$rootScope = <any>helpers.mocks.scopeWithEvents();
bodyElJq.addClass = jasmine.createSpy("addClass");
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
$rootScope.$emit(AUTH_EVENTS.loginSuccess);
// 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);
$rootScope = <any>helpers.mocks.scopeWithEvents();
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
$rootScope.$emit(AUTH_EVENTS.logoutSuccess);
// 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");
});
});