Skip to content

Instantly share code, notes, and snippets.

@SebinLee
Created May 28, 2020 07:56
Show Gist options
  • Save SebinLee/ab7bd6d2e98cd27985e46f969a3cf12e to your computer and use it in GitHub Desktop.
Save SebinLee/ab7bd6d2e98cd27985e46f969a3cf12e to your computer and use it in GitHub Desktop.
React Testing Recipe 번역 - Mocking Modules
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