Skip to content

Commit

Permalink
Fix attachment unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Dec 20, 2024
1 parent e1d937d commit 50a5007
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[isPresentationMode]="isPresentationMode()"
(onCompletion)="toggleCompletion($event)"
(onCollapse)="toggleCollapse($event)"
(onShowIsolated)="handleDownload(false)"
(onShowOriginalVersion)="handleDownload(true)"
(onShowIsolated)="handleDownload()"
(onShowOriginalVersion)="handleOriginalVersion()"
>
@if (lectureUnit().attachment?.uploadDate) {
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,25 @@ export class AttachmentUnitComponent extends LectureUnitDirective<AttachmentUnit
* Downloads the file as the student version if available, otherwise the instructor version
* If it is not the student view, it always downloads the original version
*/
handleDownload(originalVersion: boolean) {
handleDownload() {
this.logEvent();

const attachment = this.lectureUnit().attachment!;
// Determine the link based on the availability of a student version
const link = this.lectureUnit().attachment!.studentVersion || this.fileService.createStudentLink(this.lectureUnit().attachment!.link!);

// Determine the link based on the originalVersion flag and the availability of a student version
const link = originalVersion ? attachment.link! : attachment.studentVersion || this.fileService.createStudentLink(attachment.link!);
if (link) {
this.fileService.downloadFileByAttachmentName(link, this.lectureUnit().attachment!.name!);
this.onCompletion.emit({ lectureUnit: this.lectureUnit(), completed: true });
}
}

handleOriginalVersion() {
this.logEvent();

const link = this.lectureUnit().attachment!.link!;

if (link) {
this.fileService.downloadFileByAttachmentName(link, attachment.name!);
this.fileService.downloadFileByAttachmentName(link, this.lectureUnit().attachment!.name!);
this.onCompletion.emit({ lectureUnit: this.lectureUnit(), completed: true });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,23 @@ describe('AttachmentUnitComponent', () => {
});

it('should handle download', () => {
const createStudentLinkSpy = jest.spyOn(fileService, 'createStudentLink');
const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');
const onCompletionEmitSpy = jest.spyOn(component.onCompletion, 'emit');

fixture.detectChanges();
component.handleDownload();

expect(createStudentLinkSpy).toHaveBeenCalledOnce();
expect(downloadFileSpy).toHaveBeenCalledOnce();
expect(onCompletionEmitSpy).toHaveBeenCalledOnce();
});

it('should handle original version', () => {
const downloadFileSpy = jest.spyOn(fileService, 'downloadFileByAttachmentName');
const onCompletionEmitSpy = jest.spyOn(component.onCompletion, 'emit');

component.handleOriginalVersion();

expect(downloadFileSpy).toHaveBeenCalledOnce();
expect(onCompletionEmitSpy).toHaveBeenCalledOnce();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export class MockFileService {

replaceLectureAttachmentPrefixAndUnderscores = (link: string) => link;
replaceAttachmentPrefixAndUnderscores = (link: string) => link;

createStudentLink = (link: string) => link;
}

0 comments on commit 50a5007

Please sign in to comment.