-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating Article Renderer to be able to return the focusedElement so that we can scroll these elements into view above the keypad. #755
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
77ea490
Going to make a PR so that I can test this in webapp. I feel like sto…
SonicScrewdriver 017f78f
Changeset
SonicScrewdriver 827c7e4
commenting the console logs for now as I will likely be coming back t…
SonicScrewdriver e7cab0a
Cleaning up PR
SonicScrewdriver 5357dd1
PR feedback
SonicScrewdriver 5c20503
Missed a console log
SonicScrewdriver 74d7b8c
Added tests
SonicScrewdriver 807202a
Minor variable name updates to ensure clarity
SonicScrewdriver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": minor | ||
--- | ||
|
||
Adding logic to ArticleRenderer so that it can return our currently focused element. |
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
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
131 changes: 131 additions & 0 deletions
131
packages/perseus/src/__tests__/article-renderer.test.tsx
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,131 @@ | ||
import { | ||
StatefulKeypadContextProvider, | ||
KeypadContext, | ||
} from "@khanacademy/math-input"; | ||
import {RenderStateRoot} from "@khanacademy/wonder-blocks-core"; | ||
import {screen, render, fireEvent, waitFor} from "@testing-library/react"; | ||
import * as React from "react"; | ||
import "@testing-library/jest-dom"; // Imports custom matchers | ||
|
||
import { | ||
testDependencies, | ||
testDependenciesV2, | ||
} from "../../../../testing/test-dependencies"; | ||
import KeypadSwitch from "../../../math-input/src/components/keypad-switch"; | ||
import {articleWithExpression} from "../__testdata__/article-renderer.testdata"; | ||
import ArticleRenderer from "../article-renderer"; | ||
import * as Dependencies from "../dependencies"; | ||
import {ApiOptions} from "../perseus-api"; | ||
|
||
import type {APIOptions} from "../types"; | ||
|
||
function KeypadWithContext() { | ||
return ( | ||
<KeypadContext.Consumer> | ||
{({setKeypadElement}) => { | ||
return ( | ||
<KeypadSwitch | ||
onElementMounted={setKeypadElement} | ||
onDismiss={() => {}} | ||
onAnalyticsEvent={async () => {}} | ||
useV2Keypad | ||
/> | ||
); | ||
}} | ||
</KeypadContext.Consumer> | ||
); | ||
} | ||
// This looks alot like `widgets/__tests__/renderQuestion.jsx', except we use | ||
// the ArticleRenderer instead of Renderer | ||
export const RenderArticle = ( | ||
apiOptions: APIOptions = Object.freeze({}), | ||
): { | ||
container: HTMLElement; | ||
renderer: ArticleRenderer; | ||
} => { | ||
let renderer: ArticleRenderer | null = null; | ||
const {container} = render( | ||
<RenderStateRoot> | ||
<StatefulKeypadContextProvider> | ||
<KeypadContext.Consumer> | ||
{({keypadElement, setRenderer}) => ( | ||
<ArticleRenderer | ||
ref={(node) => { | ||
renderer = node; | ||
setRenderer(node); | ||
}} | ||
json={articleWithExpression} | ||
dependencies={testDependenciesV2} | ||
apiOptions={{...apiOptions}} | ||
keypadElement={keypadElement} | ||
/> | ||
)} | ||
</KeypadContext.Consumer> | ||
<KeypadWithContext /> | ||
</StatefulKeypadContextProvider> | ||
</RenderStateRoot>, | ||
); | ||
if (!renderer) { | ||
throw new Error(`Failed to render!`); | ||
} | ||
return {container, renderer}; | ||
}; | ||
|
||
describe("article renderer", () => { | ||
beforeEach(() => { | ||
// Mock ResizeObserver used by the mobile keypad | ||
window.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
disconnect: jest.fn(), | ||
})); | ||
|
||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue( | ||
testDependencies, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
// The Renderer uses a timer to wait for widgets to complete rendering. | ||
// If we don't spin the timers here, then the timer fires in the test | ||
// _after_ and breaks it because we do setState() in the callback, | ||
// and by that point the component has been unmounted. | ||
jest.runOnlyPendingTimers(); | ||
}); | ||
|
||
it("should render the content", () => { | ||
// Arrange and Act | ||
RenderArticle({ | ||
...ApiOptions.defaults, | ||
isMobile: false, | ||
customKeypad: false, | ||
}); | ||
|
||
// Assert | ||
expect(screen.getByRole("textbox")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should call the onFocusChanged callback when an input is focused", async () => { | ||
// Arrange | ||
const answerableCallback = jest.fn(); | ||
|
||
// Act | ||
RenderArticle({ | ||
...ApiOptions.defaults, | ||
onFocusChange: answerableCallback, | ||
isMobile: true, | ||
customKeypad: true, | ||
}); | ||
|
||
const input = screen.getByLabelText( | ||
"Math input box Tap with one or two fingers to open keyboard", | ||
); | ||
|
||
fireEvent.touchStart(input); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByRole("button", {name: "4"})).toBeVisible(); | ||
}); | ||
expect(answerableCallback).toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!