Skip to content

Instantly share code, notes, and snippets.

@eunsukimme
Last active June 30, 2021 12:17
Show Gist options
  • Save eunsukimme/39d58a0374ca2b0e36a0422f3d74f92e to your computer and use it in GitHub Desktop.
Save eunsukimme/39d58a0374ca2b0e36a0422f3d74f92e to your computer and use it in GitHub Desktop.
refactor test code for Counter component with @testing-library/dom
// Counter.test.js
import { getQueriesForElement } from "@testing-library/dom";
import ReactDOM, { unmountComponentAtNode } from "react-dom";
import { act } from "react-dom/test-utils";
import Counter from "./Counter";
let container = null;
export const render = (element) => {
container = document.createElement("div");
document.body.appendChild(container);
act(() => {
ReactDOM.render(element, container);
});
return getQueriesForElement(container);
};
export const cleanup = () => {
unmountComponentAtNode(container);
container.remove();
container = null;
};
afterEach(() => {
cleanup(container);
});
it("Counter 컴포넌트를 렌더링합니다", () => {
const { getByText } = render(<Counter />);
getByText("Counter: 0");
getByText("+");
getByText("-");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment