{"id":13581,"date":"2021-06-13T19:48:52","date_gmt":"2021-06-13T10:48:52","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13581"},"modified":"2023-09-26T23:00:59","modified_gmt":"2023-09-26T14:00:59","slug":"%e3%80%90react%e3%80%91%e3%80%8c%e3%83%9a%e3%83%bc%e3%82%b8%e9%81%b7%e7%a7%bb%e3%80%8d%e3%81%ae%e3%83%86%e3%82%b9%e3%83%88","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13581","title":{"rendered":"\u3010React\u3011\u300c\u30da\u30fc\u30b8\u9077\u79fb\u300d\u306e\u30c6\u30b9\u30c8"},"content":{"rendered":"\n<p>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u30da\u30fc\u30b8\u9077\u79fb\u306e\u30c6\u30b9\u30c8\u3092\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">react-router-dom\u3092\u30e2\u30c3\u30af\u5316\u3059\u308b\u65b9\u6cd5<\/h2>\n\n\n\n<p>react-router-dom\u81ea\u4f53\u3092jest\u3067\u30e2\u30c3\u30af\u5316\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u306e\u65b9\u6cd5\u3060\u3068withRouter\u3068\u304b\u304c\u30b3\u30fc\u30c9\u306b\u8a18\u8ff0\u3057\u3066\u3042\u308b\u30b3\u30fc\u30c9\u3060\u3068\u3046\u307e\u304f\u3044\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<p>\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306f<span style=\"color: #ff0000;\"><strong>\u5b9f\u969b\u306b\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u30da\u30fc\u30b8\u9077\u79fb\u3092\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u306f\u306a\u3044<\/strong><\/span>\u3068\u3044\u3046\u70b9\u3067\u3059\u3002react-router-dom\u306e\u30e2\u30c3\u30af\u3092\u7528\u610f\u3057\u3066\u305d\u308c\u304c\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308a\u6b63\u5e38\u306b\u66f8\u304d\u63db\u308f\u3063\u3066\u3044\u308b\u304b\u3092\u30c6\u30b9\u30c8\u3059\u308b\u30c6\u30b9\u30c8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">useHistory\u3092\u30e2\u30c3\u30af\u5316\u3059\u308b\u3002<\/h3>\n\n\n\n<p>react-router-dom\u306e\u4e2d\u306b\u3042\u308buseHistory\u306epush\u30e1\u30bd\u30c3\u30c9\u3092\u30c6\u30b9\u30c8\u7528\u306b\u30c0\u30df\u30fc\u306e\u95a2\u6570\u3067\u4e0a\u66f8\u304d\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u30c0\u30df\u30fc\u306e\u95a2\u6570\nconst mockHistoryPush = jest.fn();\n\njest.mock('react-router-dom', () =&gt; ({\n  useHistory: () =&gt; ({\n    push: mockHistoryPush, \/\/ push\u30e1\u30bd\u30c3\u30c9\u3092\u30c0\u30df\u30fc\u95a2\u6570\u3067\u4e0a\u66f8\u304d\u3059\u308b\u3002\n  }),\n}));<\/pre>\n\n\n\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u3057\u305f\u3044\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067React-router-dom\u306euseHistory\u3001push\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u30da\u30fc\u30b8\u9077\u79fb\u3057\u3066\u3044\u305f\u5834\u5408\u3082\u3001\u3053\u3061\u3089\u3067\u7528\u610f\u3057\u305f\u30c0\u30df\u30fc\u306epush\u30e1\u30bd\u30c3\u30c9\u3092\u898b\u306b\u3044\u304f\u3088\u3046\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u4f8b\uff08\u9077\u79fb\u524d\u306b\u975e\u540c\u671f\u51e6\u7406\u304c\u3042\u308b\u30d1\u30bf\u30fc\u30f3\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import { render, screen, cleanup } from '@testing-library\/react';\nimport userEvent from '@testing-library\/user-event';\n\n\u301c\u301c\u301c\u301c\u301c\u301c\u301c\u301c \u4e2d\u7565\u3000\u301c\u301c\u301c\u301c\u301c\u301c\u301c\u301c\n\n  it('\u30da\u30fc\u30b8\u9077\u79fb\u30c6\u30b9\u30c8', async () =&gt; {\n    render(\n        &lt;XXX\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8 \/&gt;\n    );\n    userEvent.click(screen.getByText('\u9077\u79fb\u30a2\u30af\u30b7\u30e7\u30f3\u30dc\u30bf\u30f3'));\n    expect(\n      await screen.findByText('\u30ed\u30b0\u30a4\u30f3\u6210\u529f\u6642\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u306a\u3069')\n    ).toBeInTheDocument();\n    expect(mockHistoryPush).toBeCalledWith('\/\u9077\u79fb\u5f8c\u306e\u30d1\u30b9');\n    expect(mockHistoryPush).toHaveBeenCalledTimes(1);\n  });<\/pre>\n\n\n\n<p>\u307e\u305a\u306f\u3001userEvent\u3092\u4f7f\u3063\u3066\u30da\u30fc\u30b8\u9077\u79fb\u306e\u30c8\u30ea\u30ac\u30fc\u3068\u306a\u308b\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u306b\u3001\u3082\u3057\u3001\u9077\u79fb\u524d\u306b\u975e\u540c\u671f\u51e6\u7406\u306a\u3069\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001await\u3067\u30da\u30fc\u30b8\u9077\u79fb\u304c\u6210\u529f\u3057\u305f\u30c8\u30ea\u30ac\u30fc\u3068\u306a\u308b\u3082\u306e\u3092\u8a18\u8ff0\u3057\u3066\u5b8c\u4e86\u3092\u5f85\u3061\u307e\u3059\u3002\uff08\u3082\u3057\u3001\u975e\u540c\u671f\u51e6\u7406\u304c\u306a\u3044\u5834\u5408\u306f\u3053\u306e\u8a18\u8ff0\u304c\u306a\u304f\u3066\u3082\u5927\u4e08\u592b\u3067\u3059\u3002\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">toBeCalledWith('\/\u9077\u79fb\u5f8c\u306e\u30d1\u30b9')<\/h4>\n\n\n\n<p>mockHistoryPush(History\u306e\u30e2\u30c3\u30af)\u304c\u300c\/\u30ed\u30b0\u30a4\u30f3\u5f8c\u306e\u30d1\u30b9(\u4f8b\u3048\u3070\u3001\/users)\u300d\u3068\u3044\u3046\u30d1\u30b9\u3067\u547c\u3073\u51fa\u3055\u308c\u305f\u304b\u3092\u5224\u5b9a\u3057\u3066\u304f\u308c\u308b\u30de\u30c3\u30c1\u30e3\u30fc\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">toHaveBeenCalledTimes(\u547c\u3073\u51fa\u3055\u308c\u305f\u56de\u6570);<\/h4>\n\n\n\n<p>mockHistoryPush(History\u306e\u30e2\u30c3\u30af)\u304c\u4f55\u56de\u547c\u3073\u51fa\u3055\u308c\u305f\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30de\u30c3\u30c1\u30e3\u30fc\u3067\u3059\u3002push\u3059\u308b\u306e\u304c1\u56de\u3060\u3051\u306a\u306e\u3067\u3042\u308c\u30701\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u7528\u9014<\/h5>\n\n\n\n<p>\u4e3b\u306b\u3001<span style=\"color: #ff0000;\"><strong>\u7279\u5b9a\u306e\u30b1\u30fc\u30b9\u3067\u306fpush\u304c\u3055\u308c\u306a\u3044\u306e\u3092\u30c6\u30b9\u30c8\u3057\u305f\u3044\u5834\u5408<\/strong><\/span>\u306b\u4f7f\u3044\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001\u8a8d\u8a3c\u306b\u5931\u6557\u3057\u305f\u5834\u5408\u306f\u30da\u30fc\u30b8\u9077\u79fb\u3057\u306a\u3044\u306e\u3067\u3001push\u3057\u306a\u3044\u306e\u30670\u3092\u6307\u5b9a\u3057\u3066\u547c\u3070\u308c\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u306a\u3069\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Router\u3092\u30e2\u30c3\u30af\u5316\u305b\u305a\u305d\u306e\u307e\u307e\u4f7f\u3046\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u3053\u306e\u65b9\u6cd5\u3067\u3042\u308c\u3070\u3001withRouter\u3067\u56f2\u3063\u3066\u3042\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8 \u304c\u3042\u3063\u305f\u3068\u3057\u3066\u3082\u6b63\u5e38\u306b\u52d5\u304b\u3059\u3053\u3068\u304c\u53ef\u80fd\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">describe(\"xxx\", () =&gt; {\n  it(\"yyy\", () =&gt; {\n    const history = createMemoryHistory();\n    history.push(\"\u9077\u79fb\u5143\u306e\u30d1\u30b9\");\n\n    render(\n      &lt;Router history={history}&gt;\n        &lt;\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8  \/&gt;\n      &lt;\/Router&gt;\n    );\n    userEvent.click(screen.getByText(\"\u30dc\u30bf\u30f3\"));\n    expect(history.location.pathname).toEqual(\"\u9077\u79fb\u5148\u30d1\u30b9\");\n  });\n});<\/pre>\n","protected":false},"excerpt":{"rendered":"\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u30da\u30fc\u30b8\u9077\u79fb\u306e\u30c6\u30b9\u30c8\u3092\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002 react-router-dom\u3092\u30e2\u30c3\u30af\u5316\u3059\u308b\u65b9\u6cd5 react-router-dom\u81ea\u4f53\u3092jest\u3067\u30e2\u30c3\u30af\u5316\u3057\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u306e\u65b9\u6cd5\u3060\u3068withRouter\u3068\u304b\u304c\u30b3\u30fc\u30c9 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[116],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13581"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13581"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13581\/revisions"}],"predecessor-version":[{"id":23212,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13581\/revisions\/23212"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13581"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}