Skip to content

Instantly share code, notes, and snippets.

View tidusia's full-sized avatar

Thibaud Duthoit tidusia

View GitHub Profile
import React from "react";
import { render } from "@testing-library/react";
import { Props } from ".";
import { Default } from "./index.stories";
test("should render the title", () => {
const props = Default.args as Props;
const { getByText } = render(<Default {...props} />);
const actual = getByText(props.title);
expect(actual).toBeInTheDocument();
import React from "react";
import { render } from "@testing-library/react";
import { Props } from ".";
import { Default } from "./index.stories";
describe("COMPONENT_NAME", () => {
describe("Default", () => {
const props = Default.args as Props;
test("should match snapshot", () => {
import { axe } from "jest-axe";
test("should be accessible", async () => {
const { container } = render(<Component />);
expect(await axe(container)).toHaveNoViolations();
});
import React from "react";
import * as stories from "./index.stories";
import { axe } from "jest-axe";
import userEvent from "@testing-library/user-event";
import { composeStories } from "@storybook/testing-react";
import { render, screen } from "@testing-library/react";
const { Default } = composeStories(stories);
describe("MyComponent", () => {
test("should match snapshot", () => {
const { container } = render(<Default {...props} />);
expect(container.firstChild).toMatchSnapshot();
});
test("should be accessible and match snapshot", async () => {
const { container } = render(<Default {...props} />);
expect(await axe(container)).toHaveNoViolations();
expect(container.firstChild).toMatchSnapshot();
});
jest.mock("react-modal", () => ({ children }: any) => children);
const server = setupServer(
rest.get("/jobs", (req, res, ctx) => res(ctx.json(fixtures.jobs))),
);
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
import { ComponentMeta } from "@storybook/react";
import COMPONENT_NAME from ".";
import { userEvent, within } from "@storybook/testing-library";
export default {
component: COMPONENT_NAME,
decorators: [(story) => <div>{story()}</div>],
parameters: {
layout: "centered",
},
@tidusia
tidusia / context-based-compound-component.js
Created December 22, 2021 13:37
Context based Compound Component example
// Flexible Compound Components - epicreact.dev
import * as React from 'react'
import {Switch} from '../switch'
// 📜 https://reactjs.org/docs/context.html#reactcreatecontext
const ToggleContext = React.createContext()
ToggleContext.displayName = 'ToggleContext'
function Toggle({children}) {