Last active
June 30, 2021 12:17
-
-
Save eunsukimme/39d58a0374ca2b0e36a0422f3d74f92e to your computer and use it in GitHub Desktop.
refactor test code for Counter component with @testing-library/dom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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