-
Notifications
You must be signed in to change notification settings - Fork 367
Fix flaky TeX Chromatic snapshots for Matcher and Sorter widgets #3692
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
Open
Myranae
wants to merge
11
commits into
main
Choose a base branch
from
tb/matcher-sorter-asset-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a9b5278
[tb/matcher-sorter-asset-tracking] Cherry pick TeX story commits
Myranae bc1055e
[tb/matcher-sorter-asset-tracking] docs(changeset): Fix flakey TeX Ch…
Myranae 2413fb9
[tb/matcher-sorter-asset-tracking] Add the withAssetContextHOC
Myranae 8deaa2c
[tb/matcher-sorter-asset-tracking] Update comments
Myranae 6c5f65d
[tb/matcher-sorter-asset-tracking] Update sorter and matcher to use w…
Myranae 2a3938a
[tb/matcher-sorter-asset-tracking] Add unit tests
Myranae aa577cb
[tb/matcher-sorter-asset-tracking] Merge branch 'main' into tb/matche…
Myranae aec3822
[tb/matcher-sorter-asset-tracking] Connect to asset.context provider
Myranae 0967064
[tb/matcher-sorter-asset-tracking] Add test for asymmetric height case
Myranae f0f3db2
[tb/matcher-sorter-asset-tracking] Fix Matcher settling for asymmetri…
Myranae 2be2a63
[tb/matcher-sorter-asset-tracking] Lint fixes
Myranae 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 hidden or 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 | ||
| --- | ||
|
|
||
| Fix flakey TeX Chromatic snapshots for Matcher and Sorter widgets. |
94 changes: 94 additions & 0 deletions
94
packages/perseus/src/components/__tests__/with-asset-context.test.tsx
This file contains hidden or 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,94 @@ | ||
| import {render, screen} from "@testing-library/react"; | ||
| import * as React from "react"; | ||
|
|
||
| import AssetContext from "../../asset-context"; | ||
| import {withAssetContext} from "../with-asset-context"; | ||
|
|
||
| // Minimal test-only component used to exercise withAssetContext. It accepts | ||
| // the props the HOC injects (`setAssetStatus` and `assetKey`) and surfaces | ||
| // them in two ways the tests can observe: as a `data-asset-key` attribute on | ||
| // the rendered button (for inspecting which key the HOC generated), and via | ||
| // a click handler that calls `setAssetStatus` (so tests can verify the | ||
| // injected function reaches the wrapped component and is callable). | ||
| type MockComponentProps = { | ||
| label: string; | ||
| setAssetStatus: (assetKey: string, loaded: boolean) => void; | ||
| assetKey: string; | ||
| }; | ||
|
|
||
| function MockComponent(props: MockComponentProps): React.ReactElement { | ||
| return ( | ||
| <button | ||
| onClick={() => props.setAssetStatus(props.assetKey, true)} | ||
| data-testid={props.label} | ||
| data-asset-key={props.assetKey} | ||
| > | ||
| {props.label} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| describe("withAssetContext", () => { | ||
| it("injects setAssetStatus from AssetContext as a prop", () => { | ||
| // Arrange | ||
| const setAssetStatusSpy = jest.fn(); | ||
| const Wrapped = withAssetContext(MockComponent, "widget"); | ||
|
|
||
| // Act — clicking calls setAssetStatus via the injected prop, which | ||
| // proves the function from the Provider reached MockComponent. | ||
| render( | ||
| <AssetContext.Provider | ||
| value={{ | ||
| assetStatuses: {}, | ||
| setAssetStatus: setAssetStatusSpy, | ||
| }} | ||
| > | ||
| <Wrapped label="hello" /> | ||
| </AssetContext.Provider>, | ||
| ); | ||
| screen.getByTestId("hello").click(); | ||
|
|
||
| // Assert | ||
| expect(setAssetStatusSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("injects an assetKey with the configured prefix", () => { | ||
| // Arrange, Act | ||
| const Wrapped = withAssetContext(MockComponent, "widget"); | ||
| render(<Wrapped label="hello" />); | ||
|
|
||
| // Assert | ||
| expect(screen.getByTestId("hello").dataset.assetKey).toMatch( | ||
| /^widget-/, | ||
| ); | ||
| }); | ||
|
|
||
| it("generates a unique assetKey per rendered instance", () => { | ||
| // Arrange, Act | ||
| const Wrapped = withAssetContext(MockComponent, "widget"); | ||
| render( | ||
| <> | ||
| <Wrapped label="first" /> | ||
| <Wrapped label="second" /> | ||
| </>, | ||
| ); | ||
|
|
||
| // Assert | ||
| const first = screen.getByTestId("first"); | ||
| const second = screen.getByTestId("second"); | ||
| expect(first.dataset.assetKey).not.toBe(second.dataset.assetKey); | ||
| }); | ||
|
|
||
| it("uses the AssetContext default no-op when no Provider is above", () => { | ||
| // Arrange, Act — render without an AssetContext.Provider. | ||
| const Wrapped = withAssetContext(MockComponent, "widget"); | ||
| render(<Wrapped label="hello" />); | ||
|
|
||
| // Assert — the HOC still generates an assetKey (useId works without | ||
| // a Provider) and the default no-op setAssetStatus is callable (so | ||
| // the click handler doesn't crash). | ||
| const button = screen.getByTestId("hello"); | ||
| expect(button.dataset.assetKey).toMatch(/^widget-/); | ||
| expect(() => button.click()).not.toThrow(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,43 @@ | ||
| import * as React from "react"; | ||
|
|
||
| import AssetContext from "../asset-context"; | ||
|
|
||
| type SetAssetStatus = (assetKey: string, loaded: boolean) => void; | ||
|
|
||
| type InjectedProps = { | ||
| setAssetStatus: SetAssetStatus; | ||
| assetKey: string; | ||
| }; | ||
|
|
||
| export function withAssetContext<P extends InjectedProps>( | ||
| WrappedComponent: React.ComponentType<P>, | ||
| keyPrefix: string, | ||
| ): React.ForwardRefExoticComponent< | ||
| React.PropsWithoutRef<Omit<P, keyof InjectedProps>> & | ||
| React.RefAttributes<any> | ||
| > { | ||
| const WithAssetContextComponent = React.forwardRef< | ||
| any, | ||
| Omit<P, keyof InjectedProps> | ||
| >((props, ref) => { | ||
| const {setAssetStatus} = React.useContext(AssetContext); | ||
| const uniqueId = React.useId(); | ||
| const assetKey = `${keyPrefix}-${uniqueId}`; | ||
|
|
||
| return ( | ||
| <WrappedComponent | ||
| // eslint-disable-next-line no-restricted-syntax -- TypeScript can't verify that spreading `Omit<P, K>` + adding `K`-properties reconstitutes P (the "P could be a different subtype" limitation). The runtime behavior is sound: we provide every field P needs. | ||
| {...(props as P)} | ||
| setAssetStatus={setAssetStatus} | ||
| assetKey={assetKey} | ||
| ref={ref} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| WithAssetContextComponent.displayName = `withAssetContext(${ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. This is helpful! |
||
| WrappedComponent.displayName || WrappedComponent.name || "Component" | ||
| })`; | ||
|
|
||
| return WithAssetContextComponent; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
nit: If you omit this, can you use
screen.getByRole("button", {name: /label/});instead? That'd avoid using test ids, I think.