From 2e0755c9a5e1afdc8f082678ffee4c2effd76e18 Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Wed, 9 Mar 2016 07:18:49 -0300 Subject: [PATCH] ll --- src/app/components/navbar/navbar.spec.ts | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------- src/app/components/navbar/navbar.ts | 2 ++ src/spec/helpers.ts | 16 ++++++++++++++++ 3 files changed, 159 insertions(+), 74 deletions(-) diff --git a/src/app/components/navbar/navbar.spec.ts b/src/app/components/navbar/navbar.spec.ts index 620e46f..a074cda 100644 --- a/src/app/components/navbar/navbar.spec.ts +++ b/src/app/components/navbar/navbar.spec.ts @@ -1,8 +1,12 @@ +<<<<<<< Updated upstream import { createComponentFromClass, quickCreateComponent, provideEmptyObjects } from "./../../../spec/helpers"; +======= +import * as helpers from "./../../../spec/helpers"; +>>>>>>> Stashed changes import { Navbar } from "./navbar"; @@ -18,6 +22,15 @@ import { provide } from "ng-forward"; +<<<<<<< Updated upstream +======= +import { +Session, +AuthService, +AuthController, +IAuthEvents +} from "./../auth"; +>>>>>>> Stashed changes describe("Components", () => { @@ -36,35 +49,51 @@ describe("Components", () => { $on: (eventName: string, func: Function) => { this.eventCalledHook = func; } - } + }; let modalInstance = { close: () => { } - } + }; let $modal = { open: (args: {}) => { return modalInstance; } - } + }; let authService = { logout: () => { } - } + }; let stateService = jasmine.createSpyObj("$state", ["go"]); let providers = [ - new Provider('moment', { useValue: {} }), - new Provider('$modal', { useValue: $modal }), - new Provider('AuthService', { useValue: authService }), + new Provider('moment', { + useValue: {} + }), + new Provider('$modal', { + useValue: $modal + }), + new Provider('AuthService', { + useValue: authService + }), new Provider('Session', { useValue: { - currentUser: () => { return user } + currentUser: () => { + return user; + } } }), - new Provider('$scope', { useValue: scope }), - new Provider('$state', { useValue: stateService }), - new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) + new Provider('$scope', { + useValue: scope + }), + new Provider('$state', { + useValue: stateService + }), + new Provider('AUTH_EVENTS', { + useValue: { + AUTH_EVENTS + } + }) ]; beforeEach(angular.mock.module("templates")); @@ -78,7 +107,7 @@ describe("Components", () => { let scope = jasmine.createSpyObj("scope", ["$on"]); let providers = [ - provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), + helpers.provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), new Provider('Session', { useValue: { currentUser: () => { @@ -96,20 +125,22 @@ describe("Components", () => { }) ]; - quickCreateComponent({ + helpers.quickCreateComponent({ providers: providers, template: "", directives: [Navbar] }).then(fixture => { let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; expect(navbarInstance).toBeDefined(); - expect(navbarInstance["currentUser"]).toEqual(user) + expect(navbarInstance["currentUser"]).toEqual(user); done(); }); }); - it('It should open on click', (done: Function) => { - quickCreateComponent({ + + it('should open on click', (done: Function) => { + + helpers.quickCreateComponent({ providers: providers, template: "", directives: [Navbar] @@ -126,11 +157,12 @@ describe("Components", () => { bindToController: true }) done(); - }) + }); }); - it('It should logout', (done: Function) => { - quickCreateComponent({ + it('should logout', (done: Function) => { + + helpers.quickCreateComponent({ providers: providers, template: "", directives: [Navbar] @@ -141,64 +173,99 @@ describe("Components", () => { navbarComp.logout(); expect(authService.logout).toHaveBeenCalled(); done(); - }) + }); + }); + it('should not activate user when logged in', (done: Function) => { + helpers.quickCreateComponent({ + providers: providers, + template: "", + directives: [Navbar] + }) + .then(fixture => { + let navbarComp: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; + spyOn(navbarComp, "openLogin"); + navbarComp.activate(); + expect((navbarComp.openLogin).calls.count()).toBe(0); + done(); + }); + + }); + + it('should activate when user not logged in', (done: Function) => { + user = null; + helpers.quickCreateComponent({ + providers: providers, + template: "", + directives: [Navbar] + }) + .then(fixture => { + let navbarComp: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; + spyOn(navbarComp, "openLogin"); + navbarComp.activate(); + expect(navbarComp.openLogin).toHaveBeenCalled(); + done(); + }); }); + it('closes the modal the login', (done: Function) => { + let scope = { + $on: (eventName: string, func: Function) => { + console.log("ON Called!"); + } + }; + + let modalInstance = { + close: () => { + console.log("CLOSE Called!"); + } + }; + + let $modal = { + open: (args: {}) => { + return modalInstance; + } + }; + + + + let stateService = jasmine.createSpyObj("$state", ["go"]); + let providers = [ + new Provider('moment', { useValue: {} }), + new Provider('$modal', { useValue: $modal }), + new Provider('AuthService', { useValue: {} }), + new Provider('Session', { + useValue: { + currentUser: () => { return user; } + } + }), + new Provider('$scope', { useValue: scope }), + new Provider('$state', { useValue: stateService }), + new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) + ]; + spyOn(modalInstance, "close"); + + helpers.quickCreateComponent({ + providers: providers, + template: "", + directives: [Navbar] + }) + .then(fixture => { + let navbarComp: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; + + spyOn($modal, 'open'); + navbarComp.activate(); + navbarComp.openLogin(); + let openArgs = { templateUrl: 'app/components/auth/login.html', controller: Function, controllerAs: 'vm', bindToController: true }; - // it('closes the modal the login', (done: Function) => { - // let scope = { - // eventCalledHook: () => { }, - // $on: (eventName: string, func: Function) => { - // console.log("ON Called!"); - // this.eventCalledHook = func; - // } - // } - // - // let modalInstance = { - // close: () => { - // console.log("CLOSE Called!"); - // } - // } - // - // let $modal = { - // $open: (args: {}) => { - // return modalInstance; - // } - // } - // - // let stateService = jasmine.createSpyObj("$state", ["go"]); - // let providers = [ - // new Provider('moment', { useValue: {} }), - // new Provider('$modal', { useValue: $modal }), - // new Provider('AuthService', { useValue: {} }), - // new Provider('Session', { - // useValue: { - // currentUser: () => { return user } - // } - // }), - // new Provider('$scope', { useValue: scope }), - // new Provider('$state', { useValue: stateService }), - // new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } }) - // ]; - // spyOn(modalInstance, "close"); - // - // quickCreateComponent({ - // providers: providers, - // template: "", - // directives: [Navbar] - // }) - // .then(fixture => { - // let navbarComp: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; - // navbarComp.activate(); - // navbarComp.openLogin - // - // expect($modal.open).toHaveBeenCalledWith({}) - // scope.eventCalledHook(); - // expect(modalInstance.close).toHaveBeenCalled(); - // done(); - // }) - // //done(); - // }); + + expect($modal.open).toHaveBeenCalled(); + scope.eventCalledHook(); + expect(modalInstance.close).toHaveBeenCalled(); + done(); + }); + // done(); + }); +>>>>>>> Stashed changes }); }); diff --git a/src/app/components/navbar/navbar.ts b/src/app/components/navbar/navbar.ts index d00bfe5..1f1bea0 100644 --- a/src/app/components/navbar/navbar.ts +++ b/src/app/components/navbar/navbar.ts @@ -29,6 +29,7 @@ export class Navbar { this.currentUser = session.currentUser(); $scope.$on(AUTH_EVENTS.loginSuccess, () => { + console.log("HEREERERERERERERERERRE"); if (this.modalInstance) { this.modalInstance.close(); this.modalInstance = null; @@ -44,6 +45,7 @@ export class Navbar { } openLogin() { + console.log('OPENLOGIN', this.$modal); this.modalInstance = this.$modal.open({ templateUrl: 'app/components/auth/login.html', controller: AuthController, diff --git a/src/spec/helpers.ts b/src/spec/helpers.ts index 1adb90a..6c052d9 100644 --- a/src/spec/helpers.ts +++ b/src/spec/helpers.ts @@ -60,12 +60,20 @@ class AngularServiceHookComponent { } } +/** + * This helper class allows get angular services to be used in integration tests + * i.e: '$http', '$q', '$location', etc... + */ class AngularServiceFactory { +<<<<<<< Updated upstream fixtureComponentHookPoint: ComponentFixture; tcb: TestComponentBuilder = new TestComponentBuilder(); constructor() { this.fixtureComponentHookPoint = (this.tcb)["create"](AngularServiceHookComponent); +======= + constructor(private fixtureComponentHookPoint: ComponentFixture) { +>>>>>>> Stashed changes } getAngularService(angularService: string) { @@ -81,6 +89,7 @@ class AngularServiceFactory { } } +<<<<<<< Updated upstream export var angularServiceFactory = new AngularServiceFactory(); /** * This help function allows get angular services to be used in integration tests @@ -97,6 +106,13 @@ export function getQService(): ng.IQService { export function getHttpBackendService(): ng.IHttpBackendService { return angularServiceFactory.getHttpBackendService(); } +======= +export function getAngularServiceFactory(fixture: ComponentFixture) { + return new AngularServiceFactory(fixture); +} + + +>>>>>>> Stashed changes // export function getResolvablePromise() { // let $q = getQService(); -- libgit2 0.21.2