-
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.
add tests for NumberLineEditor (#892)
* add tests for NumberLineEditor * changeset
- Loading branch information
Showing
4 changed files
with
183 additions
and
5 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-editor": patch | ||
--- | ||
|
||
add tests for NumberLineEditor |
18 changes: 18 additions & 0 deletions
18
packages/perseus-editor/src/widgets/__stories__/number-line-editor.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,18 @@ | ||
import {action} from "@storybook/addon-actions"; | ||
import * as React from "react"; | ||
|
||
import NumberLineEditor from "../number-line-editor"; | ||
|
||
type StoryArgs = Record<any, any>; | ||
|
||
type Story = { | ||
title: string; | ||
}; | ||
|
||
export default { | ||
title: "Perseus/Editor/Widgets/Number Line Editor", | ||
} as Story; | ||
|
||
export const Default = (args: StoryArgs): React.ReactElement => { | ||
return <NumberLineEditor onChange={action("onChange")} />; | ||
}; |
144 changes: 144 additions & 0 deletions
144
packages/perseus-editor/src/widgets/__tests__/number-line-editor.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,144 @@ | ||
import {Dependencies} from "@khanacademy/perseus"; | ||
import {render, screen} from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import * as React from "react"; | ||
|
||
import "@testing-library/jest-dom"; | ||
|
||
import {testDependencies} from "../../../../../testing/test-dependencies"; | ||
import NumberLineEditor from "../number-line-editor"; | ||
|
||
describe("number-line-editor", () => { | ||
beforeEach(() => { | ||
jest.spyOn(Dependencies, "getDependencies").mockReturnValue( | ||
testDependencies, | ||
); | ||
}); | ||
|
||
it("should render", async () => { | ||
render(<NumberLineEditor onChange={() => undefined} />); | ||
|
||
expect(await screen.findByText("Correct x")).toBeInTheDocument(); | ||
}); | ||
|
||
const relationships = ["lt", "gt", "le", "ge", "eq"]; | ||
relationships.forEach((rel) => { | ||
it(`should be possible to set relationship to: ${rel}`, async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
const select = screen.getByRole("combobox", { | ||
name: "Select relationship", | ||
}); | ||
userEvent.selectOptions(select, rel); | ||
|
||
expect(onChangeMock).toBeCalledWith( | ||
expect.objectContaining({correctRel: rel}), | ||
); | ||
}); | ||
}); | ||
|
||
it("should be possible to update the answer", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
const input = screen.getByPlaceholderText("answer"); | ||
userEvent.type(input, "1"); | ||
|
||
expect(onChangeMock).toBeCalledWith({correctX: 1}); | ||
}); | ||
|
||
it("should be possible to update position", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
const input = screen.getByRole("textbox", {name: "Position: ∈"}); | ||
userEvent.type(input, "1"); | ||
|
||
expect(onChangeMock).toBeCalledWith({initialX: 1}); | ||
}); | ||
|
||
const styles = { | ||
decimal: "Decimals", | ||
improper: "Improper fractions", | ||
mixed: "Mixed numbers", | ||
"non-reduced": "Non-reduced", | ||
}; | ||
Object.entries(styles).forEach(([key, title]) => { | ||
it(`should be possible to update style: ${title}`, async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
userEvent.click(screen.getByTitle(title)); | ||
|
||
expect(onChangeMock).toBeCalledWith({labelStyle: key}); | ||
}); | ||
}); | ||
|
||
it("should be possible to change show tick controller", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
userEvent.click( | ||
screen.getByRole("checkbox", {name: "Show tick controller"}), | ||
); | ||
|
||
expect(onChangeMock).toBeCalledWith({isTickCtrl: true}); | ||
}); | ||
|
||
it("should be possible to change show label tickets", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
userEvent.click( | ||
screen.getByRole("checkbox", {name: "Show label ticks"}), | ||
); | ||
|
||
expect(onChangeMock).toBeCalledWith({labelTicks: false}); | ||
}); | ||
|
||
it("should be possible to change show tooltips", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
userEvent.click(screen.getByRole("checkbox", {name: "Show tooltips"})); | ||
|
||
expect(onChangeMock).toBeCalledWith({showTooltips: true}); | ||
}); | ||
|
||
it("should be possible to update tick steps", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
const input = screen.getByRole("textbox", { | ||
name: "or tick step:", | ||
}); | ||
userEvent.type(input, "6"); | ||
|
||
expect(onChangeMock).toBeCalledWith({ | ||
numDivisions: null, | ||
tickStep: 6, | ||
}); | ||
}); | ||
|
||
it("should be possible to update snap divisions", async () => { | ||
const onChangeMock = jest.fn(); | ||
|
||
render(<NumberLineEditor onChange={onChangeMock} />); | ||
|
||
const input = screen.getByRole("textbox", { | ||
name: "Snap increments per tick:", | ||
}); | ||
userEvent.type(input, "6"); | ||
|
||
expect(onChangeMock).toBeCalledWith({snapDivisions: 26}); | ||
}); | ||
}); |
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