-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out validation logic for iframe (#1705)
## Summary: For the server-side scoring project, we are moving validation logic out of each widget component. This moves the validation logic for the iframe widget and adds tests for the validator function. Issue: LEMS-2480 ## Test plan: - Confirm all tests pass - Confirm iframe stories still work in a ZND Author: Myranae Reviewers: Myranae, handeyeco Required Reviewers: Approved By: handeyeco Checks: ✅ gerald, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1705
- Loading branch information
Showing
4 changed files
with
91 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
Move validation logic out of the iframe widget and add tests |
49 changes: 49 additions & 0 deletions
49
packages/perseus/src/widgets/iframe/iframe-validator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {iframeValidator} from "./iframe-validator"; | ||
|
||
import type {PerseusIFrameUserInput} from "../../validation.types"; | ||
|
||
describe("iframeValidator", () => { | ||
it("is correct when the state from the iframe shows the status is correct", () => { | ||
// Arrange | ||
const state: PerseusIFrameUserInput = { | ||
status: "correct", | ||
message: "Good job!", | ||
}; | ||
|
||
// Act | ||
const result = iframeValidator(state); | ||
|
||
// Assert | ||
expect(result).toHaveBeenAnsweredCorrectly(); | ||
}); | ||
|
||
it("is incorrect when the state from the iframe shows the status is incorrect", () => { | ||
// Arrange | ||
const state: PerseusIFrameUserInput = { | ||
status: "incorrect", | ||
message: "Try again!", | ||
}; | ||
|
||
// Act | ||
const result = iframeValidator(state); | ||
|
||
// Assert | ||
expect(result).toHaveBeenAnsweredIncorrectly(); | ||
}); | ||
|
||
// Note: It looks like the iframe only says if the answer is correct or | ||
// incorrect, but status is set to "incomplete" by default. | ||
it("should return invalid score before user interactions", () => { | ||
// Arrange | ||
const state: PerseusIFrameUserInput = { | ||
status: "incomplete", | ||
message: null, | ||
}; | ||
|
||
// Act | ||
const result = iframeValidator(state); | ||
|
||
// Assert | ||
expect(result).toHaveInvalidInput("Keep going, you're not there yet!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type {PerseusScore} from "../../types"; | ||
import type {PerseusIFrameUserInput} from "../../validation.types"; | ||
|
||
export function iframeValidator(state: PerseusIFrameUserInput): PerseusScore { | ||
// The iframe can tell us whether it's correct or incorrect, | ||
// and pass an optional message | ||
if (state.status === "correct") { | ||
return { | ||
type: "points", | ||
earned: 1, | ||
total: 1, | ||
message: state.message || null, | ||
}; | ||
} | ||
if (state.status === "incorrect") { | ||
return { | ||
type: "points", | ||
earned: 0, | ||
total: 1, | ||
message: state.message || null, | ||
}; | ||
} | ||
return { | ||
type: "invalid", | ||
message: "Keep going, you're not there yet!", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters