@quizparts/react 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Choice.d.ts +11 -0
- package/dist/components/Choice.d.ts.map +1 -0
- package/dist/components/Choice.js +36 -0
- package/dist/components/ChoiceIndicator.d.ts +10 -0
- package/dist/components/ChoiceIndicator.d.ts.map +1 -0
- package/dist/components/ChoiceIndicator.js +19 -0
- package/dist/components/Choices.d.ts +7 -0
- package/dist/components/Choices.d.ts.map +1 -0
- package/dist/components/Choices.js +8 -0
- package/dist/components/ContinueButton.d.ts +7 -0
- package/dist/components/ContinueButton.d.ts.map +1 -0
- package/dist/components/ContinueButton.js +7 -0
- package/dist/components/DefaultQuestionLayout.d.ts +16 -0
- package/dist/components/DefaultQuestionLayout.d.ts.map +1 -0
- package/dist/components/DefaultQuestionLayout.js +20 -0
- package/dist/components/Feedback.d.ts +5 -0
- package/dist/components/Feedback.d.ts.map +1 -0
- package/dist/components/Feedback.js +8 -0
- package/dist/components/Hint.d.ts +12 -0
- package/dist/components/Hint.d.ts.map +1 -0
- package/dist/components/Hint.js +11 -0
- package/dist/components/Instruction.d.ts +10 -0
- package/dist/components/Instruction.d.ts.map +1 -0
- package/dist/components/Instruction.js +16 -0
- package/dist/components/MatchPairs.d.ts +2 -0
- package/dist/components/MatchPairs.d.ts.map +1 -0
- package/dist/components/MatchPairs.js +38 -0
- package/dist/components/NextButton.d.ts +6 -0
- package/dist/components/NextButton.d.ts.map +1 -0
- package/dist/components/NextButton.js +6 -0
- package/dist/components/OrderList.d.ts +2 -0
- package/dist/components/OrderList.d.ts.map +1 -0
- package/dist/components/OrderList.js +48 -0
- package/dist/components/Progress.d.ts +5 -0
- package/dist/components/Progress.d.ts.map +1 -0
- package/dist/components/Progress.js +6 -0
- package/dist/components/ProgressBar.d.ts +6 -0
- package/dist/components/ProgressBar.d.ts.map +1 -0
- package/dist/components/ProgressBar.js +7 -0
- package/dist/components/Prompt.d.ts +5 -0
- package/dist/components/Prompt.d.ts.map +1 -0
- package/dist/components/Prompt.js +8 -0
- package/dist/components/Question.d.ts +7 -0
- package/dist/components/Question.d.ts.map +1 -0
- package/dist/components/Question.js +8 -0
- package/dist/components/QuestionBody.d.ts +8 -0
- package/dist/components/QuestionBody.d.ts.map +1 -0
- package/dist/components/QuestionBody.js +3 -0
- package/dist/components/QuestionCard.d.ts +10 -0
- package/dist/components/QuestionCard.d.ts.map +1 -0
- package/dist/components/QuestionCard.js +3 -0
- package/dist/components/QuestionCounter.d.ts +9 -0
- package/dist/components/QuestionCounter.d.ts.map +1 -0
- package/dist/components/QuestionCounter.js +9 -0
- package/dist/components/QuestionFooter.d.ts +8 -0
- package/dist/components/QuestionFooter.d.ts.map +1 -0
- package/dist/components/QuestionFooter.js +3 -0
- package/dist/components/QuestionHeader.d.ts +8 -0
- package/dist/components/QuestionHeader.d.ts.map +1 -0
- package/dist/components/QuestionHeader.js +3 -0
- package/dist/components/QuestionInput.d.ts +9 -0
- package/dist/components/QuestionInput.d.ts.map +1 -0
- package/dist/components/QuestionInput.js +33 -0
- package/dist/components/QuizComplete.d.ts +17 -0
- package/dist/components/QuizComplete.d.ts.map +1 -0
- package/dist/components/QuizComplete.js +13 -0
- package/dist/components/QuizFlow.d.ts +24 -0
- package/dist/components/QuizFlow.d.ts.map +1 -0
- package/dist/components/QuizFlow.js +15 -0
- package/dist/components/QuizRoot.d.ts +9 -0
- package/dist/components/QuizRoot.d.ts.map +1 -0
- package/dist/components/QuizRoot.js +7 -0
- package/dist/components/SentenceBuilder.d.ts +2 -0
- package/dist/components/SentenceBuilder.d.ts.map +1 -0
- package/dist/components/SentenceBuilder.js +36 -0
- package/dist/components/SubmitButton.d.ts +7 -0
- package/dist/components/SubmitButton.d.ts.map +1 -0
- package/dist/components/SubmitButton.js +7 -0
- package/dist/components/TextInput.d.ts +7 -0
- package/dist/components/TextInput.d.ts.map +1 -0
- package/dist/components/TextInput.js +22 -0
- package/dist/components/ThemeVarsProvider.d.ts +15 -0
- package/dist/components/ThemeVarsProvider.d.ts.map +1 -0
- package/dist/components/ThemeVarsProvider.js +7 -0
- package/dist/constants/defaultInstructions.d.ts +3 -0
- package/dist/constants/defaultInstructions.d.ts.map +1 -0
- package/dist/constants/defaultInstructions.js +8 -0
- package/dist/context/QuizContext.d.ts +40 -0
- package/dist/context/QuizContext.d.ts.map +1 -0
- package/dist/context/QuizContext.js +88 -0
- package/dist/hooks/useProgress.d.ts +16 -0
- package/dist/hooks/useProgress.d.ts.map +1 -0
- package/dist/hooks/useProgress.js +22 -0
- package/dist/hooks/useQuestion.d.ts +55 -0
- package/dist/hooks/useQuestion.d.ts.map +1 -0
- package/dist/hooks/useQuestion.js +77 -0
- package/dist/hooks/useQuiz.d.ts +22 -0
- package/dist/hooks/useQuiz.d.ts.map +1 -0
- package/dist/hooks/useQuiz.js +59 -0
- package/dist/hooks/useQuizContext.d.ts +2 -0
- package/dist/hooks/useQuizContext.d.ts.map +1 -0
- package/dist/hooks/useQuizContext.js +10 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +135 -0
- package/dist/test/setup.d.ts +2 -0
- package/dist/test/setup.d.ts.map +1 -0
- package/dist/test/setup.js +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useQuizContext } from './useQuizContext.js';
|
|
3
|
+
/**
|
|
4
|
+
* Quiz-level state and actions. Use inside QuizProvider.
|
|
5
|
+
* @returns Current question, canSubmit, submitAnswer, goToNextQuestion, score, etc.
|
|
6
|
+
*/
|
|
7
|
+
export const useQuiz = () => {
|
|
8
|
+
const { session, progress, submitAnswer, goToNextQuestion, goToPreviousQuestion, resetQuiz } = useQuizContext();
|
|
9
|
+
const currentQuestionIndex = session.currentQuestionIndex;
|
|
10
|
+
const currentQuestion = session.quiz.questions[currentQuestionIndex] ?? null;
|
|
11
|
+
const questionCount = session.quiz.questions.length;
|
|
12
|
+
const canSubmit = useMemo(() => {
|
|
13
|
+
const q = currentQuestion;
|
|
14
|
+
const state = session.questionStates[currentQuestionIndex];
|
|
15
|
+
if (!q || !state)
|
|
16
|
+
return false;
|
|
17
|
+
if (state.status === 'correct' || state.status === 'incorrect')
|
|
18
|
+
return false;
|
|
19
|
+
if (q.type === 'multiple_choice') {
|
|
20
|
+
return typeof state.input.selectedChoiceId === 'string' && state.input.selectedChoiceId.length > 0;
|
|
21
|
+
}
|
|
22
|
+
if (q.type === 'multi_select') {
|
|
23
|
+
const ids = state.input.selectedChoiceIds ?? [];
|
|
24
|
+
return ids.length > 0;
|
|
25
|
+
}
|
|
26
|
+
if (q.type === 'text_input') {
|
|
27
|
+
return typeof state.input.text === 'string' && state.input.text.trim().length > 0;
|
|
28
|
+
}
|
|
29
|
+
if (q.type === 'match_pairs') {
|
|
30
|
+
const pairs = state.input.matchPairs ?? [];
|
|
31
|
+
return 'pairs' in q && Array.isArray(q.pairs) && pairs.length === q.pairs.length;
|
|
32
|
+
}
|
|
33
|
+
if (q.type === 'order_items') {
|
|
34
|
+
const ordered = state.input.orderedIds ?? [];
|
|
35
|
+
return 'items' in q && Array.isArray(q.items) && ordered.length === q.items.length;
|
|
36
|
+
}
|
|
37
|
+
if (q.type === 'sentence_builder') {
|
|
38
|
+
const order = state.input.sentenceOrder ?? [];
|
|
39
|
+
return 'answer' in q && Array.isArray(q.answer) && order.length === q.answer.length;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}, [session, currentQuestionIndex, currentQuestion]);
|
|
43
|
+
const isComplete = questionCount > 0 && progress.attemptedCount >= questionCount;
|
|
44
|
+
return {
|
|
45
|
+
quiz: session.quiz,
|
|
46
|
+
currentQuestion,
|
|
47
|
+
currentQuestionIndex,
|
|
48
|
+
questionCount,
|
|
49
|
+
canSubmit,
|
|
50
|
+
isComplete,
|
|
51
|
+
score: session.score,
|
|
52
|
+
canGoNext: progress.canGoNext,
|
|
53
|
+
canGoPrevious: progress.canGoPrevious,
|
|
54
|
+
goToNextQuestion,
|
|
55
|
+
goToPreviousQuestion,
|
|
56
|
+
submitAnswer,
|
|
57
|
+
resetQuiz,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuizContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useQuizContext.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,cAAc,8CAM1B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { getQuizContext } from '../context/QuizContext.js';
|
|
3
|
+
const MISSING_PROVIDER = 'Quiz hooks must be used within a QuizProvider. Wrap your app or quiz tree with <QuizProvider quiz={...}>.';
|
|
4
|
+
export const useQuizContext = () => {
|
|
5
|
+
const ctx = useContext(getQuizContext());
|
|
6
|
+
if (ctx == null) {
|
|
7
|
+
throw new Error(MISSING_PROVIDER);
|
|
8
|
+
}
|
|
9
|
+
return ctx;
|
|
10
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @quizparts/react – React bindings for the quiz engine.
|
|
3
|
+
*
|
|
4
|
+
* Wrap your app (or quiz section) in QuizProvider with a parsed quiz; use QuizRoot, Question,
|
|
5
|
+
* Prompt, Choices, Choice, SubmitButton, Feedback, Progress, etc. for multiple_choice and
|
|
6
|
+
* multi_select. Use TextInput for text_input, MatchPairs for match_pairs, OrderList for
|
|
7
|
+
* order_items, SentenceBuilder for sentence_builder. Hooks: useQuiz, useQuestion, useProgress.
|
|
8
|
+
*/
|
|
9
|
+
export { CORE_VERSION } from '@quizparts/core';
|
|
10
|
+
/** Current React bindings version. */
|
|
11
|
+
export declare const REACT_VERSION = "0.0.1";
|
|
12
|
+
export { QuizProvider, getQuizContext, type QuizContextValue } from './context/QuizContext.js';
|
|
13
|
+
export { useQuizContext } from './hooks/useQuizContext.js';
|
|
14
|
+
export { useQuiz } from './hooks/useQuiz.js';
|
|
15
|
+
export { useQuestion } from './hooks/useQuestion.js';
|
|
16
|
+
export { useProgress } from './hooks/useProgress.js';
|
|
17
|
+
export { QuizRoot } from './components/QuizRoot.js';
|
|
18
|
+
export { Question } from './components/Question.js';
|
|
19
|
+
export { QuestionCard } from './components/QuestionCard.js';
|
|
20
|
+
export { QuestionHeader } from './components/QuestionHeader.js';
|
|
21
|
+
export { QuestionBody } from './components/QuestionBody.js';
|
|
22
|
+
export { QuestionFooter } from './components/QuestionFooter.js';
|
|
23
|
+
export { QuestionInput } from './components/QuestionInput.js';
|
|
24
|
+
export { Prompt } from './components/Prompt.js';
|
|
25
|
+
export { Instruction } from './components/Instruction.js';
|
|
26
|
+
export { ThemeVarsProvider } from './components/ThemeVarsProvider.js';
|
|
27
|
+
export { QuizComplete } from './components/QuizComplete.js';
|
|
28
|
+
export { DefaultQuestionLayout } from './components/DefaultQuestionLayout.js';
|
|
29
|
+
export { QuizFlow } from './components/QuizFlow.js';
|
|
30
|
+
export { DEFAULT_INSTRUCTIONS } from './constants/defaultInstructions.js';
|
|
31
|
+
export { defaultTheme } from '@quizparts/theme';
|
|
32
|
+
export { createQuizFromJson } from '@quizparts/schema';
|
|
33
|
+
export { Hint } from './components/Hint.js';
|
|
34
|
+
export { Choices } from './components/Choices.js';
|
|
35
|
+
export { Choice } from './components/Choice.js';
|
|
36
|
+
export { ChoiceIndicator } from './components/ChoiceIndicator.js';
|
|
37
|
+
export { SubmitButton } from './components/SubmitButton.js';
|
|
38
|
+
export { ContinueButton } from './components/ContinueButton.js';
|
|
39
|
+
export { Feedback } from './components/Feedback.js';
|
|
40
|
+
export { Progress } from './components/Progress.js';
|
|
41
|
+
export { ProgressBar } from './components/ProgressBar.js';
|
|
42
|
+
export { QuestionCounter } from './components/QuestionCounter.js';
|
|
43
|
+
export { TextInput } from './components/TextInput.js';
|
|
44
|
+
export { NextButton } from './components/NextButton.js';
|
|
45
|
+
export { MatchPairs } from './components/MatchPairs.js';
|
|
46
|
+
export { OrderList } from './components/OrderList.js';
|
|
47
|
+
export { SentenceBuilder } from './components/SentenceBuilder.js';
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,sCAAsC;AACtC,eAAO,MAAM,aAAa,UAAU,CAAC;AAErC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,KAAK,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC/F,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAErD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @quizparts/react – React bindings for the quiz engine.
|
|
3
|
+
*
|
|
4
|
+
* Wrap your app (or quiz section) in QuizProvider with a parsed quiz; use QuizRoot, Question,
|
|
5
|
+
* Prompt, Choices, Choice, SubmitButton, Feedback, Progress, etc. for multiple_choice and
|
|
6
|
+
* multi_select. Use TextInput for text_input, MatchPairs for match_pairs, OrderList for
|
|
7
|
+
* order_items, SentenceBuilder for sentence_builder. Hooks: useQuiz, useQuestion, useProgress.
|
|
8
|
+
*/
|
|
9
|
+
export { CORE_VERSION } from '@quizparts/core';
|
|
10
|
+
/** Current React bindings version. */
|
|
11
|
+
export const REACT_VERSION = '0.0.1';
|
|
12
|
+
export { QuizProvider, getQuizContext } from './context/QuizContext.js';
|
|
13
|
+
export { useQuizContext } from './hooks/useQuizContext.js';
|
|
14
|
+
export { useQuiz } from './hooks/useQuiz.js';
|
|
15
|
+
export { useQuestion } from './hooks/useQuestion.js';
|
|
16
|
+
export { useProgress } from './hooks/useProgress.js';
|
|
17
|
+
export { QuizRoot } from './components/QuizRoot.js';
|
|
18
|
+
export { Question } from './components/Question.js';
|
|
19
|
+
export { QuestionCard } from './components/QuestionCard.js';
|
|
20
|
+
export { QuestionHeader } from './components/QuestionHeader.js';
|
|
21
|
+
export { QuestionBody } from './components/QuestionBody.js';
|
|
22
|
+
export { QuestionFooter } from './components/QuestionFooter.js';
|
|
23
|
+
export { QuestionInput } from './components/QuestionInput.js';
|
|
24
|
+
export { Prompt } from './components/Prompt.js';
|
|
25
|
+
export { Instruction } from './components/Instruction.js';
|
|
26
|
+
export { ThemeVarsProvider } from './components/ThemeVarsProvider.js';
|
|
27
|
+
export { QuizComplete } from './components/QuizComplete.js';
|
|
28
|
+
export { DefaultQuestionLayout } from './components/DefaultQuestionLayout.js';
|
|
29
|
+
export { QuizFlow } from './components/QuizFlow.js';
|
|
30
|
+
export { DEFAULT_INSTRUCTIONS } from './constants/defaultInstructions.js';
|
|
31
|
+
export { defaultTheme } from '@quizparts/theme';
|
|
32
|
+
export { createQuizFromJson } from '@quizparts/schema';
|
|
33
|
+
export { Hint } from './components/Hint.js';
|
|
34
|
+
export { Choices } from './components/Choices.js';
|
|
35
|
+
export { Choice } from './components/Choice.js';
|
|
36
|
+
export { ChoiceIndicator } from './components/ChoiceIndicator.js';
|
|
37
|
+
export { SubmitButton } from './components/SubmitButton.js';
|
|
38
|
+
export { ContinueButton } from './components/ContinueButton.js';
|
|
39
|
+
export { Feedback } from './components/Feedback.js';
|
|
40
|
+
export { Progress } from './components/Progress.js';
|
|
41
|
+
export { ProgressBar } from './components/ProgressBar.js';
|
|
42
|
+
export { QuestionCounter } from './components/QuestionCounter.js';
|
|
43
|
+
export { TextInput } from './components/TextInput.js';
|
|
44
|
+
export { NextButton } from './components/NextButton.js';
|
|
45
|
+
export { MatchPairs } from './components/MatchPairs.js';
|
|
46
|
+
export { OrderList } from './components/OrderList.js';
|
|
47
|
+
export { SentenceBuilder } from './components/SentenceBuilder.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
3
|
+
import { render, screen, act, cleanup } from '@testing-library/react';
|
|
4
|
+
import userEvent from '@testing-library/user-event';
|
|
5
|
+
afterEach(() => cleanup());
|
|
6
|
+
import { parseQuiz } from '@quizparts/schema';
|
|
7
|
+
import { REACT_VERSION, QuizProvider, useQuiz, useProgress, QuizRoot, Question, Prompt, Choices, Choice, SubmitButton, Feedback, Progress, TextInput, NextButton, } from './index.js';
|
|
8
|
+
const quizJson = {
|
|
9
|
+
id: 'test',
|
|
10
|
+
title: 'Test Quiz',
|
|
11
|
+
questions: [
|
|
12
|
+
{
|
|
13
|
+
id: 'q1',
|
|
14
|
+
type: 'multiple_choice',
|
|
15
|
+
prompt: 'What is 2+2?',
|
|
16
|
+
choices: [
|
|
17
|
+
{ id: 'a', text: '3' },
|
|
18
|
+
{ id: 'b', text: '4' },
|
|
19
|
+
],
|
|
20
|
+
answer: 'b',
|
|
21
|
+
explanation: '2+2 equals 4.',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'q2',
|
|
25
|
+
type: 'text_input',
|
|
26
|
+
prompt: 'Say hello',
|
|
27
|
+
answer: 'hello',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
const parsed = parseQuiz(quizJson);
|
|
32
|
+
const quiz = parsed.success ? parsed.data : { id: '', title: '', questions: [] };
|
|
33
|
+
describe('@quizparts/react', () => {
|
|
34
|
+
it('exports REACT_VERSION', () => {
|
|
35
|
+
expect(REACT_VERSION).toBe('0.0.1');
|
|
36
|
+
});
|
|
37
|
+
it('throws when useQuiz is used outside QuizProvider', () => {
|
|
38
|
+
const Throwing = () => {
|
|
39
|
+
useQuiz();
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
expect(() => render(_jsx(Throwing, {}))).toThrow(/QuizProvider/);
|
|
43
|
+
});
|
|
44
|
+
it('throws when useProgress is used outside QuizProvider', () => {
|
|
45
|
+
const Throwing = () => {
|
|
46
|
+
useProgress();
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
expect(() => render(_jsx(Throwing, {}))).toThrow(/QuizProvider/);
|
|
50
|
+
});
|
|
51
|
+
it('Feedback shows nothing before submit', () => {
|
|
52
|
+
render(_jsx(QuizProvider, { quiz: quiz, children: _jsxs(Question, { children: [_jsx(Prompt, {}), _jsxs(Choices, { children: [_jsx(Choice, { choiceId: "a" }), _jsx(Choice, { choiceId: "b" })] }), _jsx(Feedback, {})] }) }));
|
|
53
|
+
expect(screen.queryByText('Correct!')).not.toBeInTheDocument();
|
|
54
|
+
expect(screen.queryByText('Incorrect.')).not.toBeInTheDocument();
|
|
55
|
+
});
|
|
56
|
+
it('SubmitButton is disabled when no selection', () => {
|
|
57
|
+
render(_jsx(QuizProvider, { quiz: quiz, children: _jsxs(Question, { children: [_jsx(Prompt, {}), _jsxs(Choices, { children: [_jsx(Choice, { choiceId: "a" }), _jsx(Choice, { choiceId: "b" })] }), _jsx(SubmitButton, {})] }) }));
|
|
58
|
+
const submitBtn = screen.getAllByRole('button').find((b) => b.textContent === 'Answer');
|
|
59
|
+
expect(submitBtn).toBeDisabled();
|
|
60
|
+
});
|
|
61
|
+
it('provider and useQuiz expose quiz state', () => {
|
|
62
|
+
const Reader = () => {
|
|
63
|
+
const { quiz: q, currentQuestionIndex, questionCount } = useQuiz();
|
|
64
|
+
return (_jsxs("span", { "data-testid": "info", children: [q.title, " ", currentQuestionIndex + 1, "/", questionCount] }));
|
|
65
|
+
};
|
|
66
|
+
render(_jsx(QuizProvider, { quiz: quiz, children: _jsx(Reader, {}) }));
|
|
67
|
+
expect(screen.getByTestId('info')).toHaveTextContent('Test Quiz 1/2');
|
|
68
|
+
});
|
|
69
|
+
it('multiple choice: select and submit', async () => {
|
|
70
|
+
const user = userEvent.setup();
|
|
71
|
+
render(_jsx(QuizProvider, { quiz: quiz, children: _jsx(QuizRoot, { children: _jsxs(Question, { children: [_jsx(Prompt, {}), _jsxs(Choices, { children: [_jsx(Choice, { choiceId: "a" }), _jsx(Choice, { choiceId: "b" })] }), _jsx(SubmitButton, {}), _jsx(Feedback, {})] }) }) }));
|
|
72
|
+
expect(screen.getByText('What is 2+2?')).toBeInTheDocument();
|
|
73
|
+
const choiceB = document.querySelector('[data-choice-id="b"]');
|
|
74
|
+
if (choiceB)
|
|
75
|
+
await act(() => user.click(choiceB));
|
|
76
|
+
expect(choiceB).toHaveAttribute('data-selected');
|
|
77
|
+
const submitBtn = screen.getAllByRole('button').find((b) => b.textContent === 'Answer');
|
|
78
|
+
if (submitBtn)
|
|
79
|
+
await act(() => user.click(submitBtn));
|
|
80
|
+
expect(screen.getByText('Correct!')).toBeInTheDocument();
|
|
81
|
+
expect(screen.getByText('2+2 equals 4.')).toBeInTheDocument();
|
|
82
|
+
});
|
|
83
|
+
it('progress updates after submit and next', async () => {
|
|
84
|
+
const user = userEvent.setup();
|
|
85
|
+
render(_jsxs(QuizProvider, { quiz: quiz, children: [_jsx(Progress, {}), _jsxs(Question, { children: [_jsx(Prompt, {}), _jsxs(Choices, { children: [_jsx(Choice, { choiceId: "a" }), _jsx(Choice, { choiceId: "b" })] }), _jsx(SubmitButton, {}), _jsx(NextButton, {})] })] }));
|
|
86
|
+
expect(screen.getByLabelText(/Question 1 of 2/)).toBeInTheDocument();
|
|
87
|
+
const choiceB = document.querySelector('[data-choice-id="b"]');
|
|
88
|
+
if (choiceB)
|
|
89
|
+
await act(() => user.click(choiceB));
|
|
90
|
+
const submitBtn = screen.getAllByRole('button').find((b) => b.textContent === 'Answer');
|
|
91
|
+
if (submitBtn)
|
|
92
|
+
await act(() => user.click(submitBtn));
|
|
93
|
+
const nextBtn = screen.getByRole('button', { name: 'Next' });
|
|
94
|
+
await act(() => user.click(nextBtn));
|
|
95
|
+
expect(screen.getByText('Say hello')).toBeInTheDocument();
|
|
96
|
+
});
|
|
97
|
+
it('text input: type and submit', async () => {
|
|
98
|
+
const user = userEvent.setup();
|
|
99
|
+
const quizTextOnly = { ...quiz, questions: [quiz.questions[1]] };
|
|
100
|
+
render(_jsx(QuizProvider, { quiz: quizTextOnly, children: _jsxs(Question, { children: [_jsx(Prompt, {}), _jsx(TextInput, {}), _jsx(SubmitButton, {}), _jsx(Feedback, {})] }) }));
|
|
101
|
+
const input = screen.getByRole('textbox');
|
|
102
|
+
await act(() => user.type(input, 'hello'));
|
|
103
|
+
const submitBtn = screen.getAllByRole('button').find((b) => b.textContent === 'Answer');
|
|
104
|
+
if (submitBtn)
|
|
105
|
+
await act(() => user.click(submitBtn));
|
|
106
|
+
expect(screen.getByText('Correct!')).toBeInTheDocument();
|
|
107
|
+
});
|
|
108
|
+
it('useProgress returns current, total, percent', () => {
|
|
109
|
+
const Reader = () => {
|
|
110
|
+
const p = useProgress();
|
|
111
|
+
return (_jsxs("span", { "data-testid": "prog", children: [p.current, "/", p.total, " ", p.percent, "%"] }));
|
|
112
|
+
};
|
|
113
|
+
render(_jsx(QuizProvider, { quiz: quiz, children: _jsx(Reader, {}) }));
|
|
114
|
+
expect(screen.getByTestId('prog')).toHaveTextContent('1/2 0%');
|
|
115
|
+
});
|
|
116
|
+
it('resetQuiz restores initial state', async () => {
|
|
117
|
+
const user = userEvent.setup();
|
|
118
|
+
const ResetButton = () => {
|
|
119
|
+
const { resetQuiz: reset } = useQuiz();
|
|
120
|
+
return _jsx("button", { type: "button", onClick: () => reset(), children: "Reset" });
|
|
121
|
+
};
|
|
122
|
+
render(_jsxs(QuizProvider, { quiz: quiz, children: [_jsx(Progress, {}), _jsxs(Question, { children: [_jsx(Prompt, {}), _jsxs(Choices, { children: [_jsx(Choice, { choiceId: "a" }), _jsx(Choice, { choiceId: "b" })] }), _jsx(SubmitButton, {})] }), _jsx(ResetButton, {})] }));
|
|
123
|
+
const choiceB = document.querySelector('[data-choice-id="b"]');
|
|
124
|
+
if (!choiceB)
|
|
125
|
+
throw new Error('Choice b not found');
|
|
126
|
+
await act(() => user.click(choiceB));
|
|
127
|
+
const submitBtn = screen.getAllByRole('button').find((b) => b.textContent === 'Answer');
|
|
128
|
+
if (submitBtn)
|
|
129
|
+
await act(() => user.click(submitBtn));
|
|
130
|
+
expect(screen.getByText('Score: 1')).toBeInTheDocument();
|
|
131
|
+
await act(() => user.click(screen.getByRole('button', { name: 'Reset' })));
|
|
132
|
+
expect(screen.getByText('Score: 0')).toBeInTheDocument();
|
|
133
|
+
expect(screen.getByText('What is 2+2?')).toBeInTheDocument();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/test/setup.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom/vitest';
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quizparts/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "React UI for QuizParts (web) — start here. Provider, components, and hooks; includes engine and schema.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/ovrpro/QuizParts.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/ovrpro/QuizParts#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/ovrpro/QuizParts/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": ["quiz", "react", "components", "learning", "headless"],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": ["dist"],
|
|
25
|
+
"publishConfig": { "access": "public" },
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc",
|
|
28
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"clean": "rm -rf dist"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@quizparts/core": "0.0.1",
|
|
35
|
+
"@quizparts/schema": "0.0.1",
|
|
36
|
+
"@quizparts/theme": "0.0.1"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@testing-library/dom": "^10.4.0",
|
|
43
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
44
|
+
"@testing-library/react": "^16.0.1",
|
|
45
|
+
"@testing-library/user-event": "^14.5.2",
|
|
46
|
+
"@types/react": "^18.3.12",
|
|
47
|
+
"happy-dom": "^15.11.7",
|
|
48
|
+
"react": "^18.3.1",
|
|
49
|
+
"react-dom": "^18.3.1",
|
|
50
|
+
"typescript": "^5.6.3",
|
|
51
|
+
"vitest": "^2.1.4"
|
|
52
|
+
}
|
|
53
|
+
}
|