From 08345ce098f55dc2b064b13ad293316d2d0c4170 Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Tue, 26 Sep 2023 12:04:34 -0400 Subject: [PATCH] fix(Peer Group): Non admins can not create new peer groupings #1433 --- ...new-peer-grouping-dialog.component.spec.ts | 45 ++++++++++++++++++- ...eate-new-peer-grouping-dialog.component.ts | 21 ++++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.spec.ts b/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.spec.ts index f9ea8a08a7a..f560c7de7bf 100644 --- a/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.spec.ts +++ b/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.spec.ts @@ -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'; @@ -15,6 +15,7 @@ const REFERENCE_COMPONENT_COMPONENT_ID1 = 'component1'; let component: CreateNewPeerGroupingDialogComponent; let createNewPeerGroupingSpy: jasmine.Spy, dialogCloseSpy: jasmine.Spy; let fixture: ComponentFixture; +let snackBar: MatSnackBar; describe('CreateNewPeerGroupingDialogComponent', () => { beforeEach(async () => { @@ -30,6 +31,7 @@ describe('CreateNewPeerGroupingDialogComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(CreateNewPeerGroupingDialogComponent); component = fixture.componentInstance; + snackBar = TestBed.inject(MatSnackBar); fixture.detectChanges(); }); create(); @@ -41,6 +43,7 @@ function create() { create_DifferentIdeasMaximizeLogic_ShouldCreatePeerGroup(); create_DifferentScoresAnyLogic_ShouldCreatePeerGroup(); create_DifferentScoresMaximizeLogic_ShouldCreatePeerGroup(); + create_ErrorOccurs_ShowsError(); } function create_RandomLogic_ShouldCreatePeerGroup() { @@ -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); +} diff --git a/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts b/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts index 5941ce34f45..9df8ccbbf36 100644 --- a/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts +++ b/src/assets/wise5/authoringTool/peer-grouping/create-new-peer-grouping-dialog/create-new-peer-grouping-dialog.component.ts @@ -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; + } } }