Skip to content

Commit

Permalink
write some tests, you bum!
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance committed Jan 31, 2020
1 parent d1f94d5 commit ad4cd31
Show file tree
Hide file tree
Showing 17 changed files with 429 additions and 102 deletions.
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"scripts": {
"start": "start-storybook -p 9001 -c .storybook",
"test": "cross-env NODE_ENV=test CI=true tsdx test packages",
"test:root": "cross-env NODE_ENV=test CI=true tsdx test",
"test:watch": "cross-env NODE_ENV=test tsdx test packages",
"test:coverage": "cross-env NODE_ENV=test CI=true tsdx test packages --coverage",
"test:coverage": "yarn test --coverage",
"build:changed": "lerna run build --since origin/master",
"build": "lerna run build --stream",
"ver": "lerna version --no-push",
Expand Down Expand Up @@ -42,6 +43,7 @@
"@types/react-dom": "^16.9.5",
"@types/react-router-dom": "^5.1.3",
"@types/react-test-renderer": "^16.9.2",
"@types/sinon": "^7.5.1",
"@types/styled-components": "^4.4.2",
"@types/tabbable": "^3.1.0",
"@types/warning": "^3.0.0",
Expand Down Expand Up @@ -80,6 +82,7 @@
"react-router-dom": "^5.1.2",
"react-spring": "^8.0.27",
"react-test-renderer": "^16.12.0",
"sinon": "^8.1.1",
"styled-components": "^5.0.0",
"ts-loader": "^6.2.1",
"tsdx": "^0.12.3",
Expand Down Expand Up @@ -143,7 +146,8 @@
"onKeyUp"
]
}
]
],
"@typescript-eslint/no-unused-vars": 0
}
},
"eslintIgnore": [
Expand All @@ -157,11 +161,15 @@
"collectCoverageFrom": [
"packages/**/src/**/*.{js,ts,tsx}"
],
"moduleNameMapper": {
"\\$test(.*)$": "<rootDir>/test/$1"
},
"setupFilesAfterEnv": [
"<rootDir>/test/setupTests.ts"
]
},
"prettier": {
"singleQuote": false
"singleQuote": false,
"trailingComma": "es5"
}
}
4 changes: 2 additions & 2 deletions packages/alert-dialog/src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering should open the dialog 1`] = `
exports[`<AlertDialog /> should open the dialog 1`] = `
<DocumentFragment>
<div>
<button>
Expand All @@ -10,7 +10,7 @@ exports[`rendering should open the dialog 1`] = `
</DocumentFragment>
`;

exports[`rendering should open the dialog 2`] = `
exports[`<AlertDialog /> should open the dialog 2`] = `
<body>
<div
aria-hidden="true"
Expand Down
6 changes: 3 additions & 3 deletions packages/alert-dialog/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useRef, useState } from "react";
import { render, fireEvent } from "@testing-library/react";
import { render, fireEvent } from "$test/utils";
import {
AlertDialog,
AlertDialogLabel,
AlertDialogDescription
AlertDialogDescription,
} from "@reach/alert-dialog";

describe("rendering", () => {
describe("<AlertDialog />", () => {
it("should open the dialog", () => {
const { baseElement, asFragment, getByText } = render(<BasicAlertDialog />);
expect(asFragment()).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-id/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import { useId } from "./index";
import { render } from "@testing-library/react";
import { render } from "$test/utils";

describe("rendering", () => {
describe("useId()", () => {
it("should generate an incremented ID value", () => {
function Comp() {
const justNull = null;
Expand Down
4 changes: 2 additions & 2 deletions packages/combobox/src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering should match the snapshot 1`] = `
exports[`<Combobox /> should match the snapshot 1`] = `
<DocumentFragment>
<div>
<h2>
Expand All @@ -27,7 +27,7 @@ exports[`rendering should match the snapshot 1`] = `
</DocumentFragment>
`;

exports[`rendering should match the snapshot 2`] = `
exports[`<Combobox /> should match the snapshot 2`] = `
<DocumentFragment>
<div>
<h2>
Expand Down
8 changes: 4 additions & 4 deletions packages/combobox/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React, { useState, useMemo } from "react";
import { render } from "@testing-library/react";
import { render } from "$test/utils";
import userEvent from "@testing-library/user-event";
import {
Combobox,
ComboboxInput,
ComboboxList,
ComboboxOption,
ComboboxPopover
ComboboxPopover,
} from "@reach/combobox";
import matchSorter from "match-sorter";
import { useThrottle } from "../examples/use-throttle";
import cities from "../examples/cities";

