From 0a1b1950c7693772de76973791fcde75c3b9fe88 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 19 Oct 2023 17:52:16 +1100 Subject: [PATCH 01/11] Add unit test for every method in comment-row.component.spec.ts --- .../comment-row/comment-row.component.spec.ts | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index efdfd283c3d..dc0b9350a73 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -1,5 +1,8 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { + CommentVisibilityType, +} from '../../../../types/api-output'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; @@ -45,4 +48,91 @@ describe('CommentRowComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + + it('should properly handle visibility settings if originalComment is defined', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: true, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + + component.ngOnChanges(); + + }); + + + it('should emit closeEditing event', () => { + component.closeEditingEvent.emit = jest.fn(); + + component.triggerCloseEditing(); + + expect(component.closeEditingEvent.emit).toHaveBeenCalled(); + }); + + + it('should emit saveComment event', () => { + const spy = jest.spyOn(component.saveCommentEvent, 'emit'); + component.triggerSaveCommentEvent(); + expect(spy).toHaveBeenCalled(); + }); + + + it('should emit deleteComment event after modal confirmation', (done) => { + const mockModalRef = { result: new Promise((resolve) => resolve(true)) }; + + jest.spyOn((component as any).simpleModalService, 'openConfirmationModal').mockReturnValue(mockModalRef); + const emitSpy = jest.spyOn(component.deleteCommentEvent, 'emit'); + + component.triggerDeleteCommentEvent(); + + mockModalRef.result.then(() => { + expect(emitSpy).toHaveBeenCalled(); + done(); + }); + + }); + + it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: true, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + const spy = jest.spyOn(component.visibilityStateMachine, 'allowAllApplicableTypesToSee'); + + component.ngOnChanges(); + + expect(spy).not.toHaveBeenCalled(); + }); }); From d14b7f88e9e98af3eebabbdc3f4673766a6eb8ea Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 19 Oct 2023 22:40:00 +1100 Subject: [PATCH 02/11] [#12588] Add one unit test in comment-row.component.spec.ts --- .../comment-row/comment-row.component.spec.ts | 85 ++++++++++++------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index dc0b9350a73..f02d58b3081 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -1,10 +1,8 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; -import { - CommentVisibilityType, -} from '../../../../types/api-output'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { CommentVisibilityType } from '../../../../types/api-output'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module'; import { CommentEditFormComponent } from '../comment-edit-form/comment-edit-form.component'; @@ -49,7 +47,7 @@ describe('CommentRowComponent', () => { expect(component).toBeTruthy(); }); - + describe('ngOnChanges', () => { it('should properly handle visibility settings if originalComment is defined', () => { component.model = { originalComment: { @@ -63,60 +61,84 @@ describe('CommentRowComponent', () => { lastEditedAt: new Date().getTime(), showGiverNameTo: [], }, - commentEditFormModel: { - commentText: 'Mock comment text for form', - isUsingCustomVisibilities: false, - showCommentTo: [CommentVisibilityType.GIVER], - showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], - }, - isEditing: true, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, }; component.ngOnChanges(); + expect(component.visibilityStateMachine).toBeDefined(); + expect(component.visibilityStateMachine.allowAllApplicableTypesToSee).toBeDefined(); }); + it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: true, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + + const spy = jest.spyOn(component.visibilityStateMachine, 'allowAllApplicableTypesToSee'); + component.ngOnChanges(); + expect(spy).not.toHaveBeenCalled(); + }); + }); + describe('triggerCloseEditing', () => { it('should emit closeEditing event', () => { component.closeEditingEvent.emit = jest.fn(); - component.triggerCloseEditing(); - expect(component.closeEditingEvent.emit).toHaveBeenCalled(); }); + }); - + describe('triggerSaveCommentEvent', () => { it('should emit saveComment event', () => { const spy = jest.spyOn(component.saveCommentEvent, 'emit'); component.triggerSaveCommentEvent(); expect(spy).toHaveBeenCalled(); }); + }); - - it('should emit deleteComment event after modal confirmation', (done) => { - const mockModalRef = { result: new Promise((resolve) => resolve(true)) }; - + describe('triggerDeleteCommentEvent', () => { + it('should emit deleteComment event after modal confirmation', async () => { + const mockModalRef = { result: Promise.resolve(true) }; jest.spyOn((component as any).simpleModalService, 'openConfirmationModal').mockReturnValue(mockModalRef); const emitSpy = jest.spyOn(component.deleteCommentEvent, 'emit'); - component.triggerDeleteCommentEvent(); - - mockModalRef.result.then(() => { - expect(emitSpy).toHaveBeenCalled(); - done(); - }); - + await mockModalRef.result; + expect(emitSpy).toHaveBeenCalled(); }); - it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { + it('should set visibility settings when originalComment is defined and not following feedback question', () => { component.model = { originalComment: { - isVisibilityFollowingFeedbackQuestion: true, + isVisibilityFollowingFeedbackQuestion: false, commentGiver: 'mockCommentGiver', lastEditorEmail: 'mockEditor@example.com', feedbackResponseCommentId: 12345, commentText: 'Mock comment text', - showCommentTo: [], + showCommentTo: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], createdAt: new Date().getTime(), lastEditedAt: new Date().getTime(), showGiverNameTo: [], @@ -127,12 +149,11 @@ describe('CommentRowComponent', () => { showCommentTo: [CommentVisibilityType.GIVER], showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], }, - isEditing: true, + isEditing: true, }; - const spy = jest.spyOn(component.visibilityStateMachine, 'allowAllApplicableTypesToSee'); - + const spy = jest.spyOn(component.visibilityStateMachine, 'applyVisibilitySettings'); component.ngOnChanges(); - expect(spy).not.toHaveBeenCalled(); }); + }); }); From ebceec3174fc2c0a3729ddc9492a82261cca0d25 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 20 Oct 2023 05:38:32 +1100 Subject: [PATCH 03/11] [#12588] Update on unit test in comment-row.component.spec.ts --- .../comment-row/comment-row.component.spec.ts | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index f02d58b3081..db6f5b37ef9 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -3,6 +3,7 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { CommentVisibilityType } from '../../../../types/api-output'; +import { FeedbackResponseCommentService } from '../../../../services/feedback-response-comment.service'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module'; import { CommentEditFormComponent } from '../comment-edit-form/comment-edit-form.component'; @@ -16,6 +17,16 @@ describe('CommentRowComponent', () => { let component: CommentRowComponent; let fixture: ComponentFixture; + const spyVisibilityStateMachine: any = { + allowAllApplicableTypesToSee: jest.fn(), + applyVisibilitySettings: jest.fn(), + getVisibilityTypesUnderVisibilityControl: jest.fn() + } + + const spyCommentService: any = { + getNewVisibilityStateMachine: jest.fn().mockReturnValue(spyVisibilityStateMachine) + }; + beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [ @@ -33,6 +44,9 @@ describe('CommentRowComponent', () => { NgbModule, RichTextEditorModule, ], + providers: [ + { provide: FeedbackResponseCommentService, useValue: spyCommentService }, + ] }) .compileComponents(); })); @@ -74,6 +88,10 @@ describe('CommentRowComponent', () => { expect(component.visibilityStateMachine).toBeDefined(); expect(component.visibilityStateMachine.allowAllApplicableTypesToSee).toBeDefined(); + + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); + + expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalled(); }); it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { @@ -97,18 +115,16 @@ describe('CommentRowComponent', () => { }, isEditing: true, }; - - const spy = jest.spyOn(component.visibilityStateMachine, 'allowAllApplicableTypesToSee'); component.ngOnChanges(); - expect(spy).not.toHaveBeenCalled(); + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); }); }); describe('triggerCloseEditing', () => { it('should emit closeEditing event', () => { - component.closeEditingEvent.emit = jest.fn(); + const emitSpy = jest.spyOn(component.closeEditingEvent, 'emit'); component.triggerCloseEditing(); - expect(component.closeEditingEvent.emit).toHaveBeenCalled(); + expect(emitSpy).toHaveBeenCalled(); }); }); @@ -151,9 +167,12 @@ describe('CommentRowComponent', () => { }, isEditing: true, }; - const spy = jest.spyOn(component.visibilityStateMachine, 'applyVisibilitySettings'); component.ngOnChanges(); - expect(spy).not.toHaveBeenCalled(); + + expect(spyVisibilityStateMachine.applyVisibilitySettings).toHaveBeenCalledWith({ + SHOW_COMMENT: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + SHOW_GIVER_NAME: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS] + }); }); }); }); From 3fe6ccb571bf6b714511fb24210797b49ac2b45a Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 20 Oct 2023 05:50:19 +1100 Subject: [PATCH 04/11] [#12588] Update on small typo in comment-row.component.spec.ts --- .../comment-row/comment-row.component.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index db6f5b37ef9..38d8959070f 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -2,8 +2,8 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; -import { CommentVisibilityType } from '../../../../types/api-output'; import { FeedbackResponseCommentService } from '../../../../services/feedback-response-comment.service'; +import { CommentVisibilityType } from '../../../../types/api-output'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module'; import { CommentEditFormComponent } from '../comment-edit-form/comment-edit-form.component'; @@ -20,11 +20,11 @@ describe('CommentRowComponent', () => { const spyVisibilityStateMachine: any = { allowAllApplicableTypesToSee: jest.fn(), applyVisibilitySettings: jest.fn(), - getVisibilityTypesUnderVisibilityControl: jest.fn() - } + getVisibilityTypesUnderVisibilityControl: jest.fn(), + }; const spyCommentService: any = { - getNewVisibilityStateMachine: jest.fn().mockReturnValue(spyVisibilityStateMachine) + getNewVisibilityStateMachine: jest.fn().mockReturnValue(spyVisibilityStateMachine), }; beforeEach(waitForAsync(() => { @@ -46,7 +46,7 @@ describe('CommentRowComponent', () => { ], providers: [ { provide: FeedbackResponseCommentService, useValue: spyCommentService }, - ] + ], }) .compileComponents(); })); @@ -171,7 +171,7 @@ describe('CommentRowComponent', () => { expect(spyVisibilityStateMachine.applyVisibilitySettings).toHaveBeenCalledWith({ SHOW_COMMENT: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], - SHOW_GIVER_NAME: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS] + SHOW_GIVER_NAME: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], }); }); }); From 4f053d94ec2dd5c1f638bfad73229cd5e8135e5f Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 24 Oct 2023 23:52:25 +1100 Subject: [PATCH 05/11] [#12588] Update on small change in comment-row.component.spec.ts --- .../comment-box/comment-row/comment-row.component.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index 38d8959070f..e3f46362148 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -89,9 +89,9 @@ describe('CommentRowComponent', () => { expect(component.visibilityStateMachine).toBeDefined(); expect(component.visibilityStateMachine.allowAllApplicableTypesToSee).toBeDefined(); - expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalledWith(); - expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalled(); + expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalledWith([]); }); it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { @@ -116,7 +116,7 @@ describe('CommentRowComponent', () => { isEditing: true, }; component.ngOnChanges(); - expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalledWith(); }); }); From 88ad30801a6a853856dcf115d280b5c89d59c855 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 27 Oct 2023 10:56:11 +1100 Subject: [PATCH 06/11] [#12588] Add unit test for every method in recipient-type-name.pipe.spec.ts --- .../recipient-type-name.pipe.spec.ts | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts index 1b1bf469222..5020f0b1fc0 100644 --- a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts +++ b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts @@ -1,8 +1,50 @@ import { RecipientTypeNamePipe } from './recipient-type-name.pipe'; +import { FeedbackParticipantType } from '../../../types/api-output'; describe('RecipientTypeNamePipe', () => { - it('create an instance', () => { - const pipe: RecipientTypeNamePipe = new RecipientTypeNamePipe(); + let pipe: RecipientTypeNamePipe; + + beforeEach(() => { + pipe = new RecipientTypeNamePipe(); + }); + + it('should create an instance', () => { expect(pipe).toBeTruthy(); }); + + it('should return "Team" for TEAMS', () => { + expect(pipe.transform(FeedbackParticipantType.TEAMS, FeedbackParticipantType.STUDENTS)).toEqual('Team'); + }); + + it('should return "Student" for STUDENTS', () => { + expect(pipe.transform(FeedbackParticipantType.STUDENTS, FeedbackParticipantType.STUDENTS)).toEqual('Student'); + }); + + it('should return "Instructor" for INSTRUCTORS', () => { + expect(pipe.transform(FeedbackParticipantType.INSTRUCTORS, FeedbackParticipantType.STUDENTS)).toEqual('Instructor'); + }); + + // ... you can continue with the other FeedbackParticipantTypes + + describe('for OWN_TEAM_MEMBERS and OWN_TEAM_MEMBERS_INCLUDING_SELF', () => { + it('should return "Student" when giverType is STUDENTS', () => { + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.STUDENTS)).toEqual('Student'); + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.STUDENTS)).toEqual('Student'); + }); + + it('should return "Instructor" when giverType is INSTRUCTORS', () => { + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.INSTRUCTORS)).toEqual('Instructor'); + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.INSTRUCTORS)).toEqual('Instructor'); + }); + + it('should return "Student" when giverType is TEAMS', () => { + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.TEAMS)).toEqual('Student'); + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.TEAMS)).toEqual('Student'); + }); + + it('should return "Unknown" for any other giverType', () => { + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, 'ANY_OTHER_TYPE' as FeedbackParticipantType)).toEqual('Unknown'); + expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, 'ANY_OTHER_TYPE' as FeedbackParticipantType)).toEqual('Unknown'); + }); + }); }); From 8b389075ab8a8b33d804519b81ce7419d485aeff Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 27 Oct 2023 11:07:05 +1100 Subject: [PATCH 07/11] [#12588] fix some typo --- .../comment-box/comment-row/comment-row.component.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index e3f46362148..c9e1739057e 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -168,7 +168,6 @@ describe('CommentRowComponent', () => { isEditing: true, }; component.ngOnChanges(); - expect(spyVisibilityStateMachine.applyVisibilitySettings).toHaveBeenCalledWith({ SHOW_COMMENT: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], SHOW_GIVER_NAME: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], From 620f796200eb3855b956020dbd11cf11f0eb8cad Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 27 Oct 2023 11:33:06 +1100 Subject: [PATCH 08/11] [#12588] fix some typo --- .../recipient-type-name.pipe.spec.ts | 48 ++----------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts index 5020f0b1fc0..dd17bd23898 100644 --- a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts +++ b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts @@ -1,50 +1,8 @@ import { RecipientTypeNamePipe } from './recipient-type-name.pipe'; -import { FeedbackParticipantType } from '../../../types/api-output'; describe('RecipientTypeNamePipe', () => { - let pipe: RecipientTypeNamePipe; - - beforeEach(() => { - pipe = new RecipientTypeNamePipe(); - }); - - it('should create an instance', () => { + it('create an instance', () => { + const pipe: RecipientTypeNamePipe = new RecipientTypeNamePipe(); expect(pipe).toBeTruthy(); }); - - it('should return "Team" for TEAMS', () => { - expect(pipe.transform(FeedbackParticipantType.TEAMS, FeedbackParticipantType.STUDENTS)).toEqual('Team'); - }); - - it('should return "Student" for STUDENTS', () => { - expect(pipe.transform(FeedbackParticipantType.STUDENTS, FeedbackParticipantType.STUDENTS)).toEqual('Student'); - }); - - it('should return "Instructor" for INSTRUCTORS', () => { - expect(pipe.transform(FeedbackParticipantType.INSTRUCTORS, FeedbackParticipantType.STUDENTS)).toEqual('Instructor'); - }); - - // ... you can continue with the other FeedbackParticipantTypes - - describe('for OWN_TEAM_MEMBERS and OWN_TEAM_MEMBERS_INCLUDING_SELF', () => { - it('should return "Student" when giverType is STUDENTS', () => { - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.STUDENTS)).toEqual('Student'); - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.STUDENTS)).toEqual('Student'); - }); - - it('should return "Instructor" when giverType is INSTRUCTORS', () => { - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.INSTRUCTORS)).toEqual('Instructor'); - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.INSTRUCTORS)).toEqual('Instructor'); - }); - - it('should return "Student" when giverType is TEAMS', () => { - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, FeedbackParticipantType.TEAMS)).toEqual('Student'); - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, FeedbackParticipantType.TEAMS)).toEqual('Student'); - }); - - it('should return "Unknown" for any other giverType', () => { - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS, 'ANY_OTHER_TYPE' as FeedbackParticipantType)).toEqual('Unknown'); - expect(pipe.transform(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF, 'ANY_OTHER_TYPE' as FeedbackParticipantType)).toEqual('Unknown'); - }); - }); -}); +}); \ No newline at end of file From 65ab892f694c8bb6fe9ec6226b76b65db0088449 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 27 Oct 2023 11:41:12 +1100 Subject: [PATCH 09/11] [#12588] Add some unit test in recipient-type-name.pipe.spec.ts --- .../question-submission-form/recipient-type-name.pipe.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts index dd17bd23898..1b1bf469222 100644 --- a/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts +++ b/src/web/app/components/question-submission-form/recipient-type-name.pipe.spec.ts @@ -5,4 +5,4 @@ describe('RecipientTypeNamePipe', () => { const pipe: RecipientTypeNamePipe = new RecipientTypeNamePipe(); expect(pipe).toBeTruthy(); }); -}); \ No newline at end of file +}); From 2fb30602a54591e1018e72ff0356bb735760a49f Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 29 Oct 2023 11:49:21 +1100 Subject: [PATCH 10/11] [#12588] Fix the error --- .../comment-row/comment-row.component.spec.ts | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index efdfd283c3d..5487528ed3a 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -2,6 +2,8 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { FeedbackResponseCommentService } from '../../../../services/feedback-response-comment.service'; +import { CommentVisibilityType, FeedbackVisibilityType } from '../../../../types/api-output'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module'; import { CommentEditFormComponent } from '../comment-edit-form/comment-edit-form.component'; @@ -15,6 +17,16 @@ describe('CommentRowComponent', () => { let component: CommentRowComponent; let fixture: ComponentFixture; + const spyVisibilityStateMachine: any = { + allowAllApplicableTypesToSee: jest.fn(), + applyVisibilitySettings: jest.fn(), + getVisibilityTypesUnderVisibilityControl: jest.fn(), + }; + + const spyCommentService: any = { + getNewVisibilityStateMachine: jest.fn().mockReturnValue(spyVisibilityStateMachine), + }; + beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [ @@ -32,6 +44,9 @@ describe('CommentRowComponent', () => { NgbModule, RichTextEditorModule, ], + providers: [ + { provide: FeedbackResponseCommentService, useValue: spyCommentService }, + ], }) .compileComponents(); })); @@ -45,4 +60,120 @@ describe('CommentRowComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + describe('ngOnChanges', () => { + it('should properly handle visibility settings if originalComment is defined', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: true, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + component.questionShowResponsesTo = [FeedbackVisibilityType.INSTRUCTORS, FeedbackVisibilityType.RECIPIENT]; + component.ngOnChanges(); + + expect(component.visibilityStateMachine).toBeDefined(); + expect(component.visibilityStateMachine.allowAllApplicableTypesToSee).toBeDefined(); + + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); + + expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalledWith( + component.questionShowResponsesTo + ); + }); + + it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: true, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + component.ngOnChanges(); + expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); + }); + }); + + describe('triggerCloseEditing', () => { + it('should emit closeEditing event', () => { + const emitSpy = jest.spyOn(component.closeEditingEvent, 'emit'); + component.triggerCloseEditing(); + expect(emitSpy).toHaveBeenCalled(); + }); + }); + + describe('triggerSaveCommentEvent', () => { + it('should emit saveComment event', () => { + const spy = jest.spyOn(component.saveCommentEvent, 'emit'); + component.triggerSaveCommentEvent(); + expect(spy).toHaveBeenCalled(); + }); + }); + + describe('triggerDeleteCommentEvent', () => { + it('should emit deleteComment event after modal confirmation', async () => { + const mockModalRef = { result: Promise.resolve(true) }; + jest.spyOn((component as any).simpleModalService, 'openConfirmationModal').mockReturnValue(mockModalRef); + const emitSpy = jest.spyOn(component.deleteCommentEvent, 'emit'); + component.triggerDeleteCommentEvent(); + await mockModalRef.result; + expect(emitSpy).toHaveBeenCalled(); + }); + + it('should set visibility settings when originalComment is defined and not following feedback question', () => { + component.model = { + originalComment: { + isVisibilityFollowingFeedbackQuestion: false, + commentGiver: 'mockCommentGiver', + lastEditorEmail: 'mockEditor@example.com', + feedbackResponseCommentId: 12345, + commentText: 'Mock comment text', + showCommentTo: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + createdAt: new Date().getTime(), + lastEditedAt: new Date().getTime(), + showGiverNameTo: [], + }, + commentEditFormModel: { + commentText: 'Mock comment text for form', + isUsingCustomVisibilities: false, + showCommentTo: [CommentVisibilityType.GIVER], + showGiverNameTo: [CommentVisibilityType.INSTRUCTORS], + }, + isEditing: true, + }; + component.ngOnChanges(); + expect(spyVisibilityStateMachine.applyVisibilitySettings).toHaveBeenCalledWith({ + SHOW_COMMENT: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + SHOW_GIVER_NAME: [CommentVisibilityType.GIVER, CommentVisibilityType.INSTRUCTORS], + }); + }); + }); }); From 101af472fb0bf2eb3110d0be8aa45428e62b02f1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 29 Oct 2023 12:01:51 +1100 Subject: [PATCH 11/11] [#12588] Fix the error --- .../comment-box/comment-row/comment-row.component.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts index 5487528ed3a..08b37a44577 100755 --- a/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts +++ b/src/web/app/components/comment-box/comment-row/comment-row.component.spec.ts @@ -3,7 +3,7 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { FeedbackResponseCommentService } from '../../../../services/feedback-response-comment.service'; -import { CommentVisibilityType, FeedbackVisibilityType } from '../../../../types/api-output'; +import { CommentVisibilityType, FeedbackVisibilityType } from '../../../../types/api-output'; import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module'; import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module'; import { CommentEditFormComponent } from '../comment-edit-form/comment-edit-form.component'; @@ -92,7 +92,7 @@ describe('CommentRowComponent', () => { expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalled(); expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalledWith( - component.questionShowResponsesTo + component.questionShowResponsesTo, ); });