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] Improve test code coverage of core components #13000

Closed
wants to merge 5 commits into from
Closed
Changes from all 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 @@ -23,7 +23,28 @@
fixture.detectChanges();
});

it('should create', () => {
it('should create: should return true if compnent is successfully created', () => {
expect(component).toBeTruthy();
});
});

it('updateNoneOfTheAbove: should update answers based on isNoneOfTheAboveEnabled', () => {
const component = new MsqQuestionEditAnswerFormComponent();
Copy link
Contributor

Choose a reason for hiding this comment

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

component is declared in beforeEach
this can be deleted

component.responseDetails = {
Copy link
Contributor

Choose a reason for hiding this comment

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

the type of responseDetails is
FeedbackMsqResponseDetails extends FeedbackResponseDetails
so the three missing properties and their types are

isOther: boolean
otherFieldContent: string
questionType: FeedbackQuestionType

FeedbackQuestionType is an enum

use createBuilder to create responseDetails so you don't have to enter all four properties every time you create responseDetails, here is an example of createBuilder
https://teammates.github.io/teammates/unit-testing.html#creating-test-data

you will need these imports

import { createBuilder } from '../../../../test-helpers/generic-builder';
import { FeedbackMsqResponseDetails } from '../../../../types/api-output';
import { FeedbackQuestionType } from '../../../../types/api-request';

answers: ['A', 'B', 'C',]

Check failure on line 34 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Unexpected trailing comma

Check failure on line 34 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Missing trailing comma

Check failure on line 34 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

Unexpected trailing comma

Check failure on line 34 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

Missing trailing comma
};

// Simulate isNoneOfTheAboveEnabled being false
component.updateNoneOfTheAbove();

expect(component.responseDetails.answers).toEqual(['NoneOfTheAbove']);
Copy link
Contributor

@Andy-W-Developer Andy-W-Developer May 25, 2024

Choose a reason for hiding this comment

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

responseDetails has an @Input() decorator so it is set by the parent component, not updateNoneOfTheAbove()
responseDetails.answers is not used for data binding in the html

so instead of testing the value of responseDetails.answers
test the behavior of updateNoneOfTheAbove()

it calls this.triggerResponseDetailsChangeBatch() with values for the properties answers: isOther: otherFieldContent:

use testEventEmission
https://teammates.github.io/teammates/unit-testing.html#testing-event-emission

import testEventEmission from '../../../../test-helpers/test-event-emitter';

you will need to look at the parent class of MsqQuestionEditAnswerFormComponent
which is QuestionEditAnswerFormComponent

it is in the file question-edit-answer-form.ts, the same folder as your test


// Simulate isNoneOfTheAboveEnabled being true
component.responseDetails = {
answers: ['A', 'B', 'C',]

Check failure on line 44 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Unexpected trailing comma

Check failure on line 44 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Missing trailing comma

Check failure on line 44 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

Unexpected trailing comma

Check failure on line 44 in src/web/app/components/question-types/question-edit-answer-form/msq-question-edit-answer-form.component.spec.ts

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

Missing trailing comma
};

component.updateNoneOfTheAbove();

expect(component.responseDetails.answers).toEqual([]);
});
Loading