Skip to content

Instantly share code, notes, and snippets.

@MrLeebo
Last active January 17, 2020 22:20
Show Gist options
  • Save MrLeebo/166da7c465ea286d8ba316676bd0c1f2 to your computer and use it in GitHub Desktop.
Save MrLeebo/166da7c465ea286d8ba316676bd0c1f2 to your computer and use it in GitHub Desktop.
import { prefetchQuery } from "react-query";
describe('MyComponent', () => {
beforeEach(() => {
// using react-query for data fetching makes it easy to mock out your API calls
return prefetchQuery(["myQuery"], () => [{ name: 'Steve', role: 'Developer' }])
})
it('should open a dialog', async () => {
// visit the page
const page = visit('/mycomponent/steve')
// interact with a button
const button = await page.findByText("Open Dialog")
fireEvent.click(button)
// test your expectations
const dialog = await page.findByRole("dialog")
expect(dialog).toHaveTextContent("Hello, Steve the Developer!")
})
})
import React from "react";
import { render } from "@testing-library/react";
import { clearQueryCache } from "react-query";
import "@testing-library/jest-dom/extend-expect";
import { AppContext } from "../../src/contexts";
const defaultOptions = {}
global.visit = (path, options = defaultOptions) => {
return render(
<AppContext.Provider value={options}>
<React.Suspense fallback={"Loading..."}>
<ErrorBoundary>
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>
</ErrorBoundary>
</React.Suspense>
</AppContext.Provider>
);
};
afterEach(clearQueryCache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment