Skip to content

Commit 34faa4e

Browse files
[UXP-147] fix sections update, add tests
1 parent 9e5f2c0 commit 34faa4e

9 files changed

Lines changed: 56 additions & 32 deletions

src/app/shared/mocks/submission.mock.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,9 @@ export const mockSubmissionState: SubmissionObjectState = Object.assign({}, {
31153115
isLoading: false,
31163116
isDiscarding: false,
31173117
savePending: false,
3118-
depositPending: false
3118+
depositPending: false,
3119+
externalUploadPending: false,
3120+
externalUploadErrors: []
31193121
}
31203122
});
31213123

src/app/shared/testing/submission-service.stub.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class SubmissionServiceStub {
2222
getSubmissionSaveProcessingStatus = jasmine.createSpy('getSubmissionSaveProcessingStatus');
2323
getSubmissionDepositProcessingStatus = jasmine.createSpy('getSubmissionDepositProcessingStatus');
2424
getExternalUplodaProcessingStatus = jasmine.createSpy('getExternalUplodaProcessingStatus');
25+
getExternalUplodaErorrs = jasmine.createSpy('getExternalUplodaErorrs');
2526
hasUnsavedModification = jasmine.createSpy('hasUnsavedModification');
2627
isSectionHidden = jasmine.createSpy('isSectionHidden');
2728
isSectionReadOnly = jasmine.createSpy('isSectionReadOnly');

src/app/submission/objects/submission-objects.actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ export class ExecuteExternalUploadSuccessAction implements Action {
10671067
payload: {
10681068
submissionId: string;
10691069
sectionId: string;
1070-
submissionObject: SubmissionObject[];
1070+
sectionsObject: WorkspaceitemSectionsObject;
10711071
};
10721072

10731073
/**
@@ -1077,10 +1077,10 @@ export class ExecuteExternalUploadSuccessAction implements Action {
10771077
* the submission's ID
10781078
* @param sectionId
10791079
* the section's ID
1080-
* @param submissionObject
1080+
* @param sectionsObject
10811081
*/
1082-
constructor(submissionId: string, sectionId: string, submissionObject: SubmissionObject[]) {
1083-
this.payload = { submissionId, sectionId, submissionObject };
1082+
constructor(submissionId: string, sectionId: string, sectionsObject: WorkspaceitemSectionsObject) {
1083+
this.payload = { submissionId, sectionId, sectionsObject };
10841084
}
10851085
}
10861086

src/app/submission/objects/submission-objects.effects.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,7 @@ describe('SubmissionObjectEffects test suite', () => {
452452
},
453453
forms: {
454454
'2_traditionalpageone': {
455-
touched: {
456-
'dc.title': true
457-
}
455+
touched: {}
458456
}
459457
}
460458
} as any);
@@ -481,7 +479,7 @@ describe('SubmissionObjectEffects test suite', () => {
481479
submissionId,
482480
'traditionalpageone',
483481
mockSectionsData.traditionalpageone as any,
484-
errorsToShowList.traditionalpageone,
482+
[],
485483
serverValidationErrorsList.traditionalpageone
486484
),
487485
c: new UpdateSectionDataAction(

src/app/submission/objects/submission-objects.effects.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ export class SubmissionObjectEffects {
393393

394394
executeExternalUpload$ = createEffect(() => this.actions$.pipe(
395395
ofType(SubmissionObjectActionTypes.EXECUTE_EXTERNAL_UPLOAD),
396-
switchMap((action: SetDuplicateDecisionAction) => {
396+
withLatestFrom(this.store$),
397+
switchMap(([action, currentState]: [SetDuplicateDecisionAction, any]) => {
397398
return this.operationsService.jsonPatchByResourceID(
398399
this.submissionService.getSubmissionObjectLinkName(),
399400
action.payload.submissionId,
@@ -407,10 +408,18 @@ export class SubmissionObjectEffects {
407408
if (sectionErrors.length > 0) {
408409
return new ExecuteExternalUploadErrorAction(action.payload.submissionId, sectionErrors);
409410
} else {
411+
const index: any = parseInt(action.payload.submissionId, 10);
412+
const sections = currentState.submission.objects[index]?.sections;
413+
const newSectionsState = Object.assign({}, sections);
414+
415+
//Update upload section with received files
416+
newSectionsState[SectionsType.Upload] = Object.assign({}, newSectionsState[SectionsType.Upload], {
417+
data: Object.assign({files: (response[0].sections[SectionsType.Upload] as any).files})
418+
});
410419
return new ExecuteExternalUploadSuccessAction(
411420
action.payload.submissionId,
412421
action.payload.sectionId,
413-
response
422+
newSectionsState
414423
);
415424
}
416425
}),

src/app/submission/objects/submission-objects.reducer.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ describe('submissionReducer test suite', () => {
123123
isLoading: true,
124124
isDiscarding: false,
125125
savePending: false,
126-
depositPending: false
126+
depositPending: false,
127+
externalUploadPending: false,
128+
externalUploadErrors: []
127129
}
128130
};
129131

src/app/submission/objects/submission-objects.reducer.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,30 +1155,24 @@ function endExternalUploadExecution(state: SubmissionObjectState, action: Execut
11551155
*/
11561156

11571157
function updateSubmissionFromExternalUploadEvent(state: SubmissionObjectState, action: ExecuteExternalUploadSuccessAction) {
1158-
const index: any = findKey(action.payload.submissionObject, {id: parseInt(action.payload.submissionId, 10) as any});
1159-
const sectionData = action
1160-
.payload
1161-
.submissionObject[index]
1162-
.sections;
1158+
const sectionsData = action.payload.sectionsObject;
11631159

1160+
let currentSection = {};
1161+
const currentSectionId = action.payload.sectionId;
1162+
currentSection[currentSectionId] = state[ action.payload.submissionId ].sections[currentSectionId];
11641163

1165-
const sectionsKeys = Object.keys(sectionData);
1166-
const tempSections = {};
1167-
1168-
for (let key of sectionsKeys) {
1169-
tempSections[key] = Object.assign({}, state[action.payload.submissionId].sections[key], {
1170-
data: sectionData[key]
1171-
});
1172-
}
1173-
1174-
if (hasValue(state[ action.payload.submissionId ].sections[ action.payload.sectionId ])) {
1164+
if (hasValue(sectionsData[ action.payload.sectionId ])) {
11751165
return Object.assign({}, state, {
11761166
[ action.payload.submissionId ]: Object.assign({}, state[ action.payload.submissionId ], {
1177-
sections: tempSections,
1167+
sections: Object.assign({}, sectionsData, currentSection),
11781168
externalUploadPending: false
11791169
})
11801170
});
11811171
} else {
1182-
return state;
1172+
return Object.assign({}, state, {
1173+
[action.payload.submissionId]: Object.assign({}, state[action.payload.submissionId], {
1174+
externalUploadPending: false
1175+
})
1176+
});
11831177
}
11841178
}

src/app/submission/sections/external-upload/section-external-upload.component.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Collection } from '../../../core/shared/collection.model';
2727
import { SectionExternalUploadComponent } from './section-external-upload.component';
2828
import { ExternalUploadService } from './external-upload.service';
2929
import { ExternalServiceStub } from './external-upload-service.mock';
30+
import { PathableObjectError } from '../../../core/data/response-state.model';
3031

3132
function getMockCollectionDataService(): CollectionDataService {
3233
return jasmine.createSpyObj('CollectionDataService', {
@@ -148,6 +149,7 @@ describe('SectionExternalUploadComponent test suite', () => {
148149
collectionDataService = TestBed.inject(CollectionDataService);
149150
externalUploadService = TestBed.inject(ExternalUploadService);
150151
compAsAny.pathCombiner = new JsonPatchOperationPathCombiner('sections', sectionObject.id);
152+
compAsAny.errors$ = observableOf([]);
151153
});
152154

153155
afterEach(() => {
@@ -175,7 +177,7 @@ describe('SectionExternalUploadComponent test suite', () => {
175177
expect(fixture.debugElement.query(By.css('button')).nativeElement.disabled).toBeTruthy();
176178
});
177179

178-
it('Should execute upload is source is present', () => {
180+
it('Should execute upload if source is present', () => {
179181
spyOn(compAsAny, 'submitUpload');
180182
comp.loading$ = of(false);
181183

@@ -191,6 +193,23 @@ describe('SectionExternalUploadComponent test suite', () => {
191193
expect(fixture.debugElement.query(By.css('button')).nativeElement.disabled).toBeFalsy();
192194
expect(compAsAny.submitUpload).toHaveBeenCalled();
193195
});
196+
197+
it('Should display errors if present', () => {
198+
const errorObj = [{message:'Test error message', paths: ['external-upload']} as PathableObjectError];
199+
spyOn(compAsAny, 'submitUpload');
200+
comp.loading$ = of(false);
201+
comp.errors$ = of(errorObj);
202+
203+
comp.onSectionInit();
204+
comp.source = '/path/to/file';
205+
comp.submissionId = 'subId';
206+
207+
fixture.detectChanges();
208+
209+
const errorElement = fixture.debugElement.query(By.css('.text-danger')).nativeElement;
210+
expect(errorElement.innerHTML).toEqual(' ' + errorObj[0].message + ' ');
211+
212+
});
194213
});
195214

196215
});

src/app/submission/utils/parseSectionErrorPaths.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const regexShort = /\/sections\/(.*)/;
3636
* @returns {SectionErrorPath[]}
3737
*/
3838
const parseSectionErrorPaths = (path: string | string[]): SectionErrorPath[] => {
39-
const paths = typeof path === 'string' ? [path] : path;
40-
39+
const paths = typeof path === 'string' ? [path] : hasValue(path) ? path : [];
4140
return paths.map((item) => {
4241
if (item.match(regex) && item.match(regex).length > 2) {
4342
return {

0 commit comments

Comments
 (0)