Skip to content

Commit

Permalink
feat: Added skipQuizStroke (#317)
Browse files Browse the repository at this point in the history
* feat: Added skipStrokeQuiz

* adding tests and tweaking API

---------

Co-authored-by: David Chanin <[email protected]>
  • Loading branch information
vherrmann and chanind authored May 23, 2024
1 parent 4ec4eda commit b5c0d39
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/HanziWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ export default class HanziWriter {
});
}

skipQuizStroke() {
if (this._quiz) {
this._quiz.nextStroke();
}
}

cancelQuiz() {
if (this._quiz) {
this._quiz.cancel();
Expand Down
26 changes: 18 additions & 8 deletions src/Quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ export default class Quiz {
} else {
this._handleFailure(meta);

const { showHintAfterMisses, highlightColor, strokeHighlightSpeed } =
this._options!;
const {
showHintAfterMisses,
highlightColor,
strokeHighlightSpeed,
} = this._options!;

if (
showHintAfterMisses !== false &&
Expand Down Expand Up @@ -178,13 +181,12 @@ export default class Quiz {
};
}

_handleSuccess(meta: StrokeMatchResultMeta) {
nextStroke() {
if (!this._options) return;

const { strokes, symbol } = this._character;

const {
onCorrectStroke,
onComplete,
highlightOnComplete,
strokeFadeDuration,
Expand All @@ -193,10 +195,6 @@ export default class Quiz {
strokeHighlightDuration,
} = this._options;

onCorrectStroke?.({
...this._getStrokeData({ isCorrect: true, meta }),
});

let animation: GenericMutation[] = characterActions.showStroke(
'main',
this._currentStrokeIndex,
Expand Down Expand Up @@ -228,6 +226,18 @@ export default class Quiz {
this._renderState.run(animation);
}

_handleSuccess(meta: StrokeMatchResultMeta) {
if (!this._options) return;

const { onCorrectStroke } = this._options;

onCorrectStroke?.({
...this._getStrokeData({ isCorrect: true, meta }),
});

this.nextStroke();
}

_handleFailure(meta: StrokeMatchResultMeta) {
this._mistakesOnStroke += 1;
this._totalMistakes += 1;
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/HanziWriter-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,27 @@ describe('HanziWriter', () => {
});
});

describe('skipQuizStroke', () => {
it('returns if there is not active quiz', async () => {
document.body.innerHTML = '<div id="target"></div>';
const writer = HanziWriter.create('target', '人', { charDataLoader });
await writer._withDataPromise;
expect(writer.skipQuizStroke()).toBe(undefined);
expect(writer._quiz).toBe(undefined);
});

it('skips the current stroke if a quiz is active', async () => {
document.body.innerHTML = '<div id="target"></div>';
const writer = HanziWriter.create('target', '人', { charDataLoader });
writer.quiz();
await resolvePromises();
const quiz = writer._quiz!;
writer.skipQuizStroke();
expect(quiz.nextStroke).toHaveBeenCalledTimes(1);
expect(quiz._handleSuccess).not.toHaveBeenCalled();
});
});

describe('mouse and touch events', () => {
let writer: HanziWriter;
beforeEach(async () => {
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/Quiz-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,40 @@ describe('Quiz', () => {
});
});

describe('nextStroke', () => {
it('shows the current stroke and moves to the next stroke', async () => {
const renderState = createRenderState();
const quiz = new Quiz(
char,
renderState,
new Positioner({ padding: 20, width: 200, height: 200 }),
);
const onCorrectStroke = jest.fn();
const onMistake = jest.fn();
const onComplete = jest.fn();
quiz.startQuiz(Object.assign({}, opts, { onCorrectStroke, onComplete, onMistake }));
clock.tick(1000);
await resolvePromises();

expect(quiz._currentStrokeIndex).toBe(0);
quiz.nextStroke();
await resolvePromises();

expect(quiz._userStroke).toBeUndefined();
expect(quiz._isActive).toBe(true);
expect(quiz._currentStrokeIndex).toBe(1);
expect(onCorrectStroke).not.toHaveBeenCalled();
expect(onMistake).not.toHaveBeenCalled();
expect(onComplete).not.toHaveBeenCalled();

clock.tick(1000);
await resolvePromises();

expect(renderState.state.character.main.strokes[0].opacity).toBe(1);
expect(renderState.state.character.main.strokes[1].opacity).toBe(0);
});
});

it('doesnt leave strokes partially drawn if the users finishes the quiz really fast', async () => {
(strokeMatches as any).mockImplementation(() => ({
isMatch: true,
Expand Down

0 comments on commit b5c0d39

Please sign in to comment.