forked from fabric8-ui/fabric8-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codebases-add.component.spec.ts
194 lines (179 loc) · 7.15 KB
/
codebases-add.component.spec.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { CodebasesAddComponent } from './codebases-add.component';
import { Observable } from 'rxjs';
import { Router, ActivatedRoute } from '@angular/router';
import { Contexts } from 'ngx-fabric8-wit';
import { Broadcaster, Notifications } from 'ngx-base';
import { CodebasesService } from '../services/codebases.service';
import { GitHubService } from '../services/github.service';
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { By } from '@angular/platform-browser';
import { Logger } from 'ngx-base';
import { AuthenticationService, UserService } from 'ngx-login-client';
import { MockBackend } from '@angular/http/testing';
import { XHRBackend, HttpModule } from '@angular/http';
import { ContextsMock, expectedGitHubRepo, expectedGitHubRepoCommit, expectedGitHubRepoDetails, expectedGitHubRepoLicense } from '../services/github.service.mock';
import { Codebase } from '../services/codebase';
import { FormsModule, NgForm } from '@angular/forms';
import {
async,
ComponentFixture,
fakeAsync,
inject,
TestBed,
tick
} from '@angular/core/testing';
describe('Codebases Add Component', () => {
let broadcasterMock: any;
let codebasesServiceMock: any;
let contextsMock: any;
let gitHubServiceMock: any;
let notificationMock: any;
let routerMock: any;
let routeMock: any;
let authServiceMock: any;
let userServiceMock: any;
let fixture, codebases, codebase;
beforeEach(() => {
broadcasterMock = jasmine.createSpyObj('Broadcaster', ['broadcast']);
codebasesServiceMock = jasmine.createSpyObj('CodebasesService', ['getCodebases', 'addCodebase']);
authServiceMock = jasmine.createSpy('AuthenticationService');
contextsMock = jasmine.createSpy('Contexts');
gitHubServiceMock = jasmine.createSpyObj('GitHubService', ['getRepoDetailsByFullName', 'getRepoLicenseByUrl']);
notificationMock = jasmine.createSpyObj('Notifications', ['message']);
routerMock = jasmine.createSpy('Router');
routeMock = jasmine.createSpy('ActivatedRoute');
userServiceMock = jasmine.createSpy('UserService');
TestBed.configureTestingModule({
imports: [FormsModule, HttpModule],
declarations: [CodebasesAddComponent],
providers: [
{
provide: Broadcaster, useValue: broadcasterMock
},
{
provide: CodebasesService, useValue: codebasesServiceMock
},
{
provide: Contexts, useClass: ContextsMock
},
{
provide: GitHubService, useValue: gitHubServiceMock
},
{
provide: Notifications, useValue: notificationMock
},
{
provide: Router, useValue: routerMock
},
{
provide: ActivatedRoute, useValue: routeMock
}
],
// Tells the compiler not to error on unknown elements and attributes
schemas: [NO_ERRORS_SCHEMA]
});
codebase = {
"attributes": {
"type": "git",
"url": "https://github.com/fabric8io/fabric8-ui.git"
},
"type": "codebases"
} as Codebase;
codebases = [codebase];
codebasesServiceMock.getCodebases.and.returnValue(Observable.of(codebases));
fixture = TestBed.createComponent(CodebasesAddComponent);
});
it('Init component succesfully', async(() => {
// given
const comp = fixture.componentInstance;
const debug = fixture.debugElement;
const inputGitHubRepo = debug.query(By.css('#gitHubRepo'));
inputGitHubRepo.nativeElement.value = 'start'
inputGitHubRepo.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(comp.codebases).toEqual(codebases)
});
}));
it('Display github repo details after sync button pressed', async(() => {
// given
gitHubServiceMock.getRepoDetailsByFullName.and.returnValue(Observable.of(expectedGitHubRepoDetails));
gitHubServiceMock.getRepoLicenseByUrl.and.returnValue(Observable.of(expectedGitHubRepoLicense));
const debug = fixture.debugElement;
const inputGitHubRepo = debug.query(By.css('#gitHubRepo'));
const syncButton = debug.query(By.css('#syncButton'));
const form = debug.query(By.css('form'));
fixture.detectChanges();
fixture.whenStable().then(() => {
// when github repos added and sync button clicked
inputGitHubRepo.nativeElement.value = 'TestSpace/toto';
inputGitHubRepo.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
}).then(() => {
syncButton.nativeElement.click();
fixture.detectChanges();
}).then(() => {
expect(form.nativeElement.querySelector('#created').value).toBeTruthy();
expect(form.nativeElement.querySelector('#license').value).toEqual('Apache License 2.0');
});
}));
it('Display error after sync button pressed with invalid gitname', async(() => {
// given
gitHubServiceMock.getRepoDetailsByFullName.and.returnValue(Observable.of(expectedGitHubRepoDetails));
gitHubServiceMock.getRepoLicenseByUrl.and.returnValue(Observable.of(expectedGitHubRepoLicense));
const debug = fixture.debugElement;
const inputGitHubRepo = debug.query(By.css('#gitHubRepo'));
const syncButton = debug.query(By.css('#syncButton'));
fixture.detectChanges();
fixture.whenStable().then(() => {
// when github repos added and sync button clicked
inputGitHubRepo.nativeElement.value = 'TestSpace::toto';
inputGitHubRepo.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
}).then(() => {
syncButton.nativeElement.click();
fixture.detectChanges();
}).then(() => {
// then
const errorSpan = fixture.debugElement.query(By.css('.help-block'));
expect(errorSpan).toBeTruthy();
});
}));
it('Add codebase to space', async(() => {
// given
gitHubServiceMock.getRepoDetailsByFullName.and.returnValue(Observable.of(expectedGitHubRepoDetails));
gitHubServiceMock.getRepoLicenseByUrl.and.returnValue(Observable.of(expectedGitHubRepoLicense));
codebasesServiceMock.addCodebase.and.returnValue(Observable.of(codebase));
const notificationAction = {
id: "a",
isDisabled: false,
isSeparator: false,
name: "created",
title: "code base created"
};
notificationMock.message.and.returnValue(Observable.of(notificationAction));
const debug = fixture.debugElement;
const inputGitHubRepo = debug.query(By.css('#gitHubRepo'));
const syncButton = debug.query(By.css('#syncButton'));
fixture.detectChanges();
fixture.whenStable().then(() => {
// when github repos added and sync button cliked
inputGitHubRepo.nativeElement.value = 'TestSpace/toto';
inputGitHubRepo.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(() => {
syncButton.nativeElement.click();
fixture.detectChanges();
}).then(() => {
let addButton = fixture.debugElement.query(By.css('#associateButton'));
addButton.nativeElement.click();
fixture.detectChanges();
}).then(() => {
fixture.whenStable().then(() => {
// then
expect(notificationMock.message).toHaveBeenCalled();
});
});
});
}));
});