mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-01 08:05:25 -05:00
refactor: quiz challenge question format (#56285)
This commit is contained in:
committed by
GitHub
parent
e95b040762
commit
ffe765dac1
@@ -12,7 +12,7 @@ exports[`add-quizzes plugin should match the quizzes snapshot 1`] = `
|
||||
"<p>Quiz 1, question 1, distractor 2</p>",
|
||||
"<p>Quiz 1, question 1, distractor 3</p>",
|
||||
],
|
||||
"question": "<p>Quiz 1, question 1</p>",
|
||||
"text": "<p>Quiz 1, question 1</p>",
|
||||
},
|
||||
{
|
||||
"answer": "<p>Quiz 1, question 2, answer</p>",
|
||||
@@ -21,7 +21,7 @@ exports[`add-quizzes plugin should match the quizzes snapshot 1`] = `
|
||||
"<p>Quiz 1, question 2, distractor 2</p>",
|
||||
"<p>Quiz 1, question 2, distractor 3</p>",
|
||||
],
|
||||
"question": "<p>Quiz 1, question 2</p>",
|
||||
"text": "<p>Quiz 1, question 2</p>",
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -34,7 +34,7 @@ exports[`add-quizzes plugin should match the quizzes snapshot 1`] = `
|
||||
"<p>Quiz 2, question 1, distractor 2</p>",
|
||||
"<p>Quiz 2, question 1, distractor 3</p>",
|
||||
],
|
||||
"question": "<p>Quiz 2, question 1</p>",
|
||||
"text": "<p>Quiz 2, question 1</p>",
|
||||
},
|
||||
{
|
||||
"answer": "<p>Quiz 2, question 2, answer</p>",
|
||||
@@ -43,7 +43,7 @@ exports[`add-quizzes plugin should match the quizzes snapshot 1`] = `
|
||||
"<p>Quiz 2, question 2, distractor 2</p>",
|
||||
"<p>Quiz 2, question 2, distractor 3</p>",
|
||||
],
|
||||
"question": "<p>Quiz 2, question 2</p>",
|
||||
"text": "<p>Quiz 2, question 2</p>",
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -21,17 +21,7 @@ function plugin() {
|
||||
|
||||
quizSections.forEach(quizNodes => {
|
||||
const quizQuestions = [];
|
||||
const questionTrees = [];
|
||||
|
||||
quizNodes.forEach(quizNode => {
|
||||
const isStartOfQuestion =
|
||||
quizNode.children?.[0]?.value === '--question--';
|
||||
if (isStartOfQuestion) {
|
||||
questionTrees.push([quizNode]);
|
||||
} else {
|
||||
questionTrees[questionTrees.length - 1].push(quizNode);
|
||||
}
|
||||
});
|
||||
const questionTrees = getAllSections(root(quizNodes), `--question--`);
|
||||
|
||||
if (questionTrees.length === 0) {
|
||||
throw Error(
|
||||
@@ -42,12 +32,12 @@ function plugin() {
|
||||
questionTrees.forEach(singleQuestionNodes => {
|
||||
const questionTree = root(singleQuestionNodes);
|
||||
|
||||
const questionNodes = getSection(questionTree, '--question--');
|
||||
const textNodes = getSection(questionTree, '--text--');
|
||||
const distractorNodes = getSection(questionTree, '--distractors--');
|
||||
const answerNodes = getSection(questionTree, '--answer--');
|
||||
|
||||
quizQuestions.push(
|
||||
getQuestion(questionNodes, distractorNodes, answerNodes)
|
||||
getQuestion(textNodes, distractorNodes, answerNodes)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -61,17 +51,17 @@ function plugin() {
|
||||
}
|
||||
}
|
||||
|
||||
function getQuestion(questionNodes, distractorNodes, answerNodes) {
|
||||
const question = mdastToHtml(questionNodes);
|
||||
function getQuestion(textNodes, distractorNodes, answerNodes) {
|
||||
const text = mdastToHtml(textNodes);
|
||||
const distractors = getDistractors(distractorNodes);
|
||||
const answer = mdastToHtml(answerNodes);
|
||||
|
||||
if (!question) throw Error('--question-- is missing from the quiz');
|
||||
if (!text) throw Error('--text-- is missing from the quiz question');
|
||||
if (!distractors)
|
||||
throw Error('--distractors-- are missing from quiz question');
|
||||
if (!answer) throw Error('--answer-- is missing from quiz question');
|
||||
|
||||
return { question, distractors, answer };
|
||||
return { text, distractors, answer };
|
||||
}
|
||||
|
||||
function getDistractors(distractorsNodes) {
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
const mockQuizzesAST = require('../__fixtures__/ast-with-quizzes.json');
|
||||
const parseFixture = require('./../__fixtures__/parse-fixture');
|
||||
const addQuizzes = require('./add-quizzes');
|
||||
|
||||
describe('add-quizzes plugin', () => {
|
||||
let mockQuizzesAST;
|
||||
const plugin = addQuizzes();
|
||||
let file = { data: {} };
|
||||
|
||||
beforeAll(async () => {
|
||||
mockQuizzesAST = await parseFixture('with-quizzes.md');
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
file = { data: {} };
|
||||
});
|
||||
@@ -35,8 +40,8 @@ describe('add-quizzes plugin', () => {
|
||||
expect(questions.length).toBe(2);
|
||||
|
||||
questions.forEach(question => {
|
||||
expect(question).toHaveProperty('question');
|
||||
expect(typeof question.question).toBe('string');
|
||||
expect(question).toHaveProperty('text');
|
||||
expect(typeof question.text).toBe('string');
|
||||
expect(question).toHaveProperty('answer');
|
||||
expect(typeof question.answer).toBe('string');
|
||||
expect(question).toHaveProperty('distractors');
|
||||
|
||||
Reference in New Issue
Block a user