Commit d04a87e2560d17b95736118bc9f6439473344313

Authored by ABNER SILVA DE OLIVEIRA
1 parent 12de4066

added tests for body state classes service

gulp/build.js
... ... @@ -52,7 +52,7 @@ gulp.task('html', ['inject', 'partials'], function () {
52 52 .pipe($.sourcemaps.init())
53 53 .pipe($.ngAnnotate())
54 54 // TODO - check how to make uglify work with ngforward
55   - .pipe($.uglify({ preserveComments: $.uglifySaveLicense, mangle: true, output: { beautify: false} })).on('error', conf.errorHandler('Uglify'))
  55 + .pipe($.uglify({ preserveComments: $.uglifySaveLicense, mangle: false, output: { beautify: false} })).on('error', conf.errorHandler('Uglify'))
56 56 .pipe($.sourcemaps.write('maps'))
57 57 .pipe(jsFilter.restore)
58 58 .pipe(cssFilter)
... ...
src/app/layout/services/body-state-classes.service.spec.ts
1   -import {provideFilters} from '../../../spec/helpers';
  1 +import * as helpers from '../../../spec/helpers';
2 2 import {BodyStateClassesService} from "./body-state-classes.service";
3 3 import {AuthService} from "./../../login/auth.service";
  4 +import {AUTH_EVENTS} from "./../../login/auth-events";
4 5  
5 6 describe("BodyStateClasses Service", () => {
6 7 let currentStateName = "main";
... ... @@ -12,16 +13,22 @@ describe("BodyStateClasses Service", () => {
12 13 name: currentStateName
13 14 }
14 15 },
15   - authService: AuthService = <any>{
16   - isAuthenticated: () => false
17   - },
18   - bodyEl: { className: string } = { className: "" },
19   - bodyElJq: any = [bodyEl];
  16 + authService: AuthService,
  17 + bodyEl: { className: string },
  18 + bodyElJq: any;
  19 +
20 20  
21 21 let getService = (): BodyStateClassesService => {
22 22 return new BodyStateClassesService($rootScope, $document, $state, authService);
23 23 };
24 24  
  25 + beforeEach(() => {
  26 + authService = <any>{};
  27 + authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
  28 + bodyEl = { className: "" };
  29 + bodyElJq = [bodyEl];
  30 + });
  31 +
25 32 it("should add the class noosfero-user-logged to the body element if the user is authenticated", () => {
26 33 authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
27 34 $rootScope.$on = jasmine.createSpy("$on");
... ... @@ -50,14 +57,78 @@ describe(&quot;BodyStateClasses Service&quot;, () =&gt; {
50 57 });
51 58  
52 59 it("should capture loginSuccess event and add noosfero-user-logged class to the body element", () => {
53   - pending("Test not yet implemented!");
  60 + let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
  61 + $rootScope = <any>helpers.mocks.scopeWithEvents();
  62 + bodyElJq.addClass = jasmine.createSpy("addClass");
  63 + authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(false);
  64 + let service = getService();
  65 +
  66 + service["bodyElement"] = bodyElJq;
  67 +
  68 + // triggers the service start
  69 + service.start();
  70 + // check if the the body element addClass was not called yet,
  71 + // because the user is not authenticated
  72 + expect(bodyElJq.addClass).not.toHaveBeenCalledWith(userLoggedClassName);
  73 +
  74 + // emit the event loginSuccess
  75 + $rootScope.$emit(AUTH_EVENTS.loginSuccess);
  76 +
  77 + // and check now if the addClass was called passing the userLogged class
  78 + // to the body Element
  79 + expect(bodyElJq.addClass).toHaveBeenCalledWith(userLoggedClassName);
54 80 });
55 81  
56   - it("should capture logoutSuccess event and remove noosfero-user-logged class to the body element", () => {
57   - pending("Test not yet implemented!");
  82 + it("should capture logoutSuccess event and remove noosfero-user-logged class from the body element", () => {
  83 + let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
  84 +
  85 + authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(true);
  86 + $rootScope = <any>helpers.mocks.scopeWithEvents();
  87 +
  88 + bodyElJq.addClass = jasmine.createSpy("addClass");
  89 + bodyElJq.removeClass = jasmine.createSpy("removeClass");
  90 +
  91 + let service = getService();
  92 + service["bodyElement"] = bodyElJq;
  93 +
  94 + // triggers the service start
  95 + service.start();
  96 +
  97 + // check if the the body element addClass was called
  98 + // because the user is already authenticated
  99 + expect(bodyElJq.addClass).toHaveBeenCalledWith(userLoggedClassName);
  100 +
  101 + // emit the event logoutSuccess
  102 + $rootScope.$emit(AUTH_EVENTS.logoutSuccess);
  103 +
  104 + // and check now if the removeClass was called passing the userLogged class
  105 + // to the body Element
  106 + expect(bodyElJq.removeClass).toHaveBeenCalledWith(userLoggedClassName);
58 107 });
59 108  
60 109 it("should capture $stateChangeSuccess event and switch route class in the body element", () => {
61   - pending("Test not yet implemented!");
  110 + let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME;
  111 +
  112 + authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(false);
  113 + $rootScope = <any>helpers.mocks.scopeWithEvents();
  114 + bodyElJq.addClass = (className: string) => {
  115 + bodyEl.className = className;
  116 + }
  117 + bodyElJq.removeClass = jasmine.createSpy("removeClass");
  118 +
  119 + let service = getService();
  120 + service["bodyElement"] = bodyElJq;
  121 +
  122 + // triggers the service start
  123 + service.start();
  124 +
  125 + // checks if the bodyEl has a class indicating the currentState
  126 + expect(bodyEl.className).toEqual(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + currentStateName);
  127 +
  128 + // emit the event $stateChangeSuccess
  129 + $rootScope.$emit("$stateChangeSuccess", null, {name: "new-route"});
  130 +
  131 + // and check now if the bodyEl has a class indicating the new state route
  132 + expect(bodyEl.className).toEqual(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + "new-route");
62 133 });
63 134 });
64 135 \ No newline at end of file
... ...
src/app/layout/services/body-state-classes.service.ts
... ... @@ -17,6 +17,8 @@ import {HtmlUtils} from &quot;../html-utils&quot;;
17 17 @Inject("$rootScope", "$document", "$state", AuthService)
18 18 export class BodyStateClassesService {
19 19  
  20 + private started: boolean = false;
  21 +
20 22 public static get USER_LOGGED_CLASSNAME(): string { return "noosfero-user-logged"; }
21 23 public static get ROUTE_STATE_CLASSNAME_PREFIX(): string { return "noosfero-route-"; }
22 24  
... ... @@ -32,18 +34,21 @@ export class BodyStateClassesService {
32 34 }
33 35  
34 36 start() {
35   - this.setupUserLoggedClassToggle();
36   - this.setupStateClassToggle();
  37 + if (!this.started) {
  38 + this.setupUserLoggedClassToggle();
  39 + this.setupStateClassToggle();
  40 + this.started = true;
  41 + }
37 42 }
38 43  
39   - getStateChangeSuccessHandlerFunction(bodyElement: ng.IAugmentedJQuery): (event: ng.IAngularEvent, toState: ng.ui.IState) => void {
  44 + private getStateChangeSuccessHandlerFunction(bodyElement: ng.IAugmentedJQuery): (event: ng.IAngularEvent, toState: ng.ui.IState) => void {
40 45 let self = this;
41 46 return (event: ng.IAngularEvent, toState: ng.ui.IState) => {
42   - self.switchStateClasses(bodyElement, BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX);
  47 + self.switchStateClasses(bodyElement, toState);
43 48 };
44 49 }
45 50  
46   - switchStateClasses(bodyElement: ng.IAugmentedJQuery, state: ng.ui.IState) {
  51 + private switchStateClasses(bodyElement: ng.IAugmentedJQuery, state: ng.ui.IState) {
47 52 HtmlUtils.removeCssClassByPrefix(bodyElement[0], BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX);
48 53 bodyElement.addClass(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + state.name);
49 54 }
... ...
src/spec/mocks.ts
  1 +const DEBUG = false;
  2 +
  3 +let log = (message: string, ...args: any[]) => {
  4 + if (DEBUG) {
  5 + console.log(message);
  6 + }
  7 +};
1 8  
2 9 class ScopeWithEvents {
3 10 listeners = {};
... ... @@ -13,18 +20,18 @@ class ScopeWithEvents {
13 20 }
14 21 }
15 22  
16   - public $emit(message: string, arg?: any) {
17   - console.log("Emitted " + message);
  23 + public $emit(message: string, ...args: any[]) {
  24 + log("Emitted " + message);
18 25 if ((<any>this.listeners)[message]) {
19   - console.log("LISTENERS:", (<any>this.listeners)[message]);
  26 + log("LISTENERS:", (<any>this.listeners)[message]);
20 27 (<any>this.listeners)[message].forEach((f: Function) => {
21   - f(arg);
  28 + f.apply(this, args);
22 29 });
23 30 }
24 31 }
25 32 }
26 33 export var mocks = {
27   - scopeWithEvents: new ScopeWithEvents(),
  34 + scopeWithEvents: (): ScopeWithEvents => new ScopeWithEvents(),
28 35 modalInstance: {
29 36 close: () => { }
30 37 },
... ...