Skip to content

Commit d4c3004

Browse files
93747: DsoEditMetadataForm test cases
1 parent bf9612e commit d4c3004

1 file changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
import { DsoEditMetadataChangeType, DsoEditMetadataForm } from './dso-edit-metadata-form';
2+
import { DSpaceObject } from '../../core/shared/dspace-object.model';
3+
import { MetadataValue } from '../../core/shared/metadata.models';
4+
5+
describe('DsoEditMetadataForm', () => {
6+
let form: DsoEditMetadataForm;
7+
let dso: DSpaceObject;
8+
9+
beforeEach(() => {
10+
dso = Object.assign(new DSpaceObject(), {
11+
metadata: {
12+
'dc.title': [
13+
Object.assign(new MetadataValue(), {
14+
value: 'Test Title',
15+
language: 'en',
16+
place: 0,
17+
}),
18+
],
19+
'dc.subject': [
20+
Object.assign(new MetadataValue(), {
21+
value: 'Subject One',
22+
language: 'en',
23+
place: 0,
24+
}),
25+
Object.assign(new MetadataValue(), {
26+
value: 'Subject Two',
27+
language: 'en',
28+
place: 1,
29+
}),
30+
Object.assign(new MetadataValue(), {
31+
value: 'Subject Three',
32+
language: 'en',
33+
place: 2,
34+
}),
35+
],
36+
},
37+
});
38+
form = new DsoEditMetadataForm(dso.metadata);
39+
});
40+
41+
42+
describe('adding a new value', () => {
43+
beforeEach(() => {
44+
form.add();
45+
});
46+
47+
it('should add an empty value to \"newValue\" with no place yet and editing set to true', () => {
48+
expect(form.newValue).toBeDefined();
49+
expect(form.newValue.originalValue.place).toBeUndefined();
50+
expect(form.newValue.newValue.place).toBeUndefined();
51+
expect(form.newValue.editing).toBeTrue();
52+
});
53+
54+
it('should not mark the form as changed yet', () => {
55+
expect(form.hasChanges()).toEqual(false);
56+
});
57+
58+
describe('and assigning a value and metadata field to it', () => {
59+
let mdField: string;
60+
let value: string;
61+
let expectedPlace: number;
62+
63+
beforeEach(() => {
64+
mdField = 'dc.subject';
65+
value = 'Subject Four';
66+
form.newValue.newValue.value = value;
67+
form.setMetadataField(mdField);
68+
expectedPlace = form.fields[mdField].length - 1;
69+
});
70+
71+
it('should add the new value to the values of the relevant field', () => {
72+
expect(form.fields[mdField][expectedPlace].newValue.value).toEqual(value);
73+
});
74+
75+
it('should set its editing flag to false', () => {
76+
expect(form.fields[mdField][expectedPlace].editing).toBeFalse();
77+
});
78+
79+
it('should set both its original and new place to match its position in the value array', () => {
80+
expect(form.fields[mdField][expectedPlace].newValue.place).toEqual(expectedPlace);
81+
expect(form.fields[mdField][expectedPlace].originalValue.place).toEqual(expectedPlace);
82+
});
83+
84+
it('should clear \"newValue\"', () => {
85+
expect(form.newValue).toBeUndefined();
86+
});
87+
88+
it('should mark the form as changed', () => {
89+
expect(form.hasChanges()).toEqual(true);
90+
});
91+
92+
describe('discard', () => {
93+
beforeEach(() => {
94+
form.discard();
95+
});
96+
97+
it('should remove the new value', () => {
98+
expect(form.fields[mdField][expectedPlace]).toBeUndefined();
99+
});
100+
101+
it('should mark the form as unchanged again', () => {
102+
expect(form.hasChanges()).toEqual(false);
103+
});
104+
105+
describe('reinstate', () => {
106+
beforeEach(() => {
107+
form.reinstate();
108+
});
109+
110+
it('should re-add the new value', () => {
111+
expect(form.fields[mdField][expectedPlace].newValue.value).toEqual(value);
112+
});
113+
114+
it('should mark the form as changed once again', () => {
115+
expect(form.hasChanges()).toEqual(true);
116+
});
117+
});
118+
});
119+
});
120+
});
121+
122+
describe('removing a value entirely (not just marking deleted)', () => {
123+
it('should remove the value on the correct index', () => {
124+
form.remove('dc.subject', 1);
125+
expect(form.fields['dc.subject'].length).toEqual(2);
126+
expect(form.fields['dc.subject'][0].newValue.value).toEqual('Subject One');
127+
expect(form.fields['dc.subject'][1].newValue.value).toEqual('Subject Three');
128+
});
129+
});
130+
131+
describe('moving a value', () => {
132+
beforeEach(() => {
133+
form.fields['dc.subject'][0].newValue.place = form.fields['dc.subject'][1].originalValue.place;
134+
form.fields['dc.subject'][1].newValue.place = form.fields['dc.subject'][0].originalValue.place;
135+
form.fields['dc.subject'][0].confirmChanges();
136+
form.fields['dc.subject'][1].confirmChanges();
137+
});
138+
139+
it('should mark the value as changed', () => {
140+
expect(form.fields['dc.subject'][0].hasChanges()).toEqual(true);
141+
expect(form.fields['dc.subject'][1].hasChanges()).toEqual(true);
142+
});
143+
144+
it('should mark the form as changed', () => {
145+
expect(form.hasChanges()).toEqual(true);
146+
});
147+
148+
describe('discard', () => {
149+
beforeEach(() => {
150+
form.discard();
151+
});
152+
153+
it('should reset the moved values their places to their original values', () => {
154+
expect(form.fields['dc.subject'][0].newValue.place).toEqual(form.fields['dc.subject'][0].originalValue.place);
155+
expect(form.fields['dc.subject'][1].newValue.place).toEqual(form.fields['dc.subject'][1].originalValue.place);
156+
});
157+
158+
it('should mark the form as unchanged again', () => {
159+
expect(form.hasChanges()).toEqual(false);
160+
});
161+
162+
describe('reinstate', () => {
163+
beforeEach(() => {
164+
form.reinstate();
165+
});
166+
167+
it('should move the values to their new places again', () => {
168+
expect(form.fields['dc.subject'][0].newValue.place).toEqual(form.fields['dc.subject'][1].originalValue.place);
169+
expect(form.fields['dc.subject'][1].newValue.place).toEqual(form.fields['dc.subject'][0].originalValue.place);
170+
});
171+
172+
it('should mark the form as changed once again', () => {
173+
expect(form.hasChanges()).toEqual(true);
174+
});
175+
});
176+
});
177+
});
178+
179+
describe('marking a value deleted', () => {
180+
beforeEach(() => {
181+
form.fields['dc.title'][0].change = DsoEditMetadataChangeType.REMOVE;
182+
});
183+
184+
it('should mark the value as changed', () => {
185+
expect(form.fields['dc.title'][0].hasChanges()).toEqual(true);
186+
});
187+
188+
it('should mark the form as changed', () => {
189+
expect(form.hasChanges()).toEqual(true);
190+
});
191+
192+
describe('discard', () => {
193+
beforeEach(() => {
194+
form.discard();
195+
});
196+
197+
it('should remove the deleted mark from the value', () => {
198+
expect(form.fields['dc.title'][0].change).toBeUndefined();
199+
});
200+
201+
it('should mark the form as unchanged again', () => {
202+
expect(form.hasChanges()).toEqual(false);
203+
});
204+
205+
describe('reinstate', () => {
206+
beforeEach(() => {
207+
form.reinstate();
208+
});
209+
210+
it('should re-mark the value as deleted', () => {
211+
expect(form.fields['dc.title'][0].change).toEqual(DsoEditMetadataChangeType.REMOVE);
212+
});
213+
214+
it('should mark the form as changed once again', () => {
215+
expect(form.hasChanges()).toEqual(true);
216+
});
217+
});
218+
});
219+
});
220+
221+
describe('editing a value', () => {
222+
const value = 'New title';
223+
224+
beforeEach(() => {
225+
form.fields['dc.title'][0].editing = true;
226+
form.fields['dc.title'][0].newValue.value = value;
227+
});
228+
229+
it('should not mark the form as changed yet', () => {
230+
expect(form.hasChanges()).toEqual(false);
231+
});
232+
233+
describe('and confirming the changes', () => {
234+
beforeEach(() => {
235+
form.fields['dc.title'][0].confirmChanges(true);
236+
});
237+
238+
it('should mark the value as changed', () => {
239+
expect(form.fields['dc.title'][0].hasChanges()).toEqual(true);
240+
});
241+
242+
it('should mark the form as changed', () => {
243+
expect(form.hasChanges()).toEqual(true);
244+
});
245+
246+
describe('discard', () => {
247+
beforeEach(() => {
248+
form.discard();
249+
});
250+
251+
it('should reset the changed value to its original value', () => {
252+
expect(form.fields['dc.title'][0].newValue.value).toEqual(form.fields['dc.title'][0].originalValue.value);
253+
});
254+
255+
it('should mark the form as unchanged again', () => {
256+
expect(form.hasChanges()).toEqual(false);
257+
});
258+
259+
describe('reinstate', () => {
260+
beforeEach(() => {
261+
form.reinstate();
262+
});
263+
264+
it('should put the changed value back in place', () => {
265+
expect(form.fields['dc.title'][0].newValue.value).toEqual(value);
266+
});
267+
268+
it('should mark the form as changed once again', () => {
269+
expect(form.hasChanges()).toEqual(true);
270+
});
271+
});
272+
});
273+
});
274+
});
275+
});

0 commit comments

Comments
 (0)