forked from UoEMainLibrary/dspace-datashare-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-export-csv.component.spec.ts
More file actions
222 lines (197 loc) · 8.75 KB
/
Copy pathsearch-export-csv.component.spec.ts
File metadata and controls
222 lines (197 loc) · 8.75 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
import {
ComponentFixture,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core';
import { of as observableOf } from 'rxjs';
import { ConfigurationDataService } from '../../../core/data/configuration-data.service';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { ScriptDataService } from '../../../core/data/processes/script-data.service';
import { getProcessDetailRoute } from '../../../process-page/process-page-routing.paths';
import { Process } from '../../../process-page/processes/process.model';
import { NotificationsService } from '../../notifications/notifications.service';
import {
createFailedRemoteDataObject$,
createSuccessfulRemoteDataObject$,
} from '../../remote-data.utils';
import { NotificationsServiceStub } from '../../testing/notifications-service.stub';
import { PaginatedSearchOptions } from '../models/paginated-search-options.model';
import { SearchFilter } from '../models/search-filter.model';
import { SearchExportCsvComponent } from './search-export-csv.component';
describe('SearchExportCsvComponent', () => {
let component: SearchExportCsvComponent;
let fixture: ComponentFixture<SearchExportCsvComponent>;
let scriptDataService: ScriptDataService;
let authorizationDataService: AuthorizationDataService;
let notificationsService;
let router;
let configurationDataService: jasmine.SpyObj<ConfigurationDataService>;
const process = Object.assign(new Process(), { processId: 5, scriptName: 'metadata-export-search' });
const searchConfig = new PaginatedSearchOptions({
configuration: 'test-configuration',
scope: 'test-scope',
query: 'test-query',
filters: [
new SearchFilter('f.filter1', ['filter1value1,equals', 'filter1value2,equals']),
new SearchFilter('f.filter2', ['filter2value1,contains']),
new SearchFilter('f.filter3', ['[2000 TO 2001]'], 'equals'),
],
});
configurationDataService = jasmine.createSpyObj('ConfigurationDataService', {
findByPropertyName: observableOf({ payload: { value: '500' } }),
});
function initBeforeEachAsync() {
scriptDataService = jasmine.createSpyObj('scriptDataService', {
scriptWithNameExistsAndCanExecute: observableOf(true),
invoke: createSuccessfulRemoteDataObject$(process),
});
authorizationDataService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true),
});
notificationsService = new NotificationsServiceStub();
router = jasmine.createSpyObj('authorizationService', ['navigateByUrl']);
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), NgbModule, SearchExportCsvComponent],
providers: [
{ provide: ScriptDataService, useValue: scriptDataService },
{ provide: AuthorizationDataService, useValue: authorizationDataService },
{ provide: NotificationsService, useValue: notificationsService },
{ provide: Router, useValue: router },
{ provide: ConfigurationDataService, useValue: configurationDataService },
],
}).compileComponents();
}
function initBeforeEach() {
fixture = TestBed.createComponent(SearchExportCsvComponent);
component = fixture.componentInstance;
component.searchConfig = searchConfig;
fixture.detectChanges();
}
describe('init', () => {
describe('comp', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
}));
beforeEach(() => {
initBeforeEach();
});
it('should init the comp', () => {
expect(component).toBeTruthy();
});
});
describe('when the user is an admin and the metadata-export-search script is present ', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
}));
beforeEach(() => {
initBeforeEach();
});
it('should add the button', () => {
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
expect(debugElement).toBeDefined();
});
});
describe('when the user is not an admin', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
(authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
}));
beforeEach(() => {
initBeforeEach();
});
it('should not add the button', () => {
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
expect(debugElement).toBeNull();
});
});
describe('when the metadata-export-search script is not present', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
(scriptDataService.scriptWithNameExistsAndCanExecute as jasmine.Spy).and.returnValue(observableOf(false));
}));
it('should should not add the button', () => {
initBeforeEach();
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
expect(debugElement).toBeNull();
});
it('should not call scriptWithNameExistsAndCanExecute when unauthorized', () => {
(authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
initBeforeEach();
expect(scriptDataService.scriptWithNameExistsAndCanExecute).not.toHaveBeenCalled();
});
});
});
describe('bulkedit.export.max.items probe', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
}));
it('should NOT probe bulkedit.export.max.items when the export button is not shown', () => {
// Non-admin: the button (and therefore the export-limit warning) is never rendered,
// so the config property must not be requested - this is what avoids the per-page 404.
(authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
(configurationDataService.findByPropertyName as jasmine.Spy).calls.reset();
initBeforeEach();
expect(configurationDataService.findByPropertyName).not.toHaveBeenCalledWith('bulkedit.export.max.items');
});
it('should probe bulkedit.export.max.items once the export button is shown', () => {
// Admin with the export script available: the warning is evaluated, so the limit is fetched.
(configurationDataService.findByPropertyName as jasmine.Spy).calls.reset();
initBeforeEach();
expect(configurationDataService.findByPropertyName).toHaveBeenCalledWith('bulkedit.export.max.items');
});
});
describe('export', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
}));
beforeEach(() => {
initBeforeEach();
});
it('should call the invoke script method with the correct parameters', () => {
component.export();
expect(scriptDataService.invoke).toHaveBeenCalledWith('metadata-export-search',
[
{ name: '-q', value: searchConfig.query },
{ name: '-s', value: searchConfig.scope },
{ name: '-c', value: searchConfig.configuration },
{ name: '-f', value: 'filter1,equals=filter1value1' },
{ name: '-f', value: 'filter1,equals=filter1value2' },
{ name: '-f', value: 'filter2,contains=filter2value1' },
{ name: '-f', value: 'filter3,equals=[2000 TO 2001]' },
], []);
component.searchConfig = null;
fixture.detectChanges();
component.export();
expect(scriptDataService.invoke).toHaveBeenCalledWith('metadata-export-search', [], []);
});
it('should show a success message when the script was invoked successfully and redirect to the corresponding process page', () => {
component.export();
expect(notificationsService.success).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalledWith(getProcessDetailRoute(process.processId));
});
it('should show an error message when the script was not invoked successfully and stay on the current page', () => {
(scriptDataService.invoke as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Error', 500));
component.export();
expect(notificationsService.error).toHaveBeenCalled();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
});
describe('clicking the button', () => {
beforeEach(waitForAsync(() => {
initBeforeEachAsync();
}));
beforeEach(() => {
initBeforeEach();
});
it('should trigger the export function', () => {
spyOn(component, 'export');
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
debugElement.triggerEventHandler('click', null);
expect(component.export).toHaveBeenCalled();
});
});
});