-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(text-editor): covert tests to use react-testing-library
- Loading branch information
1 parent
4974245
commit 8333ffd
Showing
13 changed files
with
2,112 additions
and
1,779 deletions.
There are no files selected for viewing
118 changes: 0 additions & 118 deletions
118
src/components/text-editor/__internal__/editor-link/editor-link.spec.tsx
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
src/components/text-editor/__internal__/editor-link/editor-link.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,111 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { ContentState } from "draft-js"; | ||
import EditorLink from "./editor-link.component"; | ||
import EditorContext from "../editor.context"; | ||
|
||
test("derives the url from the`contentState` and `entityKey`", () => { | ||
render( | ||
<EditorContext.Provider value={{ editMode: false }}> | ||
<EditorLink | ||
entityKey="baz" | ||
contentState={ | ||
{ | ||
getEntity: (entityKey) => ({ getData: () => entityKey }), | ||
} as ContentState | ||
} | ||
> | ||
<div>foo</div> | ||
<div>bar</div> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
const link = screen.getByRole("link"); | ||
expect(link).toHaveAttribute("href", "https://baz"); | ||
expect(link).toHaveTextContent("foobar"); | ||
}); | ||
|
||
test("derives the url from the first child element when no `contentState` is provided", () => { | ||
const ChildComponent = ({ text }: { text: string }) => <div>{text}</div>; | ||
render( | ||
<EditorContext.Provider value={{ editMode: false }}> | ||
<EditorLink entityKey="baz"> | ||
<ChildComponent text="foo" /> | ||
<ChildComponent text="bar" /> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
const link = screen.getByRole("link"); | ||
expect(link).toHaveAttribute("href", "https://foo"); | ||
expect(link).toHaveTextContent("foobar"); | ||
}); | ||
|
||
test("derives the url from the first child element when no `entityKey` is provided", () => { | ||
const ChildComponent = ({ text }: { text: string }) => <div>{text}</div>; | ||
render( | ||
<EditorContext.Provider value={{ editMode: false }}> | ||
<EditorLink | ||
contentState={ | ||
{ | ||
getEntity: (entityKey) => ({ getData: () => entityKey }), | ||
} as ContentState | ||
} | ||
> | ||
<ChildComponent text="foo" /> | ||
<ChildComponent text="bar" /> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
const link = screen.getByRole("link"); | ||
expect(link).toHaveAttribute("href", "https://foo"); | ||
expect(link).toHaveTextContent("foobar"); | ||
}); | ||
|
||
test("does not append `https://` to the url when it contains `http://`", () => { | ||
const ChildComponent = ({ text }: { text: string }) => <div>{text}</div>; | ||
render( | ||
<EditorContext.Provider value={{ editMode: false }}> | ||
<EditorLink> | ||
<ChildComponent text="http://foo" /> | ||
<ChildComponent text="bar" /> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
const link = screen.getByRole("link"); | ||
expect(link).toHaveAttribute("href", "http://foo"); | ||
expect(link).toHaveTextContent("http://foobar"); | ||
}); | ||
|
||
test("the `onLinkAdded` callback prop is called with the computed url when the component mounts", () => { | ||
const onLinkAdded = jest.fn(); | ||
const ChildComponent = ({ text }: { text: string }) => <div>{text}</div>; | ||
render( | ||
<EditorContext.Provider value={{ editMode: false, onLinkAdded }}> | ||
<EditorLink> | ||
<ChildComponent text="foo" /> | ||
<ChildComponent text="bar" /> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
expect(onLinkAdded).toHaveBeenCalledWith("https://foo"); | ||
expect(onLinkAdded).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("when the `editMode` prop is `true`, the anchor element should not have a `href` prop", () => { | ||
const ChildComponent = ({ text }: { text: string }) => <div>{text}</div>; | ||
render( | ||
<EditorContext.Provider value={{ editMode: true }}> | ||
<EditorLink> | ||
<ChildComponent text="foo" /> | ||
<ChildComponent text="bar" /> | ||
</EditorLink> | ||
</EditorContext.Provider> | ||
); | ||
|
||
expect(screen.queryByRole("link")).not.toBeInTheDocument(); | ||
}); |
42 changes: 0 additions & 42 deletions
42
...nts/text-editor/__internal__/editor-validation-wrapper/editor-validation-wrapper.spec.tsx
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/components/text-editor/__internal__/label-wrapper/label.spec.tsx
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/components/text-editor/__internal__/label-wrapper/label.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,29 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import Label from "../../../../__internal__/label"; | ||
import LabelWrapper from "./label-wrapper.component"; | ||
|
||
test("should render children", () => { | ||
render( | ||
<LabelWrapper onClick={() => {}}> | ||
<Label>Test Children</Label> | ||
</LabelWrapper> | ||
); | ||
|
||
expect(screen.getByText("Test Children")).toBeVisible(); | ||
}); | ||
|
||
test("should call the `onClick` function prop when clicked", async () => { | ||
const user = userEvent.setup(); | ||
const onClick = jest.fn(); | ||
render( | ||
<LabelWrapper onClick={onClick}> | ||
<Label>Test Children</Label> | ||
</LabelWrapper> | ||
); | ||
expect(onClick).not.toHaveBeenCalled(); | ||
|
||
await user.click(screen.getByText("Test Children")); | ||
expect(onClick).toHaveBeenCalledTimes(1); | ||
}); |
63 changes: 0 additions & 63 deletions
63
src/components/text-editor/__internal__/toolbar/toolbar-button/toolbar-button.spec.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.