-
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
[Locked Labels] Implement adding/editing/deleting a standalone locked label #1539
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d9d130d
[Locked labels] View locked labels in an Interactive Graph
nishasy 8d80496
Import React, write test
nishasy 6ca6382
Replace m2b story with labels flag story
nishasy 2795b66
[Locked Labels] Implement adding/editing/deleting a standalone locked…
nishasy b33b216
Merge branch 'main' into view-locked-label
nishasy 2fbb312
streamline code
nishasy 506e7f8
Merge branch 'view-locked-label' into add-locked-label
nishasy 9fa4074
Merge branch 'main' into add-locked-label
nishasy 388a690
Destructure props more consistently. Streamline test.
nishasy 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,6 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
"@khanacademy/perseus-editor": minor | ||
--- | ||
|
||
[Locked Labels] Implement adding/editing/deleting a standalone locked label |
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,6 @@ | ||
--- | ||
"@khanacademy/perseus": minor | ||
"@khanacademy/perseus-editor": minor | ||
--- | ||
|
||
[Locked labels] View locked labels in an Interactive Graph |
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
48 changes: 48 additions & 0 deletions
48
packages/perseus-editor/src/components/__stories__/locked-label-settings.stories.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,48 @@ | ||
import * as React from "react"; | ||
|
||
import LockedLabelSettings from "../graph-locked-figures/locked-label-settings"; | ||
import {getDefaultFigureForType} from "../util"; | ||
|
||
import type {Meta, StoryObj} from "@storybook/react"; | ||
|
||
export default { | ||
title: "PerseusEditor/Components/Locked Label Settings", | ||
component: LockedLabelSettings, | ||
} as Meta<typeof LockedLabelSettings>; | ||
|
||
export const Default = (args): React.ReactElement => { | ||
return <LockedLabelSettings {...args} />; | ||
}; | ||
|
||
const defaultProps = { | ||
...getDefaultFigureForType("label"), | ||
onChangeProps: () => {}, | ||
onMove: () => {}, | ||
onRemove: () => {}, | ||
}; | ||
|
||
type StoryComponentType = StoryObj<typeof LockedLabelSettings>; | ||
|
||
// Set the default values in the control panel. | ||
Default.args = defaultProps; | ||
|
||
export const Expanded: StoryComponentType = { | ||
render: function Render() { | ||
const [props, setProps] = React.useState(defaultProps); | ||
|
||
const handlePropsUpdate = (newProps) => { | ||
setProps({ | ||
...props, | ||
...newProps, | ||
}); | ||
}; | ||
|
||
return ( | ||
<LockedLabelSettings | ||
{...props} | ||
expanded={true} | ||
onChangeProps={handlePropsUpdate} | ||
/> | ||
); | ||
}, | ||
}; |
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
219 changes: 219 additions & 0 deletions
219
packages/perseus-editor/src/components/__tests__/locked-label-settings.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,219 @@ | ||||
import {RenderStateRoot} from "@khanacademy/wonder-blocks-core"; | ||||
import {render, screen} from "@testing-library/react"; | ||||
import {userEvent as userEventLib} from "@testing-library/user-event"; | ||||
import * as React from "react"; | ||||
|
||||
import LockedLabelSettings from "../graph-locked-figures/locked-label-settings"; | ||||
import {getDefaultFigureForType} from "../util"; | ||||
|
||||
import type {Props} from "../graph-locked-figures/locked-label-settings"; | ||||
import type {UserEvent} from "@testing-library/user-event"; | ||||
|
||||
const defaultProps = { | ||||
...getDefaultFigureForType("label"), | ||||
onChangeProps: () => {}, | ||||
onMove: () => {}, | ||||
onRemove: () => {}, | ||||
} as Props; | ||||
|
||||
describe("Locked Label Settings", () => { | ||||
let userEvent: UserEvent; | ||||
const onChangeProps = jest.fn(); | ||||
|
||||
beforeEach(() => { | ||||
userEvent = userEventLib.setup({ | ||||
advanceTimers: jest.advanceTimersByTime, | ||||
}); | ||||
}); | ||||
|
||||
test("renders as expected with default values", () => { | ||||
// Arrange | ||||
|
||||
// Act | ||||
render(<LockedLabelSettings {...defaultProps} />, { | ||||
wrapper: RenderStateRoot, | ||||
}); | ||||
|
||||
// Assert | ||||
const titleText = screen.getByText("Label (0, 0)"); | ||||
expect(titleText).toBeInTheDocument(); | ||||
}); | ||||
|
||||
describe("Header interactions", () => { | ||||
test("should show the correct coords in the header", () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
coord={[1, 2]} | ||||
text="" | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
const titleText = screen.getByText("Label (1, 2)"); | ||||
|
||||
// Assert | ||||
expect(titleText).toBeInTheDocument(); | ||||
}); | ||||
|
||||
test("should show the text in the header", () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
color="blue" | ||||
text="something" | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
const text = screen.getByText("something"); | ||||
|
||||
// Assert | ||||
expect(text).toBeInTheDocument(); | ||||
}); | ||||
|
||||
test("calls 'onToggle' when header is clicked", async () => { | ||||
// Arrange | ||||
const onToggle = jest.fn(); | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
text="something" | ||||
onToggle={onToggle} | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
const header = screen.getByRole("button", { | ||||
name: "Label (0, 0) something", | ||||
}); | ||||
await userEvent.click(header); | ||||
|
||||
// Assert | ||||
expect(onToggle).toHaveBeenCalled(); | ||||
}); | ||||
}); | ||||
|
||||
describe("Settings interactions", () => { | ||||
test("calls 'onChangeProps' when coords are changed", async () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
onChangeProps={onChangeProps} | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
const xCoordInput = screen.getByRole("spinbutton", { | ||||
name: "x coord", | ||||
}); | ||||
const yCoordInput = screen.getByRole("spinbutton", { | ||||
name: "y coord", | ||||
}); | ||||
|
||||
await userEvent.clear(xCoordInput); | ||||
await userEvent.type(xCoordInput, "1"); | ||||
|
||||
await userEvent.clear(yCoordInput); | ||||
await userEvent.type(yCoordInput, "2"); | ||||
|
||||
// Assert | ||||
expect(onChangeProps).toHaveBeenCalledTimes(2); | ||||
// Calls are not being accumulated because they're mocked. | ||||
expect(onChangeProps).toHaveBeenNthCalledWith(1, {coord: [1, 0]}); | ||||
expect(onChangeProps).toHaveBeenNthCalledWith(2, {coord: [0, 2]}); | ||||
}); | ||||
|
||||
test("calls 'onChangeProps' when text is changed", async () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
onChangeProps={onChangeProps} | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
const textInput = screen.getByRole("textbox", { | ||||
name: "TeX", | ||||
}); | ||||
await userEvent.type(textInput, "x^2"); | ||||
|
||||
// Assert | ||||
// Assert | ||||
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.
Suggested change
|
||||
expect(onChangeProps).toHaveBeenCalledTimes(3); | ||||
// NOTE: Since the 'onChangeProps' function is being mocked, | ||||
// the equation doesn't get updated, | ||||
// and therefore the keystrokes don't accumulate. | ||||
// This is reflected in the calls to 'onChangeProps' being | ||||
// just 1 character at a time. | ||||
expect(onChangeProps).toHaveBeenNthCalledWith(1, {text: "x"}); | ||||
expect(onChangeProps).toHaveBeenNthCalledWith(2, {text: "^"}); | ||||
expect(onChangeProps).toHaveBeenNthCalledWith(3, {text: "2"}); | ||||
}); | ||||
|
||||
test("calls 'onChangeProps' when color is changed", async () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
onChangeProps={onChangeProps} | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
// Change the color | ||||
const colorSwitch = screen.getByLabelText("color"); | ||||
await userEvent.click(colorSwitch); | ||||
const colorOption = screen.getByText("green"); | ||||
await userEvent.click(colorOption); | ||||
|
||||
// Assert | ||||
expect(onChangeProps).toHaveBeenCalledWith({color: "green"}); | ||||
}); | ||||
|
||||
test("calls 'onChangeProps' when size is changed", async () => { | ||||
// Arrange | ||||
render( | ||||
<LockedLabelSettings | ||||
{...defaultProps} | ||||
onChangeProps={onChangeProps} | ||||
/>, | ||||
{ | ||||
wrapper: RenderStateRoot, | ||||
}, | ||||
); | ||||
|
||||
// Act | ||||
// Change the color | ||||
const sizeSelect = screen.getByLabelText("size"); | ||||
await userEvent.click(sizeSelect); | ||||
const sizeOption = screen.getByText("small"); | ||||
await userEvent.click(sizeOption); | ||||
|
||||
// Assert | ||||
expect(onChangeProps).toHaveBeenCalledWith({size: "small"}); | ||||
}); | ||||
}); | ||||
}); |
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
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.
You can make these assertions with a single
expect
call by doing: