Skip to content

Commit

Permalink
convert jest tests to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
alexasselin008 committed Aug 23, 2021
1 parent 2c7d2f1 commit ca9d28b
Show file tree
Hide file tree
Showing 106 changed files with 412 additions and 341 deletions.
19 changes: 13 additions & 6 deletions jest/utils/errorHandling.js → jest/utils/errorHandling.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const React = require("react");

import {ReactNode, Component} from "react"
// React logs errors to the console when an error is thrown, even when a boundary exists. Silence it temporarily.
// https://github.com/facebook/react/issues/15520
function muteConsoleErrors(patterns) {
function muteConsoleErrors(patterns: string[]) {
const nativeConsoleError = console.error.bind(console);

console.error = message => {
Expand All @@ -16,16 +16,21 @@ function muteConsoleErrors(patterns) {
};
}

class ErrorBoundary extends React.Component {
constructor() {
super();
interface ErrorBoundaryProps {
onError: (error: Error) => void;
children?: ReactNode;
}

class ErrorBoundary extends Component<ErrorBoundaryProps, { hasError: boolean}> {
constructor(props: ErrorBoundaryProps) {
super(props);

this.state = {
hasError: false
};
}

componentDidCatch(error) {
componentDidCatch(error: Error) {
const { onError } = this.props;

onError(error);
Expand All @@ -52,3 +57,5 @@ module.exports = {
muteConsoleErrors,
ErrorBoundary
};

export { muteConsoleErrors, ErrorBoundary }
4 changes: 3 additions & 1 deletion jest/utils/waitDelay.js → jest/utils/waitDelay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
async function waitDelay(delayMs) {
async function waitDelay(delayMs: number) {
await new Promise(resolve => setTimeout(resolve, delayMs));
}

module.exports = {
waitDelay
};

export {waitDelay}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@testing-library/react-hooks": "7.0.1",
"@testing-library/user-event": "13.2.0",
"@types/react": "17.0.14",
"@types/jest": "27.0.1",
"@types/react-dom": "17.0.9",
"@types/react-is": "17.0.2",
"@types/resize-observer-browser": "0.1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ test("when multiple, call onExpansionChange when the expanded tabs change", asyn
// ***** Refs *****

test("accordion ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Accordion ref={ref}>
Expand All @@ -228,7 +228,7 @@ test("accordion ref is a DOM element", async () => {
});

test("header ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Accordion>
Expand All @@ -246,7 +246,7 @@ test("header ref is a DOM element", async () => {
});

test("content ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Accordion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Alert } from "@react-components/alert";
import { Content } from "@react-components/placeholders";
import { Heading } from "@react-components/typography";
import { createRef } from "react";
import { Ref, createRef } from "react";
import { render, waitFor } from "@testing-library/react";

// ***** Behaviors *****
Expand Down Expand Up @@ -53,10 +53,10 @@ test("when autoFocusButton value is not defined, autofocus the primary button",
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Alert ref={ref}>
<Alert ref={ref} primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand All @@ -69,10 +69,11 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Alert
primaryButtonLabel="Primary"
ref={node => {
refNode = node;
}}
Expand All @@ -92,7 +93,7 @@ test("set ref once", async () => {
const handler = jest.fn();

render(
<Alert ref={handler}>
<Alert ref={handler} primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Alert, AlertTrigger } from "@react-components/alert";
import { Button } from "@react-components/button";
import { Content } from "@react-components/placeholders";
import { Heading } from "@react-components/typography";
import { Ref, createRef } from "react";
import { act } from "@testing-library/react-hooks";
import { createRef } from "react";
import { render, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

Expand All @@ -13,7 +13,7 @@ test("do not dismiss on outside click", async () => {
const { getByTestId } = render(
<AlertTrigger>
<Button data-testid="trigger">Trigger</Button>
<Alert data-testid="alert">
<Alert data-testid="alert" primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand All @@ -36,12 +36,12 @@ test("do not dismiss on outside click", async () => {
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<AlertTrigger defaultOpen ref={ref}>
<Button>Trigger</Button>
<Alert>
<Alert primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand All @@ -55,7 +55,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<AlertTrigger
Expand All @@ -65,7 +65,7 @@ test("when using a callback ref, ref is a DOM element", async () => {
}}
>
<Button>Trigger</Button>
<Alert>
<Alert primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand All @@ -84,7 +84,7 @@ test("set ref once", async () => {
render(
<AlertTrigger defaultOpen ref={handler}>
<Button>Trigger</Button>
<Alert>
<Alert primaryButtonLabel="Primary">
<Heading>Autopilot</Heading>
<Content>Are you use sure you want to engage autopilot?</Content>
</Alert>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { waitDelay } from "@utils/waitDelay";
import userEvent from "@testing-library/user-event";

beforeAll(() => {
// @ts-ignore
Transition.disableAnimation = true;
});

Expand Down Expand Up @@ -804,7 +805,7 @@ test("call onSelectionChange when a value is selected", async () => {
});

test("calling the focus function on the autocomplete ref will focus the autocomplete", async () => {
const ref = createRef();
const ref = createRef<HTMLInputElement>();

const { getByTestId } = render(
<Autocomplete
Expand All @@ -830,7 +831,7 @@ test("calling the focus function on the autocomplete ref will focus the autocomp
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLInputElement>();

render(
<Autocomplete defaultOpen ref={ref} aria-label="Planet">
Expand All @@ -848,7 +849,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Autocomplete
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Avatar } from "@react-components/avatar";
import { createRef } from "react";
import { Ref, createRef } from "react";
import { render, waitFor } from "@testing-library/react";

// ***** Aria *****
Expand All @@ -23,7 +23,7 @@ test("when an image src is provided and a custom aria-label is provided, the ari
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Avatar name="Elon Musk" ref={ref} />
Expand All @@ -36,7 +36,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Avatar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Avatar, AvatarGroup } from "@react-components/avatar";
import { createRef } from "react";
import { Ref, createRef } from "react";
import { render, waitFor } from "@testing-library/react";

// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<AvatarGroup ref={ref}>
Expand All @@ -21,7 +21,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<AvatarGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Badge } from "@react-components/badge";
import { Badge, BadgeProps } from "@react-components/badge";
import { Text } from "@react-components/typography";
import { createRef } from "react";
import { forwardRef } from "react";
import { render, waitFor } from "@testing-library/react";

const SquareBadge = forwardRef(({ children, ...rest }, ref) => {
const SquareBadge = forwardRef<HTMLElement, BadgeProps>(({ children, ...rest }, ref) => {
return (
<Badge
{...rest}
Expand All @@ -19,7 +19,7 @@ const SquareBadge = forwardRef(({ children, ...rest }, ref) => {
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<SquareBadge ref={ref}>
Expand All @@ -34,7 +34,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<SquareBadge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Box } from "@react-components/box";
import { createRef } from "react";
import { Ref, createRef } from "react";
import { render, waitFor } from "@testing-library/react";

// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Box ref={ref}>Box</Box>
Expand All @@ -18,7 +18,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Box
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from "@react-components/button";
import { Ref, createRef } from "react";
import { act, render, waitFor } from "@testing-library/react";
import { createRef } from "react";
import { waitDelay } from "@utils/waitDelay";

// ***** Behaviors *****
Expand Down Expand Up @@ -66,7 +66,7 @@ test("when type is specified, the type is forwarded", async () => {
// ***** Api *****

test("can focus the button with the focus api", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Button
Expand All @@ -86,7 +86,7 @@ test("can focus the button with the focus api", async () => {
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLButtonElement>();

render(
<Button ref={ref}>Cutoff</Button>
Expand All @@ -99,7 +99,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Button, ButtonGroup } from "@react-components/button";
import { Button, ButtonGroup, ButtonGroupProps } from "@react-components/button";
import { createRef, forwardRef } from "react";
import { render, waitFor } from "@testing-library/react";

const Group = forwardRef((props, ref) => {
const Group = forwardRef<HTMLElement, Omit<ButtonGroupProps, "children">>((props, ref) => {
return (
<ButtonGroup
{...props}
Expand All @@ -17,7 +17,7 @@ const Group = forwardRef((props, ref) => {
// ***** Refs *****

test("ref is a DOM element", async () => {
const ref = createRef();
const ref = createRef<HTMLElement>();

render(
<Group ref={ref} />
Expand All @@ -30,7 +30,7 @@ test("ref is a DOM element", async () => {
});

test("when using a callback ref, ref is a DOM element", async () => {
let refNode = null;
let refNode: HTMLElement = null;

render(
<Group
Expand Down
Loading

0 comments on commit ca9d28b

Please sign in to comment.