Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12588] Add unit test for every method in CommentRowComponent #12612

Merged
merged 14 commits into from
Nov 30, 2023
Merged
Changes from 10 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 @@ -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 } 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';
Expand All @@ -15,6 +17,16 @@ describe('CommentRowComponent', () => {
let component: CommentRowComponent;
let fixture: ComponentFixture<CommentRowComponent>;

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: [
Expand All @@ -32,6 +44,9 @@ describe('CommentRowComponent', () => {
NgbModule,
RichTextEditorModule,
],
providers: [
{ provide: FeedbackResponseCommentService, useValue: spyCommentService },
],
})
.compileComponents();
}));
Expand All @@ -45,4 +60,118 @@ describe('CommentRowComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
ThomasGreen123 marked this conversation as resolved.
Show resolved Hide resolved

describe('ngOnChanges', () => {
it('should properly handle visibility settings if originalComment is defined', () => {
component.model = {
originalComment: {
isVisibilityFollowingFeedbackQuestion: true,
commentGiver: 'mockCommentGiver',
lastEditorEmail: '[email protected]',
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(component.visibilityStateMachine).toBeDefined();
ThomasGreen123 marked this conversation as resolved.
Show resolved Hide resolved
expect(component.visibilityStateMachine.allowAllApplicableTypesToSee).toBeDefined();

expect(spyVisibilityStateMachine.allowAllApplicableTypesToSee).toHaveBeenCalledWith();
ThomasGreen123 marked this conversation as resolved.
Show resolved Hide resolved

expect(spyCommentService.getNewVisibilityStateMachine).toHaveBeenCalledWith([]);
Copy link
Contributor

Choose a reason for hiding this comment

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

let's set questionShowResponsesTo in the component, and check that getNewVisibilityStateMachine is called with the appropriate params

});

it('should allow all applicable types to see when isVisibilityFollowingFeedbackQuestion is true', () => {
component.model = {
originalComment: {
isVisibilityFollowingFeedbackQuestion: true,
commentGiver: 'mockCommentGiver',
lastEditorEmail: '[email protected]',
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).toHaveBeenCalledWith();
ThomasGreen123 marked this conversation as resolved.
Show resolved Hide resolved
});
});

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: '[email protected]',
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],
});
});
ThomasGreen123 marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading