Files
heala-backend/src/modules/admin-panel/admin-panel.ranges.spec.ts
T

77 lines
2.9 KiB
TypeScript

import { describe, expect, it, jest } from '@jest/globals';
import { Repository } from 'typeorm';
import { I18nService } from 'nestjs-i18n';
import { TestsService } from '@/modules/tests/providers/tests.service';
import { ResultRange } from '@/modules/scoring/entities/result-range.entity';
import { ResultRangeCommandProvider } from '@/modules/scoring/providers/result-range-command.provider';
import { Question } from '@/modules/questions/entities/question.entity';
import { QuestionRangeRule } from '@/modules/questions/entities/question-range-rule.entity';
import { QuestionRangeRuleCommandProvider } from '@/modules/questions/providers/range-rule/question-range-rule-command.provider';
describe.each(['result', 'question'] as const)('%s range updates', (kind) => {
function setup(overlaps: unknown[]) {
const andWhere = jest.fn<(...args: unknown[]) => unknown>();
const builder = {
where: jest.fn<(...args: unknown[]) => unknown>(),
andWhere,
getMany: jest.fn<() => Promise<unknown[]>>().mockResolvedValue(overlaps),
};
builder.where.mockReturnValue(builder);
andWhere.mockReturnValue(builder);
const save = jest
.fn<(value: unknown) => Promise<unknown>>()
.mockImplementation((value) => Promise.resolve(value));
const repository = {
createQueryBuilder: () => builder,
findOne: () =>
Promise.resolve({
id: 7,
test: { id: 2 },
question: { id: 3 },
minScore: 0,
maxScore: 5,
min: 0,
max: 5,
}),
save,
};
const update =
kind === 'result'
? () =>
new ResultRangeCommandProvider(
repository as unknown as Repository<ResultRange>,
{} as TestsService,
{} as I18nService,
).updateOne(7, { minScore: 20, maxScore: 30 })
: () =>
new QuestionRangeRuleCommandProvider(
repository as unknown as Repository<QuestionRangeRule>,
{} as Repository<Question>,
{} as I18nService,
).updateOne(7, { min: 20, max: 30 });
return { update, andWhere, save };
}
it('rejects even one overlapping different record after moving a range', async () => {
const { update, andWhere, save } = setup([{ id: 99 }]);
await expect(update()).rejects.toThrow();
expect(andWhere).toHaveBeenCalledWith(
expect.stringContaining('id != :excludeId'),
{ excludeId: 7 },
);
expect(save).not.toHaveBeenCalled();
});
it('allows non-overlapping edits and preserves entity relations', async () => {
const { update, save } = setup([]);
await update();
expect(save).toHaveBeenCalledWith(
expect.objectContaining(
kind === 'result'
? { test: { id: 2 }, testId: 2, minScore: 20, maxScore: 30 }
: { question: { id: 3 }, questionId: 3, min: 20, max: 30 },
),
);
});
});