describe("rendering", () => {
describe("<Combobox />", () => {
it("should match the snapshot", () => {
let { asFragment, getByTestId, getByRole } = render(<BasicCombobox />);
let input = getByTestId("input");
Expand Down Expand Up @@ -68,7 +68,7 @@ function useCityMatch(term: string) {
term.trim() === ""
? null
: matchSorter(cities, term, {
keys: [item => `${item.city}, ${item.state}`]
keys: [item => `${item.city}, ${item.state}`],
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[throttledTerm]
Expand Down
89 changes: 85 additions & 4 deletions packages/dialog/src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,88 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering does not render children when not open 1`] = `
<div
lang="gr"
/>
exports[`<Dialog /> closes the dialog 1`] = `
<body>
<div
aria-hidden="true"
>
<div>
<button>
Show Dialog
</button>
</div>
</div>
<reach-portal>
<div
data-focus-guard="true"
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;"
tabindex="0"
/>
<div
data-focus-guard="true"
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;"
tabindex="1"
/>
<div
data-focus-lock-disabled="false"
>
<div>
<div
data-reach-dialog-overlay=""
>
<div
aria-label="Announcement"
aria-modal="true"
data-reach-dialog-content=""
role="dialog"
tabindex="-1"
>
<div
data-testid="inner"
>
<button>
Close Dialog
</button>
<input
data-testid="text"
type="text"
/>
<button
data-testid="useless-button"
>
Ayyyyyy
</button>
</div>
</div>
</div>
</div>
</div>
<div
data-focus-guard="true"
style="width: 1px; height: 0px; padding: 0px; overflow: hidden; position: fixed; top: 1px; left: 1px;"
tabindex="0"
/>
</reach-portal>
</body>
`;

exports[`<Dialog /> closes the dialog 2`] = `
<body>
<div>
<div>
<button>
Show Dialog
</button>
</div>
</div>
</body>
`;

exports[`<Dialog /> does not render children when not open 1`] = `
<body>
<div>
<div
data-testid="root"
/>
</div>
</body>
`;
62 changes: 53 additions & 9 deletions packages/dialog/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
import React from "react";
import renderer from "react-test-renderer";
/* eslint-disable no-unused-vars */
import React, { useState } from "react";
import { useFakeTimers, SinonFakeTimers } from "sinon";
import { fireEvent, render } from "$test/utils";
import { Dialog } from "./index";

describe("rendering", () => {
function getOverlay(container: Element) {
return container.querySelector("[data-reach-dialog-overlay]");
}

describe("<Dialog />", () => {
let clock: SinonFakeTimers;
beforeEach(() => {
clock = useFakeTimers();
});
afterEach(() => {
clock.restore();
});

it("does not render children when not open", () => {
let wrapper = renderer.create(
<div lang="gr">
const { baseElement, queryByTestId } = render(
<div data-testid="root">
<Dialog isOpen={false} aria-label="cool dialog">
<div lang="en" />
<div data-testid="inner" />
</Dialog>
</div>
);
expect(wrapper.root.findAllByProps({ lang: "gr" })).toHaveLength(1);
expect(wrapper.root.findAllByProps({ lang: "en" })).toHaveLength(0);
expect(wrapper.toJSON()).toMatchSnapshot();
expect(queryByTestId("root")).toBeTruthy();
expect(queryByTestId("inner")).toBeNull();
expect(baseElement).toMatchSnapshot();
});

it("closes the dialog", () => {
const { baseElement, getByText, queryByTestId } = render(
<BasicOpenDialog />
);
expect(baseElement).toMatchSnapshot();
expect(queryByTestId("inner")).toBeTruthy();
fireEvent.click(getByText("Close Dialog"));
// Not sure why clicking the overlay doesn't work in test env, works IRL 🤷‍♂️
// fireEvent.click(getOverlay(baseElement)!)
clock.tick(10);
expect(baseElement).toMatchSnapshot();
expect(queryByTestId("inner")).toBeNull();
});
});

function BasicOpenDialog() {
const [showDialog, setShowDialog] = useState(true);
return (
<div>
<button onClick={() => setShowDialog(true)}>Show Dialog</button>
<Dialog aria-label="Announcement" isOpen={showDialog}>
<div data-testid="inner">
<button onClick={() => setShowDialog(false)}>Close Dialog</button>
<input data-testid="text" type="text" />
<button data-testid="useless-button">Ayyyyyy</button>
</div>
</Dialog>
</div>
);
}
2 changes: 1 addition & 1 deletion packages/slider/src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering should match the snapshot 1`] = `
exports[`<Slider /> should match the snapshot 1`] = `
<DocumentFragment>
<div
aria-disabled="false"
Expand Down
6 changes: 3 additions & 3 deletions packages/slider/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from "react";
// import renderer from "react-test-renderer";
// import { act } from "react-dom/test-utils";
import { render, fireEvent } from "@testing-library/react";
import { render, fireEvent } from "$test/utils";
import {
Slider,
SliderHandle,
SliderInput,
SliderMarker,
SliderTrack,
SliderTrackHighlight
SliderTrackHighlight,
// SLIDER_HANDLE_ALIGN_CENTER,
// SLIDER_HANDLE_ALIGN_CONTAIN,
// SLIDER_ORIENTATION_HORIZONTAL,
Expand All @@ -18,7 +18,7 @@ import {
const getCurrentValue = (el: HTMLElement) =>
Number(el.getAttribute("aria-valuenow"));

describe("rendering", () => {
describe("<Slider />", () => {
it("should match the snapshot", () => {
const { asFragment } = render(<Slider />);
expect(asFragment()).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rendering focuses the correct tab with keyboard navigation 1`] = `
exports[`<Tabs /> focuses the correct tab with keyboard navigation 1`] = `
<DocumentFragment>
<div>
<div
Expand Down Expand Up @@ -92,7 +92,7 @@ exports[`rendering focuses the correct tab with keyboard navigation 1`] = `
</DocumentFragment>
`;

exports[`rendering should match the snapshot 1`] = `
exports[`<Tabs /> should match the snapshot 1`] = `
<DocumentFragment>
<div>
<div
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { render, fireEvent } from "$test/utils";
import { Tabs, TabList, Tab, TabPanels, TabPanel } from "@reach/tabs";

describe("rendering", () => {
describe("<Tabs />", () => {
it("should match the snapshot", () => {
const { asFragment } = render(<BasicTabs />);
expect(asFragment()).toMatchSnapshot();
Expand Down
Loading

0 comments on commit ad4cd31

Please sign in to comment.