From 12a1974c4c91c6fdddd9eaabb83f1481e09c5749 Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Wed, 1 Jun 2016 10:17:43 -0300 Subject: [PATCH] added tests to check the state resolve login on MainComponent --- src/app/main/main.component.spec.ts | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/main/main.component.ts | 2 -- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/app/main/main.component.spec.ts diff --git a/src/app/main/main.component.spec.ts b/src/app/main/main.component.spec.ts new file mode 100644 index 0000000..43a746d --- /dev/null +++ b/src/app/main/main.component.spec.ts @@ -0,0 +1,81 @@ + +import { provide, Component, componentStore, bundleStore } from "ng-forward"; +import {MainComponent} from "./main.component"; +import {TestComponentBuilder, ComponentFixture} from "ng-forward/cjs/testing/test-component-builder"; + +import {quickCreateComponent} from "./../../spec/helpers"; + +describe("MainComponent", function () { + + let localFixture: ComponentFixture; + let $state: angular.ui.IStateService; + let $q: ng.IQService; + let authService: any = jasmine.createSpyObj("authService", ["loginFromCookie"]); + let environmentService: any = jasmine.createSpyObj("environmentService", ["get"]); + + beforeEach(angular.mock.module("ui.router")); + beforeEach(angular.mock.module("templates")); + + @Component({ + selector: "parent", + template: "
", + directives: [MainComponent], + providers: [ + provide("AuthService", + { + useValue: authService + }), + + provide("EnvironmentService", + { + useValue: environmentService + }), + ] + }) + class MainComponentParent { + constructor() { + + } + } + + beforeEach(() => { + authService.loginFromCookie = jasmine.createSpy("loginFromCookie").and.returnValue({ id: 1, name: "user1" }); + environmentService.get = jasmine.createSpy("get").and.returnValue({ id: 1, name: "Noosfero Default Environment" }); + }); + + it("renders the main component only when the login and environment were resolved", (done) => { + quickCreateComponent({ directives: [MainComponentParent], template: "" }) + .then((fixture) => { + localFixture = fixture; + // get the $state service to navigate between routes + $state = fixture.debugElement.getLocal("$state"); + // navigates to the environment home + $state.go("main.environment.home"); + localFixture.detectChanges(); + // after changes were detected it checks the current $state route + expect($state.current.name).toEqual("main.environment.home"); + done(); + }); + + }); + + it("does not render the main component when get error loading the environment", (done) => { + quickCreateComponent({ directives: [MainComponentParent], template: "" }) + .then((fixture) => { + localFixture = fixture; + // get the $state service to navigate between routes + $state = fixture.debugElement.getLocal("$state"); + // get the $q service to create a rejected promise + $q = fixture.debugElement.getLocal("$q"); + // mock the environmentService to force a rejected promise + environmentService.get = jasmine.createSpy("get").and.returnValue($q.reject("Error simulated")); + // tries to navigate to the environment home + $state.go("main.environment.home"); + localFixture.detectChanges(); + // after the changes were detected the state remains '' because the environment could not be loaded + expect($state.current.name).toEqual(""); + done(); + }); + + }); +}); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 7ceba9d..10a29e2 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -113,11 +113,9 @@ export class EnvironmentContent { name: 'main', resolve: { currentUser: function(AuthService: AuthService) { - console.log("RESOLVING USER..."); return AuthService.loginFromCookie(); }, currentEnvironment: function(EnvironmentService: EnvironmentService) { - console.log("RESOLVING ENVIRONMENT..."); return EnvironmentService.get(); } } -- libgit2 0.21.2