Skip to content

Commit

Permalink
fix(Peer Group): Non admins can not create new peer groupings
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan committed Sep 26, 2023
1 parent 449e662 commit 08345ce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatDialogRef } from '@angular/material/dialog';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { of } from 'rxjs';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { of, throwError } from 'rxjs';
import { PeerGrouping } from '../../../../../app/domain/peerGrouping';
import { ReferenceComponent } from '../../../../../app/domain/referenceComponent';
import { StudentTeacherCommonServicesModule } from '../../../../../app/student-teacher-common-services.module';
Expand All @@ -15,6 +15,7 @@ const REFERENCE_COMPONENT_COMPONENT_ID1 = 'component1';
let component: CreateNewPeerGroupingDialogComponent;
let createNewPeerGroupingSpy: jasmine.Spy, dialogCloseSpy: jasmine.Spy;
let fixture: ComponentFixture<CreateNewPeerGroupingDialogComponent>;
let snackBar: MatSnackBar;

describe('CreateNewPeerGroupingDialogComponent', () => {
beforeEach(async () => {
Expand All @@ -30,6 +31,7 @@ describe('CreateNewPeerGroupingDialogComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(CreateNewPeerGroupingDialogComponent);
component = fixture.componentInstance;
snackBar = TestBed.inject(MatSnackBar);
fixture.detectChanges();
});
create();
Expand All @@ -41,6 +43,7 @@ function create() {
create_DifferentIdeasMaximizeLogic_ShouldCreatePeerGroup();
create_DifferentScoresAnyLogic_ShouldCreatePeerGroup();
create_DifferentScoresMaximizeLogic_ShouldCreatePeerGroup();
create_ErrorOccurs_ShowsError();
}

function create_RandomLogic_ShouldCreatePeerGroup() {
Expand Down Expand Up @@ -98,3 +101,41 @@ function expectLogicCreateNewPeerGrouping(logicType: string, mode: string) {
expect(createNewPeerGroupingSpy).toHaveBeenCalledWith(newPeerGrouping);
expect(dialogCloseSpy).toHaveBeenCalled();
}

function create_ErrorOccurs_ShowsError(): void {
describe('create new peer grouping returns error', () => {
create_GenericErrorOccurs_ShowsError();
create_NotAuthorizedErrorOccurs_ShowsError();
});
}

function create_GenericErrorOccurs_ShowsError(): void {
describe('returns generic error', () => {
it('shows generic error message in snackbar', async () => {
returnErrorExpectErrorMessage('genericError', 'An error occurred. Please try again.');
});
});
}

function create_NotAuthorizedErrorOccurs_ShowsError(): void {
describe('returns not authorized error', () => {
it('shows not authorized error in snackbar', async () => {
returnErrorExpectErrorMessage('notAuthorized', 'You are not allowed to perform this action.');
});
});
}

function returnErrorExpectErrorMessage(messageCode: string, errorMessage: string): void {
const snackBarSpy = spyOn(snackBar, 'open');
createNewPeerGroupingSpy.and.returnValue(
throwError(() => {
return {
error: {
messageCode: messageCode
}
};
})
);
component.create();
expect(snackBarSpy).toHaveBeenCalledWith(errorMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ export class CreateNewPeerGroupingDialogComponent extends AuthorPeerGroupingDial
create(): Subscription {
this.peerGrouping.tag = this.peerGroupingAuthoringService.getUniqueTag();
this.updatePeerGroupingLogic();
return this.peerGroupingAuthoringService.createNewPeerGrouping(this.peerGrouping).subscribe(
() => {
return this.peerGroupingAuthoringService.createNewPeerGrouping(this.peerGrouping).subscribe({
next: () => {
this.dialogRef.close();
},
() => {
this.snackBar.open($localize`Please try again (Error: duplicate tag).`);
error: ({ error }) => {
this.handleError(error);
}
);
});
}

private handleError(error: any): void {
switch (error.messageCode) {
case 'genericError':
this.snackBar.open($localize`An error occurred. Please try again.`);
break;
case 'notAuthorized':
this.snackBar.open($localize`You are not allowed to perform this action.`);
break;
}
}
}

0 comments on commit 08345ce

Please sign in to comment.