Created
May 28, 2020 07:56
-
-
Save SebinLee/ab7bd6d2e98cd27985e46f969a3cf12e to your computer and use it in GitHub Desktop.
React Testing Recipe 번역 - Mocking Modules
This file contains 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
import React from "react"; | |
import { render, unmountComponentAtNode } from "react-dom"; | |
import { act } from "react-dom/test-utils"; | |
import Contact from "./contact"; | |
import MockedMap from "./map"; | |
//의존성을 대체할 가짜 컴포넌트(Mock)을 작성합니다. | |
jest.mock("./map", () => { | |
return function DummyMap(props) { | |
return ( | |
<div data-testid="map"> | |
{props.center.lat}:{props.center.long} | |
</div> | |
); | |
}; | |
}); | |
let container = null; | |
// 가독성을 위해 beforeEach, afterEach는 생략합니다. | |
// 생략된 코드는 위의 Setup/Teardown 부분에 있는 예시코드와 동일합니다. | |
it("should render contact information", () => { | |
const center = { lat: 0, long: 0 }; | |
act(() => { | |
render( | |
<Contact | |
name="Joni Baez" | |
email="[email protected]" | |
site="http://test.com" | |
center={center} | |
/>, | |
container | |
); | |
}); | |
expect( | |
container.querySelector("[data-testid='email']").getAttribute("href") | |
).toEqual("mailto:[email protected]"); | |
expect( | |
container.querySelector('[data-testid="site"]').getAttribute("href") | |
).toEqual("http://test.com"); | |
expect(container.querySelector('[data-testid="map"]').textContent).toEqual( | |
"0:0" | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment