/
expert-search.component.spec.ts
256 lines (197 loc) · 7.86 KB
/
expert-search.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { Component, DebugElement, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatDialogModule } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { KnoraApiConfig } from '@dasch-swiss/dsp-js';
import { DspApiConfigToken } from 'src/app/main/declarations/dsp-api-tokens';
import { SearchParams } from '../../results/list-view/list-view.component';
import { AdvancedSearchParams, AdvancedSearchParamsService } from '../services/advanced-search-params.service';
import { ExpertSearchComponent } from './expert-search.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-expert-search #expSearch (search)="gravsearchQuery($event)"></app-expert-search>`
})
class TestHostComponent implements OnInit {
@ViewChild('expSearch') expertSearch: ExpertSearchComponent;
gravsearchQ: SearchParams;
ngOnInit() {
}
gravsearchQuery(query: SearchParams) {
this.gravsearchQ = query;
}
}
describe('ExpertSearchComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
let hostCompDe: DebugElement;
let searchParamsServiceSpy: jasmine.SpyObj<AdvancedSearchParamsService>;
let advancedSearchParams: AdvancedSearchParams;
beforeEach(waitForAsync(() => {
const dspConfSpy = new KnoraApiConfig('http', 'localhost', 3333, undefined, undefined, true);
const spy = jasmine.createSpyObj('SearchParamsService', ['changeSearchParamsMsg']);
TestBed.configureTestingModule({
declarations: [
ExpertSearchComponent,
TestHostComponent
],
imports: [
BrowserAnimationsModule,
FormsModule,
MatDialogModule,
MatFormFieldModule,
MatInputModule,
ReactiveFormsModule
],
providers: [
{
provide: DspApiConfigToken,
useValue: dspConfSpy
},
{
provide: AdvancedSearchParamsService,
useValue: spy
}
]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
searchParamsServiceSpy = TestBed.inject(AdvancedSearchParamsService) as jasmine.SpyObj<AdvancedSearchParamsService>;
searchParamsServiceSpy.changeSearchParamsMsg.and.callFake((searchParams: AdvancedSearchParams) => {
advancedSearchParams = searchParams;
});
testHostFixture.detectChanges();
hostCompDe = testHostFixture.debugElement;
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.expertSearch).toBeTruthy();
});
it('should init the form with the default query', () => {
const textarea = hostCompDe.query(By.css('textarea.textarea-field-content'));
const textareaEle = textarea.nativeElement;
expect(textareaEle.value).toBe(
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
`
);
});
it('should reset the form', () => {
const resetBtn = hostCompDe.query(By.css('button.reset'));
const textarea = hostCompDe.query(By.css('textarea.textarea-field-content'));
const resetEle = resetBtn.nativeElement;
const textareaEle = textarea.nativeElement;
// delete textarea content displayed by default to make a change
textareaEle.value = '';
expect(textareaEle.value).toBe('');
resetEle.click();
testHostFixture.detectChanges();
// reset the textarea content
expect(textareaEle.value).toBe(
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
`
);
});
it('should register the query in the params service', () => {
const expectedGravsearch =
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
OFFSET 0
`
;
const submitBtn = hostCompDe.query(By.css('button[type="submit"]'));
const submitBtnEle = submitBtn.nativeElement;
submitBtnEle.click();
testHostFixture.detectChanges();
expect(searchParamsServiceSpy.changeSearchParamsMsg).toHaveBeenCalledTimes(1);
expect(advancedSearchParams).toBeDefined();
expect(advancedSearchParams.generateGravsearch(0)).toEqual(expectedGravsearch);
});
it('should emit the Gravsearch query', () => {
const expectedGravsearch =
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
OFFSET 0
`;
const submitBtn = hostCompDe.query(By.css('button[type="submit"]'));
const submitBtnEle = submitBtn.nativeElement;
expect(testHostComponent.gravsearchQ).toBeUndefined();
submitBtnEle.click();
testHostFixture.detectChanges();
expect(testHostComponent.gravsearchQ).toBeDefined();
expect(testHostComponent.gravsearchQ.query).toEqual(expectedGravsearch);
expect(testHostComponent.gravsearchQ.mode).toEqual('gravsearch');
});
it('should not return an invalid query', () => {
expect(testHostComponent.expertSearch.expertSearchForm.valid).toBeTruthy();
const textarea = hostCompDe.query(By.css('textarea.textarea-field-content'));
const textareaEle = textarea.nativeElement;
expect(textareaEle.value).toBe(
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
`
);
textareaEle.value =
`PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
PREFIX incunabula: <http://localhost:3333/ontology/0803/incunabula/v2#>
CONSTRUCT {
?book knora-api:isMainResource true .
?book incunabula:title ?title .
} WHERE {
?book a incunabula:book .
?book incunabula:title ?title .
}
OFFSET 0
`;
textareaEle.dispatchEvent(new Event('input'));
testHostFixture.detectChanges();
expect(testHostComponent.expertSearch.expertSearchForm.valid).toBeFalsy();
const submitForm = testHostComponent.expertSearch.submitQuery();
expect(submitForm).toBeFalsy();
});
});