mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-16 22:00:28 -05:00
fix(tools): improve video questions validation (#64176)
Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const { root } = require('mdast-builder');
|
||||
const find = require('unist-util-find');
|
||||
const { getSection } = require('./utils/get-section');
|
||||
const { getSection, getAllSections } = require('./utils/get-section');
|
||||
const getAllBefore = require('./utils/before-heading');
|
||||
const { getParagraphContent } = require('./utils/get-paragraph-content');
|
||||
|
||||
@@ -20,6 +20,12 @@ function plugin() {
|
||||
if (!text) throw Error('text is missing from question');
|
||||
if (!answers) throw Error('answers are missing from question');
|
||||
if (!solution) throw Error('solution is missing from question');
|
||||
if (solution > answers.length)
|
||||
throw Error(
|
||||
`solution must be within range of number of answers: 1-${answers.length}`
|
||||
);
|
||||
if (answers[solution - 1].feedback)
|
||||
throw Error('answer selected as solution cannot have feedback section');
|
||||
|
||||
return { text, answers, solution };
|
||||
}
|
||||
@@ -27,11 +33,16 @@ function plugin() {
|
||||
function getAnswers(answersNodes) {
|
||||
const answerGroups = splitOnThematicBreak(answersNodes);
|
||||
|
||||
return answerGroups.map(answerGroup => {
|
||||
return answerGroups.map((answerGroup, index) => {
|
||||
const answerTree = root(answerGroup);
|
||||
const feedbackNodes = getSection(answerTree, '--feedback--');
|
||||
const feedbackGroups = getAllSections(answerTree, '--feedback--');
|
||||
|
||||
if (feedbackGroups.length > 1)
|
||||
throw new Error(`answer ${index + 1} has multiple feedback sections`);
|
||||
|
||||
const [feedbackNodes] = feedbackGroups;
|
||||
const audioIdNodes = getSection(answerTree, '--audio-id--');
|
||||
const hasFeedback = feedbackNodes.length > 0;
|
||||
const hasFeedback = feedbackNodes?.length > 0;
|
||||
const hasAudioId = audioIdNodes.length > 0;
|
||||
|
||||
if (hasFeedback || hasAudioId) {
|
||||
|
||||
@@ -8,6 +8,9 @@ describe('add-video-question plugin', () => {
|
||||
multipleQuestionAST,
|
||||
videoOutOfOrderAST,
|
||||
videoWithAudioAST,
|
||||
videoWithSolutionAboveNumberOfAnswersAST,
|
||||
videoWithFeedbackTwiceInARow,
|
||||
videoWithCorrectAnswerWithFeedback,
|
||||
chineseVideoAST;
|
||||
const plugin = addVideoQuestion();
|
||||
let file = { data: {} };
|
||||
@@ -22,6 +25,15 @@ describe('add-video-question plugin', () => {
|
||||
'with-video-question-out-of-order.md'
|
||||
);
|
||||
videoWithAudioAST = await parseFixture('with-video-question-audio.md');
|
||||
videoWithSolutionAboveNumberOfAnswersAST = await parseFixture(
|
||||
'with-video-question-solution-above-number-of-answers.md'
|
||||
);
|
||||
videoWithFeedbackTwiceInARow = await parseFixture(
|
||||
'with-video-question-feedback-twice-in-a-row.md'
|
||||
);
|
||||
videoWithCorrectAnswerWithFeedback = await parseFixture(
|
||||
'with-video-question-correct-answer-with-feedback.md'
|
||||
);
|
||||
chineseVideoAST = await parseFixture('with-chinese-mcq.md');
|
||||
});
|
||||
|
||||
@@ -109,6 +121,27 @@ describe('add-video-question plugin', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw if solution is higher than the number of answers', () => {
|
||||
expect.assertions(1);
|
||||
expect(() =>
|
||||
plugin(videoWithSolutionAboveNumberOfAnswersAST, file)
|
||||
).toThrow('solution must be within range of number of answers: 1-3');
|
||||
});
|
||||
|
||||
it('should throw if answer has more than one feedback section', () => {
|
||||
expect.assertions(1);
|
||||
expect(() => plugin(videoWithFeedbackTwiceInARow, file)).toThrow(
|
||||
'answer 2 has multiple feedback sections'
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw if correct answer has feedback section', () => {
|
||||
expect.assertions(1);
|
||||
expect(() => plugin(videoWithCorrectAnswerWithFeedback, file)).toThrow(
|
||||
'answer selected as solution cannot have feedback section'
|
||||
);
|
||||
});
|
||||
|
||||
it('should NOT throw if there is no question', () => {
|
||||
expect.assertions(1);
|
||||
expect(() => plugin(simpleAST, file)).not.toThrow();
|
||||
|
||||
Reference in New Issue
Block a user