Commit 12a1974c4c91c6fdddd9eaabb83f1481e09c5749
1 parent
b6c4bc1a
Exists in
master
and in
14 other branches
added tests to check the state resolve login on MainComponent
Showing
2 changed files
with
81 additions
and
2 deletions
Show diff stats
... | ... | @@ -0,0 +1,81 @@ |
1 | + | |
2 | +import { provide, Component, componentStore, bundleStore } from "ng-forward"; | |
3 | +import {MainComponent} from "./main.component"; | |
4 | +import {TestComponentBuilder, ComponentFixture} from "ng-forward/cjs/testing/test-component-builder"; | |
5 | + | |
6 | +import {quickCreateComponent} from "./../../spec/helpers"; | |
7 | + | |
8 | +describe("MainComponent", function () { | |
9 | + | |
10 | + let localFixture: ComponentFixture; | |
11 | + let $state: angular.ui.IStateService; | |
12 | + let $q: ng.IQService; | |
13 | + let authService: any = jasmine.createSpyObj("authService", ["loginFromCookie"]); | |
14 | + let environmentService: any = jasmine.createSpyObj("environmentService", ["get"]); | |
15 | + | |
16 | + beforeEach(angular.mock.module("ui.router")); | |
17 | + beforeEach(angular.mock.module("templates")); | |
18 | + | |
19 | + @Component({ | |
20 | + selector: "parent", | |
21 | + template: "<main></main>", | |
22 | + directives: [MainComponent], | |
23 | + providers: [ | |
24 | + provide("AuthService", | |
25 | + { | |
26 | + useValue: authService | |
27 | + }), | |
28 | + | |
29 | + provide("EnvironmentService", | |
30 | + { | |
31 | + useValue: environmentService | |
32 | + }), | |
33 | + ] | |
34 | + }) | |
35 | + class MainComponentParent { | |
36 | + constructor() { | |
37 | + | |
38 | + } | |
39 | + } | |
40 | + | |
41 | + beforeEach(() => { | |
42 | + authService.loginFromCookie = jasmine.createSpy("loginFromCookie").and.returnValue({ id: 1, name: "user1" }); | |
43 | + environmentService.get = jasmine.createSpy("get").and.returnValue({ id: 1, name: "Noosfero Default Environment" }); | |
44 | + }); | |
45 | + | |
46 | + it("renders the main component only when the login and environment were resolved", (done) => { | |
47 | + quickCreateComponent({ directives: [MainComponentParent], template: "<parent></parent>" }) | |
48 | + .then((fixture) => { | |
49 | + localFixture = fixture; | |
50 | + // get the $state service to navigate between routes | |
51 | + $state = fixture.debugElement.getLocal("$state"); | |
52 | + // navigates to the environment home | |
53 | + $state.go("main.environment.home"); | |
54 | + localFixture.detectChanges(); | |
55 | + // after changes were detected it checks the current $state route | |
56 | + expect($state.current.name).toEqual("main.environment.home"); | |
57 | + done(); | |
58 | + }); | |
59 | + | |
60 | + }); | |
61 | + | |
62 | + it("does not render the main component when get error loading the environment", (done) => { | |
63 | + quickCreateComponent({ directives: [MainComponentParent], template: "<parent></parent>" }) | |
64 | + .then((fixture) => { | |
65 | + localFixture = fixture; | |
66 | + // get the $state service to navigate between routes | |
67 | + $state = fixture.debugElement.getLocal("$state"); | |
68 | + // get the $q service to create a rejected promise | |
69 | + $q = fixture.debugElement.getLocal("$q"); | |
70 | + // mock the environmentService to force a rejected promise | |
71 | + environmentService.get = jasmine.createSpy("get").and.returnValue($q.reject("Error simulated")); | |
72 | + // tries to navigate to the environment home | |
73 | + $state.go("main.environment.home"); | |
74 | + localFixture.detectChanges(); | |
75 | + // after the changes were detected the state remains '' because the environment could not be loaded | |
76 | + expect($state.current.name).toEqual(""); | |
77 | + done(); | |
78 | + }); | |
79 | + | |
80 | + }); | |
81 | +}); | ... | ... |
src/app/main/main.component.ts
... | ... | @@ -113,11 +113,9 @@ export class EnvironmentContent { |
113 | 113 | name: 'main', |
114 | 114 | resolve: { |
115 | 115 | currentUser: function(AuthService: AuthService) { |
116 | - console.log("RESOLVING USER..."); | |
117 | 116 | return AuthService.loginFromCookie(); |
118 | 117 | }, |
119 | 118 | currentEnvironment: function(EnvironmentService: EnvironmentService) { |
120 | - console.log("RESOLVING ENVIRONMENT..."); | |
121 | 119 | return EnvironmentService.get(); |
122 | 120 | } |
123 | 121 | } | ... | ... |