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 tests to question constraint #12747

Merged
Merged
Show file tree
Hide file tree
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
@@ -1,16 +1,28 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';

import { ConstsumRecipientsQuestionConstraintComponent } from './constsum-recipients-question-constraint.component';
import { createBuilder } from '../../../../test-helpers/generic-builder';
import { FeedbackConstantSumResponseDetails } from '../../../../types/api-output';
import { FeedbackQuestionType } from '../../../../types/api-request';
import { FeedbackResponseRecipientSubmissionFormModel }
from '../../question-submission-form/question-submission-form-model';

describe('ConstsumRecipientsQuestionConstraintComponent', () => {
let component: ConstsumRecipientsQuestionConstraintComponent;
let fixture: ComponentFixture<ConstsumRecipientsQuestionConstraintComponent>;

const formBuilder = createBuilder<FeedbackResponseRecipientSubmissionFormModel>({
responseId: '123',
recipientIdentifier: 'recipient123',
isValid: true,
responseDetails: { questionType: FeedbackQuestionType.CONSTSUM },
});

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ConstsumRecipientsQuestionConstraintComponent],
})
.compileComponents();
.compileComponents();
}));

beforeEach(() => {
Expand All @@ -22,4 +34,97 @@ describe('ConstsumRecipientsQuestionConstraintComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('allAnswers: should return an empty array if no forms provided', () => {
component.recipientSubmissionForms = [];
expect(component.allAnswers).toEqual([]);
});

it('allAnswers: should return 0 if the answers array is empty', () => {
const details: FeedbackConstantSumResponseDetails = {
questionType: FeedbackQuestionType.CONSTSUM,
answers: [],
};
const form = formBuilder.responseDetails(details).build();

component.recipientSubmissionForms = [form];
expect(component.allAnswers).toEqual([0]);
});

it('allAnswers: should return the first answer if it exists', () => {
const details: FeedbackConstantSumResponseDetails = {
questionType: FeedbackQuestionType.CONSTSUM,
answers: [5, 10, 15],
};
const form = formBuilder.responseDetails(details).build();

component.recipientSubmissionForms = [form];
expect(component.allAnswers).toEqual([5]);
});

it('allAnswers: should return the first answers of multiple forms', () => {
const detailsOne: FeedbackConstantSumResponseDetails = {
questionType: FeedbackQuestionType.CONSTSUM,
answers: [5, 10, 15],
};
const formOne = formBuilder.responseDetails(detailsOne).build();

const detailsTwo: FeedbackConstantSumResponseDetails = {
questionType: FeedbackQuestionType.CONSTSUM,
answers: [3, 6, 9],
};
const formTwo = formBuilder.responseDetails(detailsTwo).build();

component.recipientSubmissionForms = [formOne, formTwo];
expect(component.allAnswers).toEqual([5, 3]);
});

it('isAllPointsUneven: should return true when all points are unique', () => {
const mockAllAnswers = jest.fn().mockReturnValue([1, 2, 3]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isAllPointsUneven).toEqual(true);
});

it('isAllPointsUneven: should return false when some points are repeated', () => {
const mockAllAnswers = jest.fn().mockReturnValue([1, 2, 2, 3]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isAllPointsUneven).toEqual(false);
});

it('isAllPointsUneven: should return true when there are no points', () => {
const mockAllAnswers = jest.fn().mockReturnValue([]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isAllPointsUneven).toEqual(true);
});

it('isSomePointsUneven: should return true when length is 1', () => {
const mockAllAnswers = jest.fn().mockReturnValue([1]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isSomePointsUneven).toEqual(true);
});

it('isSomePointsUneven: should return true when there are multiple points and some are different', () => {
const mockAllAnswers = jest.fn().mockReturnValue([1, 2, 3]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isSomePointsUneven).toEqual(true);
});

it('isSomePointsUneven: should return false when all answers are the same and length is greater than 1', () => {
const mockAllAnswers = jest.fn().mockReturnValue([2, 2, 2]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isSomePointsUneven).toEqual(false);
});

it('isSomePointsUneven: should return true when there are no points', () => {
const mockAllAnswers = jest.fn().mockReturnValue([]);
Object.defineProperty(component, 'allAnswers', { get: mockAllAnswers });

expect(component.isSomePointsUneven).toEqual(true);
});
});
Loading
Loading