From d04a87e2560d17b95736118bc9f6439473344313 Mon Sep 17 00:00:00 2001 From: ABNER SILVA DE OLIVEIRA Date: Thu, 24 Mar 2016 18:47:21 -0300 Subject: [PATCH] added tests for body state classes service --- gulp/build.js | 2 +- src/app/layout/services/body-state-classes.service.spec.ts | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- src/app/layout/services/body-state-classes.service.ts | 15 ++++++++++----- src/spec/mocks.ts | 17 ++++++++++++----- 4 files changed, 104 insertions(+), 21 deletions(-) diff --git a/gulp/build.js b/gulp/build.js index d9a5c1e..4d7e466 100644 --- a/gulp/build.js +++ b/gulp/build.js @@ -52,7 +52,7 @@ gulp.task('html', ['inject', 'partials'], function () { .pipe($.sourcemaps.init()) .pipe($.ngAnnotate()) // TODO - check how to make uglify work with ngforward - .pipe($.uglify({ preserveComments: $.uglifySaveLicense, mangle: true, output: { beautify: false} })).on('error', conf.errorHandler('Uglify')) + .pipe($.uglify({ preserveComments: $.uglifySaveLicense, mangle: false, output: { beautify: false} })).on('error', conf.errorHandler('Uglify')) .pipe($.sourcemaps.write('maps')) .pipe(jsFilter.restore) .pipe(cssFilter) diff --git a/src/app/layout/services/body-state-classes.service.spec.ts b/src/app/layout/services/body-state-classes.service.spec.ts index 5d8c7bb..1ee2c9a 100644 --- a/src/app/layout/services/body-state-classes.service.spec.ts +++ b/src/app/layout/services/body-state-classes.service.spec.ts @@ -1,6 +1,7 @@ -import {provideFilters} from '../../../spec/helpers'; +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"; @@ -12,16 +13,22 @@ describe("BodyStateClasses Service", () => { name: currentStateName } }, - authService: AuthService = { - isAuthenticated: () => false - }, - bodyEl: { className: string } = { className: "" }, - bodyElJq: any = [bodyEl]; + authService: AuthService, + bodyEl: { className: string }, + bodyElJq: any; + let getService = (): BodyStateClassesService => { return new BodyStateClassesService($rootScope, $document, $state, authService); }; + beforeEach(() => { + authService = {}; + 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"); @@ -50,14 +57,78 @@ describe("BodyStateClasses Service", () => { }); it("should capture loginSuccess event and add noosfero-user-logged class to the body element", () => { - pending("Test not yet implemented!"); + let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME; + $rootScope = 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 to the body element", () => { - pending("Test not yet implemented!"); + 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 = 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", () => { - pending("Test not yet implemented!"); + let userLoggedClassName = BodyStateClassesService.USER_LOGGED_CLASSNAME; + + authService.isAuthenticated = jasmine.createSpy("isAuthenticated").and.returnValue(false); + $rootScope = 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"); }); }); \ No newline at end of file diff --git a/src/app/layout/services/body-state-classes.service.ts b/src/app/layout/services/body-state-classes.service.ts index 5b33191..bbc5ec8 100644 --- a/src/app/layout/services/body-state-classes.service.ts +++ b/src/app/layout/services/body-state-classes.service.ts @@ -17,6 +17,8 @@ import {HtmlUtils} from "../html-utils"; @Inject("$rootScope", "$document", "$state", AuthService) export class BodyStateClassesService { + private started: boolean = false; + public static get USER_LOGGED_CLASSNAME(): string { return "noosfero-user-logged"; } public static get ROUTE_STATE_CLASSNAME_PREFIX(): string { return "noosfero-route-"; } @@ -32,18 +34,21 @@ export class BodyStateClassesService { } start() { - this.setupUserLoggedClassToggle(); - this.setupStateClassToggle(); + if (!this.started) { + this.setupUserLoggedClassToggle(); + this.setupStateClassToggle(); + this.started = true; + } } - getStateChangeSuccessHandlerFunction(bodyElement: ng.IAugmentedJQuery): (event: ng.IAngularEvent, toState: ng.ui.IState) => void { + private getStateChangeSuccessHandlerFunction(bodyElement: ng.IAugmentedJQuery): (event: ng.IAngularEvent, toState: ng.ui.IState) => void { let self = this; return (event: ng.IAngularEvent, toState: ng.ui.IState) => { - self.switchStateClasses(bodyElement, BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX); + self.switchStateClasses(bodyElement, toState); }; } - switchStateClasses(bodyElement: ng.IAugmentedJQuery, state: ng.ui.IState) { + private switchStateClasses(bodyElement: ng.IAugmentedJQuery, state: ng.ui.IState) { HtmlUtils.removeCssClassByPrefix(bodyElement[0], BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX); bodyElement.addClass(BodyStateClassesService.ROUTE_STATE_CLASSNAME_PREFIX + state.name); } diff --git a/src/spec/mocks.ts b/src/spec/mocks.ts index 08571d5..280645d 100644 --- a/src/spec/mocks.ts +++ b/src/spec/mocks.ts @@ -1,3 +1,10 @@ +const DEBUG = false; + +let log = (message: string, ...args: any[]) => { + if (DEBUG) { + console.log(message); + } +}; class ScopeWithEvents { listeners = {}; @@ -13,18 +20,18 @@ class ScopeWithEvents { } } - public $emit(message: string, arg?: any) { - console.log("Emitted " + message); + public $emit(message: string, ...args: any[]) { + log("Emitted " + message); if ((this.listeners)[message]) { - console.log("LISTENERS:", (this.listeners)[message]); + log("LISTENERS:", (this.listeners)[message]); (this.listeners)[message].forEach((f: Function) => { - f(arg); + f.apply(this, args); }); } } } export var mocks = { - scopeWithEvents: new ScopeWithEvents(), + scopeWithEvents: (): ScopeWithEvents => new ScopeWithEvents(), modalInstance: { close: () => { } }, -- libgit2 0.21.2