forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curation-form.component.spec.ts
153 lines (132 loc) · 6.38 KB
/
curation-form.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
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CurationFormComponent } from './curation-form.component';
import { ScriptDataService } from '../core/data/processes/script-data.service';
import { ProcessDataService } from '../core/data/processes/process-data.service';
import { of as observableOf } from 'rxjs';
import { RequestEntry } from '../core/data/request.reducer';
import { DSOSuccessResponse, RestResponse } from '../core/cache/response.models';
import { Process } from '../process-page/processes/process.model';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
import { NotificationsServiceStub } from '../shared/testing/notifications-service.stub';
import { RouterStub } from '../shared/testing/router.stub';
import { NotificationsService } from '../shared/notifications/notifications.service';
import { Router } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { ConfigurationDataService } from '../core/data/configuration-data.service';
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
describe('CurationFormComponent', () => {
let comp: CurationFormComponent;
let fixture: ComponentFixture<CurationFormComponent>;
let scriptDataService: ScriptDataService;
let processDataService: ProcessDataService;
let configurationDataService: ConfigurationDataService;
let notificationsService;
let router;
const requestEntry = Object.assign(new RequestEntry(),
{response: new DSOSuccessResponse(['process-link'], 200, 'success')});
const failedRequestEntry = Object.assign(new RequestEntry(),
{response: new RestResponse(false, 400, 'Bad Request')});
const process = Object.assign(new Process(), {processId: 'process-id'});
beforeEach(async(() => {
scriptDataService = jasmine.createSpyObj('scriptDataService', {
invoke: observableOf(requestEntry)
});
processDataService = jasmine.createSpyObj('processDataService', {
findByHref: createSuccessfulRemoteDataObject$(process)
});
configurationDataService = jasmine.createSpyObj('configurationDataService', {
findByPropertyName: createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), {
name: 'plugin.named.org.dspace.curate.CurationTask',
values: [
'org.dspace.ctask.general.ProfileFormats = profileformats',
'',
'org.dspace.ctask.general.RequiredMetadata = requiredmetadata',
'org.dspace.ctask.general.MetadataValueLinkChecker = checklinks',
'value-to-be-skipped'
]
}))
});
notificationsService = new NotificationsServiceStub();
router = new RouterStub();
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), FormsModule, ReactiveFormsModule],
declarations: [CurationFormComponent],
providers: [
{provide: ScriptDataService, useValue: scriptDataService},
{provide: ProcessDataService, useValue: processDataService},
{provide: NotificationsService, useValue: notificationsService},
{provide: Router, useValue: router},
{provide: ConfigurationDataService, useValue: configurationDataService},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CurationFormComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
describe('init', () => {
it('should initialise the comp and contain the different tasks', () => {
expect(comp).toBeDefined();
const elements = fixture.debugElement.queryAll(By.css('option'));
expect(elements.length).toEqual(3);
expect(elements[0].nativeElement.innerHTML).toContain('curation-task.task.profileformats.label');
expect(elements[1].nativeElement.innerHTML).toContain('curation-task.task.requiredmetadata.label');
expect(elements[2].nativeElement.innerHTML).toContain('curation-task.task.checklinks.label');
});
});
describe('hasHandleValue', () => {
it('should return true when a dsoHandle value was provided', () => {
comp.dsoHandle = 'some-handle';
fixture.detectChanges();
expect(comp.hasHandleValue()).toBeTrue();
});
it('should return false when no dsoHandle value was provided', () => {
expect(comp.hasHandleValue()).toBeFalse();
});
});
describe('submit', () => {
it('should submit the selected process and handle to the scriptservice and navigate to the corresponding process page', () => {
comp.dsoHandle = 'test-handle';
comp.submit();
expect(scriptDataService.invoke).toHaveBeenCalledWith('curate', [
{name: '-t', value: 'profileformats'},
{name: '-i', value: 'test-handle'},
], []);
expect(notificationsService.success).toHaveBeenCalled();
expect(processDataService.findByHref).toHaveBeenCalledWith('process-link');
expect(router.navigate).toHaveBeenCalledWith(['/processes', 'process-id']);
});
it('should the selected process and handle to the scriptservice and stay on the page on error', () => {
(scriptDataService.invoke as jasmine.Spy).and.returnValue(observableOf(failedRequestEntry));
comp.dsoHandle = 'test-handle';
comp.submit();
expect(scriptDataService.invoke).toHaveBeenCalledWith('curate', [
{name: '-t', value: 'profileformats'},
{name: '-i', value: 'test-handle'},
], []);
expect(notificationsService.error).toHaveBeenCalled();
expect(processDataService.findByHref).not.toHaveBeenCalled();
expect(router.navigate).not.toHaveBeenCalled();
});
});
it('should use the handle provided by the form when no dsoHandle is provided', () => {
comp.form.get('handle').patchValue('form-handle');
comp.submit();
expect(scriptDataService.invoke).toHaveBeenCalledWith('curate', [
{name: '-t', value: 'profileformats'},
{name: '-i', value: 'form-handle'},
], []);
});
it('should use "all" when the handle provided by the form is empty and when no dsoHandle is provided', () => {
comp.submit();
expect(scriptDataService.invoke).toHaveBeenCalledWith('curate', [
{name: '-t', value: 'profileformats'},
{name: '-i', value: 'all'},
], []);
});
});