Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('FolderPickerComponent', () => {
expect(some(component.currentFolder.ChildItemVOs, 'isRecord')).toBeFalsy();

const getLeanItemsExpected = require('@root/test/responses/folder.getLeanItems.folderPicker.myFiles.success.json');
spyOn(api.folder, 'getLeanItems').and.returnValue(
of(new FolderResponse(getLeanItemsExpected)),
spyOn(api.folder, 'getWithChildren').and.returnValue(
Promise.resolve(new FolderResponse(getLeanItemsExpected)),
);

await component.loadCurrentFolderChildData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { PromptService } from '@shared/services/prompt/prompt.service';
import { EventService } from '@shared/services/event/event.service';
import { CookieService } from 'ngx-cookie-service';
import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
import { FolderVO } from '@models/index';
import { RecordVO } from '@models/record-vo';
import { FolderResponse } from '@shared/services/api/folder.repo';
import { ProfileEditComponent } from './profile-edit.component';

describe('ProfileEditComponent', () => {
Expand All @@ -32,17 +35,34 @@ describe('ProfileEditComponent', () => {
mockProfileService.getProfileItemDictionary.and.returnValue({});
mockProfileService.fetchProfileItems.and.returnValue(Promise.resolve());
mockProfileService.checkProfilePublic.and.returnValue(true);
const mockAccountService = {
getPrivateRoot: jasmine
.createSpy('getPrivateRoot')
.and.returnValue('root-folder'),
};

const mockApiService = {
folder: {
updateRoot: jasmine
.createSpy('updateRoot')
.and.returnValue(Promise.resolve()),
},
};

const mockFolderPickerService = {
chooseRecord: jasmine.createSpy('chooseRecord'),
};

beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [ProfileEditComponent],
providers: [
{ provide: DialogCdkService, useValue: mockDialogService },
{ provide: AccountService, useValue: {} },
{ provide: ApiService, useValue: {} },
{ provide: AccountService, useValue: mockAccountService },
{ provide: ApiService, useValue: mockApiService },
{ provide: MessageService, useValue: {} },
{ provide: ProfileService, useValue: mockProfileService },
{ provide: FolderPickerService, useValue: {} },
{ provide: FolderPickerService, useValue: mockFolderPickerService },
{ provide: PromptService, useValue: {} },
{
provide: EventService,
Expand All @@ -59,6 +79,14 @@ describe('ProfileEditComponent', () => {
fixture = TestBed.createComponent(ProfileEditComponent);
component = fixture.componentInstance;
fixture.detectChanges();

component.publicRoot = new FolderVO({
thumbArchiveNbr: 111,
thumbURL200: 'old200',
thumbURL500: 'old500',
thumbURL1000: 'old1000',
thumbURL2000: 'old2000',
});
});

it('should create', () => {
Expand Down Expand Up @@ -87,4 +115,42 @@ describe('ProfileEditComponent', () => {
width: '600px',
});
});

it('should update banner picture when chooseBannerPicture succeeds', async () => {
const mockRecord = new RecordVO({
thumbURL200: 'new200',
thumbURL500: 'new500',
thumbURL1000: 'new1000',
thumbURL2000: 'new2000',
archiveNbr: 999,
});
mockFolderPickerService.chooseRecord.and.resolveTo(mockRecord);

await component.chooseBannerPicture();

expect(mockApiService.folder.updateRoot).toHaveBeenCalled();
const [foldersArg, fieldsArg] =
mockApiService.folder.updateRoot.calls.mostRecent().args;

expect(fieldsArg).toEqual(['thumbArchiveNbr', 'view', 'viewProperty']);
expect(foldersArg[0].thumbArchiveNbr).toBe(999);

expect(component.publicRoot.thumbArchiveNbr).toBe(999);
expect(component.publicRoot.thumbURL200).toBe('new200');
expect(component.publicRoot.thumbURL500).toBe('new500');
expect(component.publicRoot.thumbURL1000).toBe('new1000');
expect(component.publicRoot.thumbURL2000).toBe('new2000');
});

it('should restore original thumbArchiveNbr when chooseBannerPicture throws FolderResponse', async () => {
const originalValue = component.publicRoot.thumbArchiveNbr;

mockFolderPickerService.chooseRecord.and.callFake(() => {
throw new FolderResponse({});
});

await component.chooseBannerPicture();

expect(component.publicRoot.thumbArchiveNbr).toBe(originalValue);
});
});
15 changes: 5 additions & 10 deletions src/app/core/components/profile-edit/profile-edit.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, Inject, AfterViewInit } from '@angular/core';
import { FolderVO, ArchiveVO, RecordVO } from '@models';
import { FolderVO, ArchiveVO } from '@models';
import {
ProfileItemVOData,
ProfileItemVODictionary,
Expand Down Expand Up @@ -158,16 +158,11 @@ export class ProfileEditComponent implements OnInit, AfterViewInit {
['thumbArchiveNbr', 'view', 'viewProperty'],
);
// borrow thumb URLs from record for now, until they can be regenerated
const thumbProps: Array<keyof (ArchiveVO | RecordVO)> = [
'thumbURL200',
'thumbURL500',
'thumbURL1000',
'thumbURL2000',
];
for (const prop of thumbProps) {
this.publicRoot[prop] = record[prop] as never;
}
this.publicRoot.thumbArchiveNbr = record.archiveNbr;
this.publicRoot.thumbURL200 = record.thumbURL200;
this.publicRoot.thumbURL500 = record.thumbURL500;
this.publicRoot.thumbURL1000 = record.thumbURL1000;
this.publicRoot.thumbURL2000 = record.thumbURL2000;
} catch (err) {
if (err instanceof FolderResponse) {
this.publicRoot.thumbArchiveNbr = originalValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@
@if (!item.isFolder && !isZip) {
<div
prBgImage
[bgSrc]="
!inGridView
? item.thumbURL200 || item.thumbUrl200
: item.thumbURL500 || item.thumbUrl500
"
[bgSrc]="!inGridView ? item.thumbURL200 : item.thumbURL500"
[cover]="inGridView"
></div>
}
Expand Down
12 changes: 4 additions & 8 deletions src/app/models/get-thumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ interface ThumbnailData {
}
export interface HasThumbnails {
thumbURL200?: string;
thumbUrl200?: string;
thumbnail256?: string;
thumbURL500?: string;
thumbUrl500?: string;
thumbURL1000?: string;
thumbUrl1000?: string;
thumbURL2000?: string;
thumbUrl2000?: string;
}

export function GetThumbnailInfo(
item: HasThumbnails,
minimumSize: number,
): ThumbnailData | undefined {
const thumbnails: ThumbnailData[] = [
{ size: 200, url: item.thumbUrl200 ?? item.thumbURL200 },
{ size: 200, url: item.thumbURL200 },
{ size: 256, url: item.thumbnail256 },
{ size: 500, url: item.thumbUrl500 ?? item.thumbURL500 },
{ size: 1000, url: item.thumbUrl1000 ?? item.thumbURL1000 },
{ size: 2000, url: item.thumbUrl2000 ?? item.thumbURL2000 },
{ size: 500, url: item.thumbURL500 },
{ size: 1000, url: item.thumbURL1000 },
{ size: 2000, url: item.thumbURL2000 },
].filter((thumb) => thumb.url);
const biggestAvailableThumbnail = thumbnails[thumbnails.length - 1];
if (minimumSize > biggestAvailableThumbnail?.size) {
Expand Down
25 changes: 25 additions & 0 deletions src/app/shared/services/api/folder.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ describe('Folder repo', () => {
folderRepo = TestBed.inject(FolderRepo);
});

it('should post folderVOs and return a FolderResponse', async () => {
const folder1 = new FolderVO({ folderId: 1 });
const folder2 = new FolderVO({ folderId: 2 });
const mockResponse = { success: true } as any;

httpSpy.sendRequestPromise.and.resolveTo(mockResponse);
const result = await folderRepo.post([folder1, folder2]);

expect(httpSpy.sendRequestPromise).toHaveBeenCalledWith(
'/folder/post',
[
{ FolderVO: jasmine.any(FolderVO) },
{ FolderVO: jasmine.any(FolderVO) },
],
{ ResponseClass: jasmine.any(Function) },
);

const callArgs = httpSpy.sendRequestPromise.calls.mostRecent().args[1];

expect(callArgs[0].FolderVO instanceof FolderVO).toBeTrue();
expect(callArgs[1].FolderVO instanceof FolderVO).toBeTrue();

expect(result).toBe(mockResponse);
});

Comment thread
cecilia-donnelly marked this conversation as resolved.
it('should get folder with children using the auth token', async () => {
const mockFolderVO = { folderId: 42 } as FolderVO;

Expand Down
10 changes: 0 additions & 10 deletions src/app/shared/services/api/folder.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,6 @@ export class FolderRepo extends BaseRepo {
});
}

public getLeanItems(folderVOs: FolderVO[]): Observable<FolderResponse> {
const data = folderVOs.map((folderVO) => ({
FolderVO: new FolderVO(folderVO),
}));

return this.http.sendRequest<FolderResponse>('/folder/getLeanItems', data, {
ResponseClass: FolderResponse,
});
}

public async post(folderVOs: FolderVO[]): Promise<FolderResponse> {
const data = folderVOs.map((folderVO) => ({
FolderVO: new FolderVO(folderVO),
Expand Down
8 changes: 6 additions & 2 deletions src/app/shared/services/api/record.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface StelaShare {
archive: {
id: string;
name: string;
thumbUrl200: string;
thumbURL200: string;
};
}
export type StelaRecord = Omit<RecordVO, 'files'> & {
Expand Down Expand Up @@ -151,7 +151,7 @@ export const convertStelaSharetoShareVO = (stelaShare: StelaShare): ShareVO =>
ArchiveVO: {
archiveId: stelaShare.archive.id,
fullName: stelaShare.archive.name,
thumbURL200: stelaShare.archive.thumbUrl200,
thumbURL200: stelaShare.archive.thumbURL200,
},
});

Expand All @@ -170,6 +170,10 @@ export const convertStelaRecordToRecordVO = (
): RecordVO =>
new RecordVO({
...stelaRecord,
thumbURL200: stelaRecord.thumbUrl200,
thumbURL500: stelaRecord.thumbUrl500,
thumbURL1000: stelaRecord.thumbUrl1000,
thumbURL2000: stelaRecord.thumbUrl2000,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, so this is the new mapping.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, so this is the new mapping.

Yes, it was easier to do this mapping to RecordVO than try to map all URL to camelcase Url.

TagVOs: (stelaRecord.tags ?? []).map((stelaTag) =>
convertStelaTagToTagVO(stelaTag, stelaRecord.archiveId),
),
Expand Down
82 changes: 77 additions & 5 deletions src/app/shared/services/data/data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { environment } from '@root/environments/environment';
import { DataStatus } from '@models/data-status.enum';

import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
import { ApiService } from '@shared/services/api/api.service';

const navigateMinData = require('@root/test/responses/folder.navigateMin.success.json');
const getLeanItemsData = require('@root/test/responses/folder.getLeanItems.success.json');
Expand All @@ -24,6 +25,32 @@ const testRecord = new RecordVO({
folder_linkId: 4,
archiveNbr: 'archivenbr',
});
const childItemVOsMock = [
{
folder_linkId: '233483',
archiveNbr: '05r0-016p',
},
{
folder_linkId: '233484',
archiveNbr: '05r0-016p',
},
{
folder_linkId: '224722',
archiveNbr: '05r0-016p',
},
{
folder_linkId: '223367',
archiveNbr: '05r0-016p',
},
{
folder_linkId: '223366',
archiveNbr: '05r0-016p',
},
{
folder_linkId: '233485',
archiveNbr: '05r0-016p',
},
];

// we should refactor the data service test suite and mock all the dependencies
// so we do not have to fix the tests everytime an injected service changes
Expand Down Expand Up @@ -76,7 +103,13 @@ describe('DataService', () => {

it('should fetch lean data for placeholder items', (done) => {
const service = TestBed.inject(DataService);
const httpMock = TestBed.inject(HttpTestingController);
const api = TestBed.inject(ApiService);
spyOn(api.folder, 'getWithChildren').and.returnValue(
Promise.resolve({
isSuccessful: true,
getFolderVO: () => ({ ChildItemVOs: childItemVOsMock }),
} as unknown as FolderResponse),
);
const navigateResponse = new FolderResponse(navigateMinData);
const currentFolder = navigateResponse.getFolderVO(true);
service.setCurrentFolder(currentFolder);
Expand All @@ -88,15 +121,13 @@ describe('DataService', () => {
service
.fetchLeanItems(currentFolder.ChildItemVOs)
.then(() => {
expect(api.folder.getWithChildren).toHaveBeenCalled();
currentFolder.ChildItemVOs.forEach((item) => {
expect(item.dataStatus).toEqual(DataStatus.Lean);
});
done();
})
.catch(done.fail);

const req = httpMock.expectOne(`${environment.apiUrl}/folder/getLeanItems`);
req.flush(getLeanItemsData);
});

it('should handle an empty array when fetching lean data', (done) => {
Expand All @@ -116,6 +147,45 @@ describe('DataService', () => {
});
});

it('should return 0 and reset items when fetchLeanItems response is unsuccessful', (done) => {
const service = TestBed.inject(DataService);
const api = TestBed.inject(ApiService);
spyOn(api.folder, 'getWithChildren').and.returnValue(
Promise.resolve({
isSuccessful: false,
} as unknown as FolderResponse),
);

const navigateResponse = new FolderResponse(navigateMinData);
const currentFolder = navigateResponse.getFolderVO(true);
service.setCurrentFolder(currentFolder);
currentFolder.ChildItemVOs.forEach((item: RecordVO | FolderVO) => {
service.registerItem(item);
});
const rejects: number[] = [];
currentFolder.ChildItemVOs.forEach((item, index) => {
item.fetched = new Promise((resolve, reject) => {
const wrappedReject = () => {
rejects.push(index);
reject();
};
(item as any)._reject = wrappedReject;
});
});

service
.fetchLeanItems(currentFolder.ChildItemVOs)
.then((count) => {
expect(count).toBe(0);
currentFolder.ChildItemVOs.forEach((item) => {
expect(item.isFetching).toBeFalse();
expect(item.fetched).toBeNull();
});
done();
})
.catch(done.fail);
});

// the method fetchFullItems uses both the record.repo and the folder.repo
// and the data service test suite does not create mocks for them
// because the methods api.folder.getWithChildren and api.record.get
Expand Down Expand Up @@ -209,7 +279,9 @@ describe('DataService', () => {
})
.catch(done.fail);

const req = httpMock.expectOne(`${environment.apiUrl}/folder/getLeanItems`);
const req = httpMock.expectOne(
`${environment.apiUrl}/v2/folder?folderIds[]=149612`,
);
req.flush(getLeanItemsData);
});
});
Loading