/
edit-list-item.component.spec.ts
230 lines (185 loc) · 11.5 KB
/
edit-list-item.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
import { Component, DebugElement, ViewChild } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ApiResponseData, CreateChildNodeRequest, ListNodeInfoResponse, ListsEndpointAdmin, UpdateChildNodeRequest } from '@dasch-swiss/dsp-js';
import { DspActionModule, DspApiConnectionToken, ProgressIndicatorComponent } from '@dasch-swiss/dsp-ui';
import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import { AjaxResponse } from 'rxjs/ajax';
import { EditListItemComponent } from './edit-list-item.component';
/**
* test host component to simulate parent component for updating an existing child node.
*/
@Component({
template: '<app-edit-list-item #editListItem [iri]="iri" [mode]="mode" [projectIri]="projectIri"></app-edit-list-item>'
})
class TestHostUpdateChildNodeComponent {
@ViewChild('editListItem') editListItem: EditListItemComponent;
iri = 'http://rdfh.ch/lists/0001/otherTreeList01';
mode = 'update';
projectIri = 'http://rdfh.ch/projects/0001';
constructor() {}
}
/**
* test host component to simulate parent component for inserting a new child node.
*/
@Component({
template: '<app-edit-list-item #editListItem [iri]="iri" [mode]="mode" [parentIri]="parentIri" [position]="position" [projectCode]="projectCode" [projectIri]="projectIri"></app-edit-list-item>'
})
class TestHostInsertChildNodeComponent {
@ViewChild('editListItem') editListItem: EditListItemComponent;
iri = 'http://rdfh.ch/lists/0001/otherTreeList01';
mode = 'insert';
parentIri = 'http://rdfh.ch/lists/0001/otherTreeList';
position = 0;
projectCode = '0001';
projectIri = 'http://rdfh.ch/projects/0001';
constructor() {}
}
describe('EditListItemComponent', () => {
let testHostComponent: TestHostUpdateChildNodeComponent;
let testHostFixture: ComponentFixture<TestHostUpdateChildNodeComponent>;
let editListItemComponentDe: DebugElement;
let formInvalidMessageDe: DebugElement;
beforeEach(async(() => {
const listsEndpointSpyObj = {
admin: {
listsEndpoint: jasmine.createSpyObj('listsEndpoint', ['getListNodeInfo', 'updateChildNode', 'createChildNode'])
}
};
TestBed.configureTestingModule({
declarations: [
EditListItemComponent,
TestHostUpdateChildNodeComponent,
TestHostInsertChildNodeComponent,
ProgressIndicatorComponent,
],
imports: [
BrowserAnimationsModule,
DspActionModule,
TranslateModule.forRoot()
],
providers: [
{
provide: DspApiConnectionToken,
useValue: listsEndpointSpyObj
}
]
})
.compileComponents();
}));
describe('update list child node', () => {
beforeEach(() => {
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
(dspConnSpy.admin.listsEndpoint as jasmine.SpyObj<ListsEndpointAdmin>).getListNodeInfo.and.callFake(
() => {
const response = new ListNodeInfoResponse();
response.nodeinfo.id = 'http://rdfh.ch/lists/0001/otherTreeList01';
response.nodeinfo.labels = [{ 'value': 'Tree list node 01', 'language': 'en' }];
response.nodeinfo.comments = [{ 'value': 'My comment', 'language': 'en' }];
return of(ApiResponseData.fromAjaxResponse({ response } as AjaxResponse));
}
);
testHostFixture = TestBed.createComponent(TestHostUpdateChildNodeComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
const hostCompDe = testHostFixture.debugElement;
editListItemComponentDe = hostCompDe.query(By.directive(EditListItemComponent));
expect(editListItemComponentDe).toBeTruthy();
});
it('should assign labels and comments', () => {
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
expect(testHostComponent.editListItem.labels).toEqual([{ 'value': 'Tree list node 01', 'language': 'en' }]);
expect(testHostComponent.editListItem.comments).toEqual([{ 'value': 'My comment', 'language': 'en' }]);
expect(dspConnSpy.admin.listsEndpoint.getListNodeInfo).toHaveBeenCalledTimes(1);
expect(dspConnSpy.admin.listsEndpoint.getListNodeInfo).toHaveBeenCalledWith('http://rdfh.ch/lists/0001/otherTreeList01');
});
it('should update labels when the value changes', () => {
expect(testHostComponent.editListItem.labels).toEqual([{ 'value': 'Tree list node 01', 'language': 'en' }]);
testHostComponent.editListItem.handleData([{ 'value': 'Tree list node 01', 'language': 'en' }, { 'value': 'Baumlistenknoten 01', 'language': 'de' }], 'labels');
expect(testHostComponent.editListItem.labels).toEqual([{ 'value': 'Tree list node 01', 'language': 'en' }, { 'value': 'Baumlistenknoten 01', 'language': 'de' }]);
expect(testHostComponent.editListItem.saveButtonDisabled).toBeFalsy();
testHostComponent.editListItem.handleData([], 'labels');
expect(testHostComponent.editListItem.saveButtonDisabled).toBeTruthy();
testHostFixture.detectChanges();
formInvalidMessageDe = editListItemComponentDe.query(By.css('mat-hint.invalid-form'));
expect(formInvalidMessageDe.nativeElement.innerText).toEqual('A label is required.');
});
it('should update comments when the value changes', () => {
expect(testHostComponent.editListItem.comments).toEqual([{ 'value': 'My comment', 'language': 'en' }]);
testHostComponent.editListItem.handleData([{ 'value': 'My comment', 'language': 'en' }, { 'value': 'Mein Kommentar', 'language': 'de' }], 'comments');
expect(testHostComponent.editListItem.comments).toEqual([{ 'value': 'My comment', 'language': 'en' }, { 'value': 'Mein Kommentar', 'language': 'de' }]);
expect(testHostComponent.editListItem.saveButtonDisabled).toBeFalsy();
testHostComponent.editListItem.handleData([], 'comments');
expect(testHostComponent.editListItem.saveButtonDisabled).toBeFalsy();
});
it('should update the child node info', () => {
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
testHostComponent.editListItem.handleData([{ 'value': 'Tree list node 01', 'language': 'en' }, { 'value': 'Baumlistenknoten 01', 'language': 'de' }], 'labels');
testHostComponent.editListItem.handleData([{ 'value': 'My comment', 'language': 'en' }, { 'value': 'Mein Kommentar', 'language': 'de' }], 'comments');
(dspConnSpy.admin.listsEndpoint as jasmine.SpyObj<ListsEndpointAdmin>).updateChildNode.and.callFake(
() => {
const response = new ListNodeInfoResponse();
response.nodeinfo.id = 'http://rdfh.ch/lists/0001/otherTreeList01';
response.nodeinfo.labels = [{ 'value': 'Tree list node 01', 'language': 'en' }, { 'value': 'Baumlistenknoten 01', 'language': 'de' }];
response.nodeinfo.comments = [{ 'value': 'My comment', 'language': 'en' }, { 'value': 'Mein Kommentar', 'language': 'de' }];
expect(testHostComponent.editListItem.labels).toEqual(response.nodeinfo.labels);
expect(testHostComponent.editListItem.comments).toEqual(response.nodeinfo.comments);
return of(ApiResponseData.fromAjaxResponse({ response } as AjaxResponse));
}
);
const updateChildNodeRequest: UpdateChildNodeRequest = new UpdateChildNodeRequest();
updateChildNodeRequest.projectIri = testHostComponent.editListItem.projectIri;
updateChildNodeRequest.listIri = testHostComponent.editListItem.iri;
updateChildNodeRequest.labels = testHostComponent.editListItem.labels;
updateChildNodeRequest.comments = testHostComponent.editListItem.comments;
testHostComponent.editListItem.updateChildNode();
expect(dspConnSpy.admin.listsEndpoint.updateChildNode).toHaveBeenCalledTimes(1);
expect(dspConnSpy.admin.listsEndpoint.updateChildNode).toHaveBeenCalledWith(updateChildNodeRequest);
});
});
describe('insert list child node', () => {
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostInsertChildNodeComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
const hostCompDe = testHostFixture.debugElement;
editListItemComponentDe = hostCompDe.query(By.directive(EditListItemComponent));
expect(editListItemComponentDe).toBeTruthy();
});
it('should instantiate empty arrays for labels and comments', () => {
expect(testHostComponent.editListItem.labels).toEqual([]);
expect(testHostComponent.editListItem.comments).toEqual([]);
});
it('should create (insert) a new child node', () => {
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
testHostComponent.editListItem.handleData([{ 'value': 'My new child node value', 'language': 'en' }], 'labels');
testHostComponent.editListItem.handleData([{ 'value': 'My new child node comment', 'language': 'en' }], 'comments');
(dspConnSpy.admin.listsEndpoint as jasmine.SpyObj<ListsEndpointAdmin>).createChildNode.and.callFake(
() => {
const response = new ListNodeInfoResponse();
response.nodeinfo.name = 'My new child node';
response.nodeinfo.id = 'http://rdfh.ch/lists/0001/otherTreeList0123';
response.nodeinfo.labels = [{ 'value': 'My new child node value', 'language': 'en' }];
response.nodeinfo.comments = [{ 'value': 'My new child node comment', 'language': 'en' }];
response.nodeinfo.position = 0;
expect(testHostComponent.editListItem.labels).toEqual(response.nodeinfo.labels);
expect(testHostComponent.editListItem.comments).toEqual(response.nodeinfo.comments);
expect(testHostComponent.editListItem.position).toEqual(response.nodeinfo.position);
return of(ApiResponseData.fromAjaxResponse({ response } as AjaxResponse));
}
);
const createChildNodeRequest: CreateChildNodeRequest = new CreateChildNodeRequest();
createChildNodeRequest.projectIri = testHostComponent.editListItem.projectIri;
createChildNodeRequest.labels = testHostComponent.editListItem.labels;
createChildNodeRequest.comments = testHostComponent.editListItem.comments;
createChildNodeRequest.position = testHostComponent.editListItem.position;
createChildNodeRequest.parentNodeIri = testHostComponent.editListItem.parentIri;
testHostComponent.editListItem.insertChildNode();
expect(dspConnSpy.admin.listsEndpoint.createChildNode).toHaveBeenCalledTimes(1);
});
});
});