Newer
Older
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
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {ExecutionsComponent} from './executions.component';
import {Component, DebugElement, Input} from "@angular/core";
import {Job} from "../model/job";
import {ReplaySubject} from "rxjs";
import {ActivatedRoute, convertToParamMap, ParamMap, Params} from "@angular/router";
import {JobsService} from "../services/jobs.service";
import {ReactiveFormsModule} from "@angular/forms";
import {By} from "@angular/platform-browser";
//mock the child
@Component({
selector: 'app-qa-job',
template: '<p>Mock child component</p>'
})
class MockChildComponent {
@Input() job: Job;
@Input() queue: string;
}
// mock the ActivatedRoute
class MockActivatedRoute {
private _params: Params;
private _paramMap: ParamMap;
private _queryParams: Params;
private _queryParamMap: ParamMap;
private paramsSubject = new ReplaySubject<Params>();
private paramMapSubject = new ReplaySubject<ParamMap>();
private queryParamsSubject = new ReplaySubject<Params>();
private queryParamMapSubject = new ReplaySubject<ParamMap>();
readonly params = this.paramsSubject.asObservable();
readonly paramMap = this.paramMapSubject.asObservable();
readonly queryParams = this.queryParamsSubject.asObservable();
readonly queryParamMap = this.queryParamMapSubject.asObservable();
setParams(params: Params) {
this._params = params;
this._paramMap = convertToParamMap(params);
this.paramsSubject.next(params);
this.paramMapSubject.next(convertToParamMap(params));
}
setQueryParams(params: Params) {
this._queryParams = params;
this._queryParamMap = convertToParamMap(params);
this.queryParamsSubject.next(params);
this.queryParamMapSubject.next(convertToParamMap(params));
}
}
describe('ExecutionsComponent', () => {
let component: ExecutionsComponent;
let fixture: ComponentFixture<ExecutionsComponent>;
let jobServiceSpy: { getJobs: jasmine.Spy };
let mockActivatedRoute: MockActivatedRoute;
beforeEach(async(() => {
jobServiceSpy = jasmine.createSpyObj('JobsService', ['getJobs']);
TestBed.configureTestingModule({
declarations: [ExecutionsComponent, MockChildComponent],
providers: [
{provide: JobsService, useValue: jobServiceSpy},
{provide: ActivatedRoute, useValue: new MockActivatedRoute()}
],
imports: [ReactiveFormsModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ExecutionsComponent);
component = fixture.componentInstance;
jobServiceSpy = TestBed.get(JobsService);
mockActivatedRoute = TestBed.get(ActivatedRoute);
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call for jobs on init', fakeAsync(() => {
fixture.detectChanges(); //ngOnInit
expect(jobServiceSpy.getJobs).toHaveBeenCalled();
}));
it('should set pattern when pattern query parameter exists', fakeAsync(() => {
mockActivatedRoute.setQueryParams({pattern: 'T01'});
expect(component.pattern).toEqual('', 'pattern starts empty');
fixture.detectChanges(); // ngInit
expect(component.pattern).toEqual('T01', 'pattern set from query param');
}));
it('should set status when status query parameter exists', fakeAsync(() => {
mockActivatedRoute.setQueryParams({status: 'PROCESSING'});
expect(component.status).toEqual('ALL', 'status starts as ALL');
fixture.detectChanges(); // ngOnInit
expect(component.status).toEqual('PROCESSING', 'status set from query param');
}));
it('should update pattern and call getJobs() with pattern formcontrol changes', fakeAsync(() => {
let pattern_inputDe: DebugElement = fixture.debugElement.query(By.css('#pattern'));
let pattern_inputEl: HTMLInputElement = pattern_inputDe.nativeElement;
let getJobsSpy = spyOn(component, "getJobs");
expect(component.pattern).toEqual('', 'pattern starts empty');
fixture.detectChanges(); // ngInit
expect(getJobsSpy).toHaveBeenCalledTimes(1);
expect(pattern_inputEl.value).toEqual('', 'pattern control is null');
pattern_inputEl.value = 'T01';
pattern_inputEl.dispatchEvent(new Event('input'));
tick(500); // for the debounce
fixture.detectChanges();
expect(component.pattern).toEqual('T01', 'input updated pattern');
expect(getJobsSpy).toHaveBeenCalledTimes(2);
}));
it('should update queue and call getJobs() when setQueue() is called', fakeAsync(() => {
let getJobsSpy = spyOn(component, "getJobs");
expect(component.queue).toEqual('calibration', 'queue starts as calibration');
fixture.detectChanges(); //ngOnInit
expect(getJobsSpy).toHaveBeenCalledTimes(1);
component.setQueue('quicklook');
fixture.detectChanges();
expect(component.queue).toEqual('quicklook', 'queue updated');
expect(getJobsSpy).toHaveBeenCalledTimes(2);
}));
it('should update status and call getJobs() when setStatus() is called', fakeAsync(() => {
let getJobsSpy = spyOn(component, "getJobs");
expect(component.status).toEqual('ALL', 'status starts as ALL');
fixture.detectChanges(); //ngOnInit
expect(getJobsSpy).toHaveBeenCalledTimes(1);
component.setStatus('PROCESSING');
fixture.detectChanges();
expect(component.status).toEqual('PROCESSING', 'status updated');
expect(getJobsSpy).toHaveBeenCalledTimes(2);
}));
});