Skip to content

Commit

Permalink
[#12588] Add unit test for every method in CommentEditFormComponent (#…
Browse files Browse the repository at this point in the history
…12604)

* add unit test for every method in CommentEditFormComponent

* fix errors

* delete empty line

* spys assigned to a const
 comments removed
 added test for the ngOnInit() method

* fix code style

* fix code style

---------

Co-authored-by: HarryLu <[email protected]>
Co-authored-by: Wei Qing <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent e0c2032 commit 3fb5fa9
Showing 1 changed file with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ 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 {
CommentOutput,
CommentVisibilityType, FeedbackQuestionType,
FeedbackResponseDetails,
} from '../../../../types/api-output';
import { CommentVisibilityControl } from '../../../../types/comment-visibility-control';
import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module';
import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module';
import {
CommentVisibilityControlNamePipe,
CommentVisibilityTypeDescriptionPipe, CommentVisibilityTypeNamePipe,
CommentVisibilityTypeDescriptionPipe,
CommentVisibilityTypeNamePipe,
} from '../comment-visibility-setting.pipe';
import { CommentEditFormComponent } from './comment-edit-form.component';

Expand Down Expand Up @@ -42,4 +49,131 @@ describe('CommentEditFormComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

describe('ngOnInit', () => {
it('should not modify the response when response is not provided', () => {
component.ngOnInit();
expect(component.response).toBeUndefined();
});

it('should remove anonymous hash from giver and recipient when response is provided', () => {
const feedbackResponseDetails : FeedbackResponseDetails = {
questionType: FeedbackQuestionType.CONSTSUM,
};
const commentOutputs: CommentOutput[] = [];
component.response = {
isMissingResponse: true,
responseId: 'string',
giver: 'Anonymous student 123',
giverTeam: 'string',
giverSection: 'string',
recipient: 'Anonymous instructor 456',
recipientTeam: 'string',
recipientSection: 'string',
responseDetails: feedbackResponseDetails,
instructorComments: commentOutputs,
};
const response = component.response;

component.ngOnInit();

expect(response.giver).toEqual('Anonymous student');
expect(response.recipient).toEqual('Anonymous instructor');
});
});

it('should initialize with the visibility table collapsed', () => {
expect(component.isVisibilityTableExpanded).toBeFalsy();
});

it('should toggle visibility table from collapsed to expanded', () => {
expect(component.isVisibilityTableExpanded).toBeFalsy();
component.toggleVisibilityTable();
expect(component.isVisibilityTableExpanded).toBeTruthy();
});

it('should toggle visibility table from expanded to collapsed', () => {
component.isVisibilityTableExpanded = true;
expect(component.isVisibilityTableExpanded).toBeTruthy();
component.toggleVisibilityTable();
expect(component.isVisibilityTableExpanded).toBeFalsy();
});

it('should trigger model change with updated field', () => {
const testField = 'commentText';
const testData = 'Updated comment text';

const emitSpy = jest.spyOn(component.modelChange, 'emit');

component.triggerModelChange(testField, testData);

expect(emitSpy).toHaveBeenCalledWith({
...component.model,
[testField]: testData,
});
});

it('should emit the updated model when triggerModelChangeBatch is called', () => {
component.model = {
commentText: 'Initial Comment',
isUsingCustomVisibilities: false,
showCommentTo: [],
showGiverNameTo: [],
};
fixture.detectChanges();

const updatedModel = {
commentText: 'Updated Comment',
isUsingCustomVisibilities: true,
showCommentTo: ['Public'],
showGiverNameTo: ['Team'],
};

const modelChangeSpy = jest.spyOn(component.modelChange, 'emit');

component.triggerModelChangeBatch(updatedModel);

expect(modelChangeSpy).toHaveBeenCalledWith(updatedModel);
});

it('should emit the closeCommentBoxEvent when triggerCloseCommentBoxEvent is called', () => {
const closeCommentBoxEventSpy = jest.spyOn(component.closeCommentBoxEvent, 'emit');
component.triggerCloseCommentBoxEvent();
expect(closeCommentBoxEventSpy).toHaveBeenCalled();
});

it('should emit the saveCommentEvent when triggerSaveCommentEvent is called', () => {
const saveCommentEventSpy = jest.spyOn(component.saveCommentEvent, 'emit');
component.triggerSaveCommentEvent();
expect(saveCommentEventSpy).toHaveBeenCalled();
});

it('should allow and disallow visibility control and trigger model change', () => {
const isAllowed = true;
const visibilityType = CommentVisibilityType.GIVER;
const visibilityControl = CommentVisibilityControl.SHOW_COMMENT;

const allowToSee = jest.spyOn(component.visibilityStateMachine, 'allowToSee');
const disallowToSee = jest.spyOn(component.visibilityStateMachine, 'disallowToSee');
const triggerModelChangeBatch = jest.spyOn(component, 'triggerModelChangeBatch');

component.modifyVisibilityControl(isAllowed, visibilityType, visibilityControl);

expect(allowToSee).toHaveBeenCalledWith(visibilityType, visibilityControl);
expect(disallowToSee).not.toHaveBeenCalled();
expect(triggerModelChangeBatch).toHaveBeenCalledWith({
showCommentTo: [visibilityType],
showGiverNameTo: [],
});

component.modifyVisibilityControl(false, visibilityType, visibilityControl);

expect(component.visibilityStateMachine.disallowToSee).toHaveBeenCalledWith(visibilityType, visibilityControl);
expect(component.visibilityStateMachine.allowToSee).toHaveBeenCalled();

expect(component.triggerModelChangeBatch).toHaveBeenCalledWith({
showCommentTo: [],
showGiverNameTo: [],
});
});
});

0 comments on commit 3fb5fa9

Please sign in to comment.