main.component.spec.ts
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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";
import {getAngularServiceFactory} from "../../spec/helpers";
import { EVENTS_HUB_KNOW_EVENT_NAMES } from "../shared/services/events-hub.service";
describe("MainComponent", function () {
let localFixture: ComponentFixture;
let $state: angular.ui.IStateService;
let $q: ng.IQService;
let $httpBackend: ng.IHttpBackendService;
let authService: any = jasmine.createSpyObj("authService", ["loginFromCookie", "isAuthenticated", "subscribe"]);
let environmentService: any = jasmine.createSpyObj("environmentService", ["get", "getCurrentEnvironment"]);
beforeEach(angular.mock.module("ui.router"));
beforeEach(angular.mock.module("templates"));
beforeEach(angular.mock.module("pascalprecht.translate", ($translateProvider: angular.translate.ITranslateProvider) => {
$translateProvider.translations('en', {});
}));
@Component({
selector: "parent",
template: "<main></main>",
directives: [MainComponent],
providers: [
provide("AuthService",
{
useValue: authService
}),
provide("EnvironmentService",
{
useValue: environmentService
}),
provide(EVENTS_HUB_KNOW_EVENT_NAMES,
{
useValue: [
'IMAGE_PROFILE_UPDATED',
'PROFILE_INFO_UPDATED',
'ARTICLE_UPDATED',
'TASK_CLOSED'
]
}),
]
})
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: "<parent></parent>" })
.then((fixture) => {
fixture.debugElement.getLocal("$httpBackend").expectGET("/api/v1/environments/1/boxes").respond(200, {});
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: "<parent></parent>" })
.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();
});
});
});