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
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {JobspecsComponent} from './jobspecs.component';
import {of, ReplaySubject} from "rxjs";
import {ActivatedRoute, convertToParamMap, ParamMap, Params} from "@angular/router";
import {Job} from "../model/job";
import {Component, DebugElement, Input} from "@angular/core";
import {JobsService} from "../services/jobs.service";
import {ReactiveFormsModule} from "@angular/forms";
import {By} from "@angular/platform-browser";
// mock the child component
@Component({
selector: 'app-job',
template: '<p>Mock child component</p>'
})
class MockChildComponent {
@Input() job: Job;
}
// mock the ActivatedRoute
class MockActivatedRoute {
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.paramsSubject.next(params);
this.paramMapSubject.next(convertToParamMap(params));
}
setQueryParams(params: Params) {
this.queryParamsSubject.next(params);
this.queryParamMapSubject.next(convertToParamMap(params));
}
}
describe('JobspecsComponent', () => {
let component: JobspecsComponent;
let fixture: ComponentFixture<JobspecsComponent>;
let jobServiceSpy: { getJobs: jasmine.Spy };
let mockActivatedRoute: MockActivatedRoute;
beforeEach(async(() => {
jobServiceSpy = jasmine.createSpyObj('JobService', ['getJobs']);
TestBed.configureTestingModule({
declarations: [JobspecsComponent, MockChildComponent],
providers: [
{provide: JobsService, useValue: jobServiceSpy},
{provide: ActivatedRoute, useValue: new MockActivatedRoute()}
],
imports: [ReactiveFormsModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(JobspecsComponent);
component = fixture.componentInstance;
jobServiceSpy = TestBed.get(JobsService);
mockActivatedRoute = TestBed.get(ActivatedRoute);
jobServiceSpy.getJobs.and.returnValue(of([]));
});
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 queue when queue query parameter exists', fakeAsync(() => {
mockActivatedRoute.setQueryParams({queue: 'quicklook'});
expect(component.queue).toEqual('calibration', 'queue starts as calibration');
fixture.detectChanges(); // ngOnInit
expect(component.queue).toEqual('quicklook', 'queue 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);
}));
});