-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathsearch-form.component.spec.ts
More file actions
311 lines (274 loc) · 9.57 KB
/
search-form.component.spec.ts
File metadata and controls
311 lines (274 loc) · 9.57 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { DebugElement } from '@angular/core';
import {
ComponentFixture,
fakeAsync,
TestBed,
tick,
waitForAsync,
} from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { DSpaceObjectDataService } from '@dspace/core/data/dspace-object-data.service';
import { PaginationService } from '@dspace/core/pagination/pagination.service';
import { Community } from '@dspace/core/shared/community.model';
import { DSpaceObject } from '@dspace/core/shared/dspace-object.model';
import { PaginationServiceStub } from '@dspace/core/testing/pagination-service.stub';
import { RouterStub } from '@dspace/core/testing/router.stub';
import { SearchFilterServiceStub } from '@dspace/core/testing/search-filter-service.stub';
import { SearchServiceStub } from '@dspace/core/testing/search-service.stub';
import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils';
import { TranslateModule } from '@ngx-translate/core';
import { SearchService } from '../search/search.service';
import { SearchConfigurationService } from '../search/search-configuration.service';
import { SearchFilterService } from '../search/search-filters/search-filter.service';
import { SearchFormComponent } from './search-form.component';
describe('SearchFormComponent', () => {
let comp: SearchFormComponent;
let fixture: ComponentFixture<SearchFormComponent>;
let de: DebugElement;
const router = new RouterStub();
const searchService = new SearchServiceStub();
let searchFilterService: SearchFilterServiceStub;
const paginationService = new PaginationServiceStub();
const searchConfigService = { paginationID: 'test-id' };
const firstPage = { 'spc.page': 1 };
const dspaceObjectService = {
findById: () => createSuccessfulRemoteDataObject$(undefined),
};
beforeEach(waitForAsync(() => {
searchFilterService = new SearchFilterServiceStub();
return TestBed.configureTestingModule({
imports: [FormsModule, RouterTestingModule, TranslateModule.forRoot(), SearchFormComponent],
providers: [
{ provide: Router, useValue: router },
{ provide: SearchService, useValue: searchService },
{ provide: SearchFilterService, useValue: searchFilterService },
{ provide: PaginationService, useValue: paginationService },
{ provide: SearchConfigurationService, useValue: searchConfigService },
{ provide: DSpaceObjectDataService, useValue: dspaceObjectService },
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchFormComponent);
comp = fixture.componentInstance; // SearchFormComponent test instance
de = fixture.debugElement.query(By.css('form'));
});
it('should not display scopes when showScopeSelector is false', fakeAsync(() => {
comp.showScopeSelector = false;
fixture.detectChanges();
tick();
expect(de.query(By.css('.scope-button'))).toBeFalsy();
}));
it('should display scopes when showScopeSelector is true', fakeAsync(() => {
comp.showScopeSelector = true;
fixture.detectChanges();
tick();
expect(de.query(By.css('.scope-button'))).toBeTruthy();
}));
it('should display set query value in input field', fakeAsync(() => {
const testString = 'This is a test query';
comp.query = testString;
fixture.detectChanges();
tick();
const queryInput = de.query(By.css('input[name="query"]')).nativeElement;
expect(queryInput.value).toBe(testString);
}));
it('should select correct scope option in scope select', fakeAsync(() => {
fixture.detectChanges();
comp.showScopeSelector = true;
const testCommunity = objects[1];
comp.selectedScope.next(testCommunity);
fixture.detectChanges();
tick();
const scopeSelect = de.query(By.css('.scope-button')).nativeElement;
expect(scopeSelect.textContent).toContain('Sample Community');
}));
describe('updateSearch', () => {
const query = 'THOR';
const scope = 'MCU';
let searchQuery = {};
it('should navigate to the search first page even when no parameters are provided', () => {
comp.updateSearch(searchQuery);
expect(router.navigate).toHaveBeenCalledWith(comp.getSearchLinkParts(), {
queryParams: { ...searchQuery, ...firstPage },
queryParamsHandling: 'merge',
});
});
it('should navigate to the search first page with parameters only query if only query is provided', () => {
searchQuery = {
query: query,
};
comp.updateSearch(searchQuery);
expect(router.navigate).toHaveBeenCalledWith(comp.getSearchLinkParts(), {
queryParams: { ...searchQuery, ...firstPage },
queryParamsHandling: 'merge',
});
});
it('should navigate to the search first page with parameters only query if only scope is provided', () => {
searchQuery = {
scope: scope,
};
comp.updateSearch(searchQuery);
expect(router.navigate).toHaveBeenCalledWith(comp.getSearchLinkParts(), {
queryParams: { ...searchQuery, ...firstPage },
queryParamsHandling: 'merge',
});
});
});
describe('when the scope variable is used', () => {
const query = 'THOR';
const scope = 'MCU';
let searchQuery = {};
beforeEach(() => {
spyOn(comp, 'updateSearch');
});
it('should only search in the provided scope', () => {
searchQuery = {
query: query,
scope: scope,
};
comp.scope = scope;
comp.onSubmit(searchQuery);
expect(comp.updateSearch).toHaveBeenCalledWith(searchQuery);
});
it('should not create searchQuery with the scope if an empty scope is provided', () => {
searchQuery = {
query: query,
};
comp.scope = '';
comp.onSubmit(searchQuery);
expect(comp.updateSearch).toHaveBeenCalledWith(searchQuery);
});
});
});
const objects: DSpaceObject[] = [
Object.assign(new Community(), {
logo: {
self: {
_isScalar: true,
value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/10b636d0-7890-4968-bcd6-0d83bf4e2b42',
scheduler: null,
},
},
collections: {
self: {
_isScalar: true,
value: '1506937433727',
scheduler: null,
},
},
_links: {
self: {
href: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/7669c72a-3f2a-451f-a3b9-9210e7a4c02f',
},
},
id: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f',
uuid: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f',
type: Community.type,
metadata: {
'dc.description': [
{
language: null,
value: '',
},
],
'dc.description.abstract': [
{
language: null,
value: 'This is a test community to hold content for the OR2017 demostration',
},
],
'dc.description.tableofcontents': [
{
language: null,
value: '',
},
],
'dc.rights': [
{
language: null,
value: '',
},
],
'dc.title': [
{
language: null,
value: 'OR2017 - Demonstration',
},
],
'dc.identifier.uri': [
{
language: null,
value: 'http://localhost:4000/handle/10673/11',
},
],
},
}),
Object.assign(new Community(),
{
logo: {
self: {
_isScalar: true,
value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/f446c17d-6d51-45ea-a610-d58a73642d40',
scheduler: null,
},
},
collections: {
self: {
_isScalar: true,
value: '1506937433727',
scheduler: null,
},
},
_links: {
self: {
href: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/9076bd16-e69a-48d6-9e41-0238cb40d863',
},
},
id: '9076bd16-e69a-48d6-9e41-0238cb40d863',
uuid: '9076bd16-e69a-48d6-9e41-0238cb40d863',
type: Community.type,
metadata: {
'dc.description': [
{
language: null,
value: '<p>This is the introductory text for the <em>Sample Community</em> on the DSpace Demonstration Site. It is editable by System or Community Administrators (of this Community).</p>\r\n<p><strong>DSpace Communities may contain one or more Sub-Communities or Collections (of Items).</strong></p>\r\n<p>This particular Community has its own logo (the <a href=\'http://www.duraspace.org/\'>DuraSpace</a> logo).</p>',
},
],
'dc.description.abstract': [
{
language: null,
value: 'This is a sample top-level community',
},
],
'dc.description.tableofcontents': [
{
language: null,
value: '<p>This is the <em>news section</em> for this <em>Sample Community</em>. System or Community Administrators (of this Community) can edit this News field.</p>',
},
],
'dc.rights': [
{
language: null,
value: '<p><em>If this Community had special copyright text to display, it would be displayed here.</em></p>',
},
],
'dc.title': [
{
language: null,
value: 'Sample Community',
},
],
'dc.identifier.uri': [
{
language: null,
value: 'http://localhost:4000/handle/10673/1',
},
],
},
},
),
];