{"id":465844,"date":"2025-07-03T09:31:36","date_gmt":"2025-07-03T09:31:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=465844"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=465844","title":{"rendered":"<span>\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438 \u0432 React \u0441 @testing-library\/react-hooks<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c React-\u0445\u0443\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>@testing-library\/react-hooks<\/code>.<\/p>\n<h3>\u041f\u043e\u0434\u0445\u043e\u0434 \u043a \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0445\u0443\u043a\u0430\u043c<\/h3>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0442\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0441\u0447\u0451\u0442\u0447\u0438\u043a\u0430. \u0412\u043e\u0442 \u0443 \u043d\u0430\u0441 \u0445\u0443\u043a:<\/p>\n<pre><code class=\"python\">import { useState, useCallback } from 'react'  export default function useCounter() {   const [count, setCount] = useState(0)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])   return { count, increment } }<\/code><\/pre>\n<p>\u0425\u0443\u043a\u0438 \u043d\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u044f\u0442\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>renderHook<\/code>. \u0412\u0441\u0451 \u043a\u0430\u043a \u0441 \u0442\u0435\u0441\u0442\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 \u0442\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0430\u043b\u0445\u0438\u043c\u0438\u0438:<\/p>\n<pre><code class=\"python\">import { renderHook } from '@testing-library\/react-hooks' import useCounter from '.\/useCounter'  test('should initialize counter', () =&gt; {   const { result } = renderHook(() =&gt; useCounter())    expect(result.current.count).toBe(0)   expect(typeof result.current.increment).toBe('function') })<\/code><\/pre>\n<p><code>result.current<\/code> \u2014 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041d\u0435\u043b\u044c\u0437\u044f \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>const { count } = result.current<\/code> \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438 \u043f\u043e\u0442\u043e\u043c \u043e\u0436\u0438\u0434\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0441\u043d\u0438\u043c\u043e\u043a, \u0430 \u043d\u0435 \u0441\u0441\u044b\u043b\u043a\u0430. \u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043b\u043e\u0432\u044f\u0442 \u0431\u0430\u0433\u0438 \u0442\u0435, \u043a\u0442\u043e \u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u0430\u043a <code>ref<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0422\u0443\u0442 \u0443\u0436\u0435 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0437\u0432\u0430\u0442\u044c <code>act()<\/code> \u2014 \u043e\u043d \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b React \u043d\u0435 \u0440\u0443\u0433\u0430\u043b\u0441\u044f, \u0447\u0442\u043e \u0441\u0442\u0435\u0439\u0442 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0435 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430.<\/p>\n<pre><code class=\"python\">import { renderHook, act } from '@testing-library\/react-hooks'  test('should increment counter', () =&gt; {   const { result } = renderHook(() =&gt; useCounter())    act(() =&gt; {     result.current.increment()   })    expect(result.current.count).toBe(1) })<\/code><\/pre>\n<p>\u0411\u0435\u0437 <code>act()<\/code> React \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043a\u0438\u043d\u0443\u0442\u044c \u0432\u043e\u0440\u043d\u0438\u043d\u0433. \u0418\u043d\u043e\u0433\u0434\u0430 \u0434\u0430\u0436\u0435 \u043d\u0435 \u0432 \u044d\u0442\u043e\u043c \u0442\u0435\u0441\u0442\u0435, \u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c. <\/p>\n<h3>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438 \u0441\u0431\u0440\u043e\u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0421\u043a\u0430\u0436\u0435\u043c, \u043d\u0430\u0448 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c <code>initialValue<\/code> \u0438 \u0434\u0435\u043b\u0430\u0442\u044c <code>reset()<\/code>:<\/p>\n<pre><code class=\"python\">export default function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])   const reset = useCallback(() =&gt; setCount(initialValue), [initialValue])   return { count, increment, reset } }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>initialValue<\/code> \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0440\u0435\u043d\u0434\u0435\u0440\u0435. \u0412\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"python\">test('should reset to new initial value after rerender', () =&gt; {   let initial = 5   const { result, rerender } = renderHook(() =&gt; useCounter(initial))    initial = 10   rerender()    act(() =&gt; result.current.reset())    expect(result.current.count).toBe(10) })<\/code><\/pre>\n<p>\u041d\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0443 \u0432\u0430\u0441, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0431\u0443\u0434\u0435\u0442 \u043a\u0443\u0447\u0430 \u043f\u0440\u043e\u043f\u0441\u043e\u0432. \u0422\u0430\u043c <code>let<\/code> \u043d\u0435 \u0441\u043f\u0430\u0441\u0430\u0435\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>initialProps<\/code>:<\/p>\n<pre><code class=\"python\">test('should reset to updated initial value with initialProps', () =&gt; {   const { result, rerender } = renderHook(({ init }) =&gt; useCounter(init), {     initialProps: { init: 3 }   })    rerender({ init: 7 })    act(() =&gt; result.current.reset())    expect(result.current.count).toBe(7) })<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0445\u0443\u043a\u0430 \u043f\u0440\u0438 \u0441\u043c\u0435\u043d\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u2014 \u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u043e\u0432\u0443\u0448\u043a\u0443 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.<\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0430\u043c\u0438<\/h3>\n<p>\u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0445\u0443\u043a \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 <code>useContext<\/code> \u2014 \u0434\u0435\u043b\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f. \u041d\u043e \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e. \u0412\u0441\u0451 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f <code>wrapper<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c <code>CounterContext<\/code>:<\/p>\n<pre><code class=\"python\">const CounterStepContext = React.createContext(1)  export const CounterStepProvider = ({ step, children }) =&gt; (   &lt;CounterStepContext.Provider value={step}&gt;{children}&lt;\/CounterStepContext.Provider&gt; )  export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const step = useContext(CounterStepContext)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + step), [step])   return { count, increment } }<\/code><\/pre>\n<p>\u0410 \u0432\u043e\u0442 \u0442\u0435\u0441\u0442 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c:<\/p>\n<pre><code class=\"python\">test('should increment with custom context step', () =&gt; {   const wrapper = ({ children }) =&gt; &lt;CounterStepProvider step={2}&gt;{children}&lt;\/CounterStepProvider&gt;    const { result } = renderHook(() =&gt; useCounter(), { wrapper })    act(() =&gt; result.current.increment())    expect(result.current.count).toBe(2) })<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u043d\u044f\u0442\u044c <code>step<\/code>:<\/p>\n<pre><code class=\"python\">test('should change step on rerender', () =&gt; {   const wrapper = ({ children, step }) =&gt; (     &lt;CounterStepProvider step={step}&gt;{children}&lt;\/CounterStepProvider&gt;   )    const { result, rerender } = renderHook(() =&gt; useCounter(), {     wrapper,     initialProps: { step: 2 }   })    act(() =&gt; result.current.increment())   expect(result.current.count).toBe(2)    rerender({ step: 5 })   act(() =&gt; result.current.increment())    expect(result.current.count).toBe(7) })<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c ESLint \u043d\u0430\u0447\u043d\u0451\u0442 \u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <code>displayName<\/code>, \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u0435 \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0432 \u0442\u0435\u0441\u0442\u0435:<\/p>\n<pre><code class=\"python\">\/* eslint-disable react\/display-name *\/<\/code><\/pre>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442-\u043e\u0431\u0451\u0440\u0442\u043a\u0443.<\/p>\n<h3>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0445\u0443\u043a\u0438 \u0438 waitForNextUpdate<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432 \u0445\u0443\u043a \u0437\u0430\u0432\u043e\u0437\u044f\u0442 <code>setTimeout<\/code>, <code>fetch<\/code>, <code>debounce<\/code>, \u2014 \u0432\u0441\u0451, \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>expect<\/code> \u0443\u0436\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0422\u0443\u0442 \u043d\u0443\u0436\u0435\u043d <code>waitForNextUpdate()<\/code>:<\/p>\n<pre><code class=\"python\">export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount(x =&gt; x + 1), [])   const incrementAsync = useCallback(() =&gt; setTimeout(increment, 100), [increment])   return { count, increment, incrementAsync } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442:<\/p>\n<pre><code class=\"python\">test('should async increment after delay', async () =&gt; {   const { result, waitForNextUpdate } = renderHook(() =&gt; useCounter())    result.current.incrementAsync()    await waitForNextUpdate()    expect(result.current.count).toBe(1) })<\/code><\/pre>\n<p><code>act()<\/code> \u0442\u0443\u0442 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u2014 <code>waitForNextUpdate()<\/code> \u0443\u0436\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0451 \u0432 <code>act<\/code> \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 async-\u043f\u043e\u0442\u043e\u043a, \u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f <code>waitFor()<\/code>.<\/p>\n<p>\u0418 \u0435\u0449\u0451: \u0435\u0441\u043b\u0438 \u0432\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0435 <code>debounce<\/code>, <code>throttle<\/code> \u0438\u043b\u0438 <code>requestAnimationFrame<\/code> \u2014 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0430\u0439\u043c\u0435\u0440\u044b \u0447\u0435\u0440\u0435\u0437 <code>jest.useFakeTimers()<\/code>.<\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438<\/h3>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u0414\u0430, \u043c\u043e\u0436\u043d\u043e \u0438 \u044d\u0442\u043e. \u0412\u043e\u0442 \u0445\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432\u044b\u0448\u0435 9000:<\/p>\n<pre><code class=\"python\">export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])      if (count &gt; 9000) {     throw new Error(\"It's over 9000!\")   }    return { count, increment } }<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f:<\/p>\n<pre><code class=\"python\">test('should throw when over 9000', () =&gt; {   const { result } = renderHook(() =&gt; useCounter(9000))    act(() =&gt; result.current.increment())    expect(result.error).toEqual(Error(\"It's over 9000!\")) })<\/code><\/pre>\n<p><code>result.error<\/code> \u2014 \u0440\u0435\u0434\u043a\u0430\u044f, \u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u041e\u043d\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u0430. \u0422\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f <code>useMemo<\/code>, <code>useEffect<\/code>, <code>useReducer<\/code> \u2014 \u0435\u0441\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0435.<\/p>\n<p><em>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0441 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e <\/em><a href=\"https:\/\/react-hooks-testing-library.com\/\" rel=\"noopener noreferrer nofollow\"><em>\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c<\/em><\/a><em>.<\/em><\/p>\n<hr\/>\n<p>\u0413\u043e\u0442\u043e\u0432\u044b \u0443\u0433\u043b\u0443\u0431\u0438\u0442\u044c \u0437\u043d\u0430\u043d\u0438\u044f \u0432\u00a0React \u0438 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0443\u043a\u043e\u0432? \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430 \u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430, \u0433\u0434\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443\u0442, \u043a\u0430\u043a\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c @testing\u2011library\/react\u2011hooks \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u00a0\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/otus.pw\/mxyh\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a\u00a0\u0441\u0442\u0430\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c JavaScript\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c: \u043f\u043b\u0430\u043d \u043e\u0442\u00a0\u0434\u0436\u0443\u043d\u0430 \u0434\u043e\u00a0\u043c\u0438\u0434\u043b\u0430<\/a>\u00a0\u2014 10\u00a0\u0438\u044e\u043b\u044f \u0432 20:00 <\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/P91u\/\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u0447\u0435\u043c JavaScript\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0431\u044d\u043a\u0435\u043d\u0434? \u041e\u0442\u00a0fetch \u0434\u043e\u00a0Node.js<\/a>\u00a0\u2014 23\u00a0\u0438\u044e\u043b\u044f \u0432 20:00 <\/p>\n<\/li>\n<\/ol>\n<p><em>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 <\/em><a href=\"https:\/\/otus.pw\/e4xd\/\" rel=\"noopener noreferrer nofollow\"><em>\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439<\/em><\/a><em> <\/em><a href=\"https:\/\/otus.pw\/e4xd\/\" rel=\"noopener noreferrer nofollow\"><em>\u0442\u0435\u0441\u0442<\/em><\/a><em> \u0438 \u0443\u0437\u043d\u0430\u0439\u0442\u0435, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u044b \u0443\u0436\u0435 \u0432\u043b\u0430\u0434\u0435\u0435\u0442\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c React\u2011\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u0445\u0443\u043a\u043e\u0432. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u044b\u044f\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u043b\u044f\u00a0\u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f.<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/923214\/\"> https:\/\/habr.com\/ru\/articles\/923214\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c React-\u0445\u0443\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>@testing-library\/react-hooks<\/code>.<\/p>\n<h3>\u041f\u043e\u0434\u0445\u043e\u0434 \u043a \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0445\u0443\u043a\u0430\u043c<\/h3>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0442\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0441\u0447\u0451\u0442\u0447\u0438\u043a\u0430. \u0412\u043e\u0442 \u0443 \u043d\u0430\u0441 \u0445\u0443\u043a:<\/p>\n<pre><code class=\"python\">import { useState, useCallback } from 'react'  export default function useCounter() {   const [count, setCount] = useState(0)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])   return { count, increment } }<\/code><\/pre>\n<p>\u0425\u0443\u043a\u0438 \u043d\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u044f\u0442\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>renderHook<\/code>. \u0412\u0441\u0451 \u043a\u0430\u043a \u0441 \u0442\u0435\u0441\u0442\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 \u0442\u0443\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0430\u043b\u0445\u0438\u043c\u0438\u0438:<\/p>\n<pre><code class=\"python\">import { renderHook } from '@testing-library\/react-hooks' import useCounter from '.\/useCounter'  test('should initialize counter', () =&gt; {   const { result } = renderHook(() =&gt; useCounter())    expect(result.current.count).toBe(0)   expect(typeof result.current.increment).toBe('function') })<\/code><\/pre>\n<p><code>result.current<\/code> \u2014 \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041d\u0435\u043b\u044c\u0437\u044f \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>const { count } = result.current<\/code> \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438 \u043f\u043e\u0442\u043e\u043c \u043e\u0436\u0438\u0434\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0441\u043d\u0438\u043c\u043e\u043a, \u0430 \u043d\u0435 \u0441\u0441\u044b\u043b\u043a\u0430. \u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043b\u043e\u0432\u044f\u0442 \u0431\u0430\u0433\u0438 \u0442\u0435, \u043a\u0442\u043e \u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u0430\u043a <code>ref<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0422\u0443\u0442 \u0443\u0436\u0435 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0437\u0432\u0430\u0442\u044c <code>act()<\/code> \u2014 \u043e\u043d \u043d\u0443\u0436\u0435\u043d, \u0447\u0442\u043e\u0431\u044b React \u043d\u0435 \u0440\u0443\u0433\u0430\u043b\u0441\u044f, \u0447\u0442\u043e \u0441\u0442\u0435\u0439\u0442 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0435 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430.<\/p>\n<pre><code class=\"python\">import { renderHook, act } from '@testing-library\/react-hooks'  test('should increment counter', () =&gt; {   const { result } = renderHook(() =&gt; useCounter())    act(() =&gt; {     result.current.increment()   })    expect(result.current.count).toBe(1) })<\/code><\/pre>\n<p>\u0411\u0435\u0437 <code>act()<\/code> React \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043a\u0438\u043d\u0443\u0442\u044c \u0432\u043e\u0440\u043d\u0438\u043d\u0433. \u0418\u043d\u043e\u0433\u0434\u0430 \u0434\u0430\u0436\u0435 \u043d\u0435 \u0432 \u044d\u0442\u043e\u043c \u0442\u0435\u0441\u0442\u0435, \u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c. <\/p>\n<h3>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438 \u0441\u0431\u0440\u043e\u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0421\u043a\u0430\u0436\u0435\u043c, \u043d\u0430\u0448 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c <code>initialValue<\/code> \u0438 \u0434\u0435\u043b\u0430\u0442\u044c <code>reset()<\/code>:<\/p>\n<pre><code class=\"python\">export default function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])   const reset = useCallback(() =&gt; setCount(initialValue), [initialValue])   return { count, increment, reset } }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>initialValue<\/code> \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0440\u0435\u043d\u0434\u0435\u0440\u0435. \u0412\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<pre><code class=\"python\">test('should reset to new initial value after rerender', () =&gt; {   let initial = 5   const { result, rerender } = renderHook(() =&gt; useCounter(initial))    initial = 10   rerender()    act(() =&gt; result.current.reset())    expect(result.current.count).toBe(10) })<\/code><\/pre>\n<p>\u041d\u043e \u043d\u0430 \u043f\u0440\u043e\u0434\u0435 \u0443 \u0432\u0430\u0441, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0431\u0443\u0434\u0435\u0442 \u043a\u0443\u0447\u0430 \u043f\u0440\u043e\u043f\u0441\u043e\u0432. \u0422\u0430\u043c <code>let<\/code> \u043d\u0435 \u0441\u043f\u0430\u0441\u0430\u0435\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>initialProps<\/code>:<\/p>\n<pre><code class=\"python\">test('should reset to updated initial value with initialProps', () =&gt; {   const { result, rerender } = renderHook(({ init }) =&gt; useCounter(init), {     initialProps: { init: 3 }   })    rerender({ init: 7 })    act(() =&gt; result.current.reset())    expect(result.current.count).toBe(7) })<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0445\u0443\u043a\u0430 \u043f\u0440\u0438 \u0441\u043c\u0435\u043d\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u2014 \u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u043e\u0432\u0443\u0448\u043a\u0443 \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.<\/p>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0430\u043c\u0438<\/h3>\n<p>\u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0445\u0443\u043a \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 <code>useContext<\/code> \u2014 \u0434\u0435\u043b\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f. \u041d\u043e \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e. \u0412\u0441\u0451 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f <code>wrapper<\/code>-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>\u0412\u043e\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c <code>CounterContext<\/code>:<\/p>\n<pre><code class=\"python\">const CounterStepContext = React.createContext(1)  export const CounterStepProvider = ({ step, children }) =&gt; (   &lt;CounterStepContext.Provider value={step}&gt;{children}&lt;\/CounterStepContext.Provider&gt; )  export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const step = useContext(CounterStepContext)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + step), [step])   return { count, increment } }<\/code><\/pre>\n<p>\u0410 \u0432\u043e\u0442 \u0442\u0435\u0441\u0442 \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c:<\/p>\n<pre><code class=\"python\">test('should increment with custom context step', () =&gt; {   const wrapper = ({ children }) =&gt; &lt;CounterStepProvider step={2}&gt;{children}&lt;\/CounterStepProvider&gt;    const { result } = renderHook(() =&gt; useCounter(), { wrapper })    act(() =&gt; result.current.increment())    expect(result.current.count).toBe(2) })<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u043d\u044f\u0442\u044c <code>step<\/code>:<\/p>\n<pre><code class=\"python\">test('should change step on rerender', () =&gt; {   const wrapper = ({ children, step }) =&gt; (     &lt;CounterStepProvider step={step}&gt;{children}&lt;\/CounterStepProvider&gt;   )    const { result, rerender } = renderHook(() =&gt; useCounter(), {     wrapper,     initialProps: { step: 2 }   })    act(() =&gt; result.current.increment())   expect(result.current.count).toBe(2)    rerender({ step: 5 })   act(() =&gt; result.current.increment())    expect(result.current.count).toBe(7) })<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c ESLint \u043d\u0430\u0447\u043d\u0451\u0442 \u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <code>displayName<\/code>, \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u0435 \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0432 \u0442\u0435\u0441\u0442\u0435:<\/p>\n<pre><code class=\"python\">\/* eslint-disable react\/display-name *\/<\/code><\/pre>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442-\u043e\u0431\u0451\u0440\u0442\u043a\u0443.<\/p>\n<h3>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0445\u0443\u043a\u0438 \u0438 waitForNextUpdate<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432 \u0445\u0443\u043a \u0437\u0430\u0432\u043e\u0437\u044f\u0442 <code>setTimeout<\/code>, <code>fetch<\/code>, <code>debounce<\/code>, \u2014 \u0432\u0441\u0451, \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>expect<\/code> \u0443\u0436\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0422\u0443\u0442 \u043d\u0443\u0436\u0435\u043d <code>waitForNextUpdate()<\/code>:<\/p>\n<pre><code class=\"python\">export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount(x =&gt; x + 1), [])   const incrementAsync = useCallback(() =&gt; setTimeout(increment, 100), [increment])   return { count, increment, incrementAsync } }<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442:<\/p>\n<pre><code class=\"python\">test('should async increment after delay', async () =&gt; {   const { result, waitForNextUpdate } = renderHook(() =&gt; useCounter())    result.current.incrementAsync()    await waitForNextUpdate()    expect(result.current.count).toBe(1) })<\/code><\/pre>\n<p><code>act()<\/code> \u0442\u0443\u0442 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u2014 <code>waitForNextUpdate()<\/code> \u0443\u0436\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0451 \u0432 <code>act<\/code> \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 async-\u043f\u043e\u0442\u043e\u043a, \u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f <code>waitFor()<\/code>.<\/p>\n<p>\u0418 \u0435\u0449\u0451: \u0435\u0441\u043b\u0438 \u0432\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0435 <code>debounce<\/code>, <code>throttle<\/code> \u0438\u043b\u0438 <code>requestAnimationFrame<\/code> \u2014 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0430\u0439\u043c\u0435\u0440\u044b \u0447\u0435\u0440\u0435\u0437 <code>jest.useFakeTimers()<\/code>.<\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438<\/h3>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u0414\u0430, \u043c\u043e\u0436\u043d\u043e \u0438 \u044d\u0442\u043e. \u0412\u043e\u0442 \u0445\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432\u044b\u0448\u0435 9000:<\/p>\n<pre><code class=\"python\">export function useCounter(initialValue = 0) {   const [count, setCount] = useState(initialValue)   const increment = useCallback(() =&gt; setCount((x) =&gt; x + 1), [])      if (count &gt; 9000) {     throw new Error(\"It's over 9000!\")   }    return { count, increment } }<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f:<\/p>\n<pre><code class=\"python\">test('should throw when over 9000', () =&gt; {   const { result } = renderHook(() =&gt; useCounter(9000))    act(() =&gt; result.current.increment())    expect(result.error).toEqual(Error(\"It's over 9000!\")) })<\/code><\/pre>\n<p><code>result.error<\/code> \u2014 \u0440\u0435\u0434\u043a\u0430\u044f, \u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u041e\u043d\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u0430. \u0422\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f <code>useMemo<\/code>, <code>useEffect<\/code>, <code>useReducer<\/code> \u2014 \u0435\u0441\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0435.<\/p>\n<p><em>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0441 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e <\/em><a href=\"https:\/\/react-hooks-testing-library.com\/\" rel=\"noopener noreferrer nofollow\"><em>\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c<\/em><\/a><em>.<\/em><\/p>\n<hr\/>\n<p>\u0413\u043e\u0442\u043e\u0432\u044b \u0443\u0433\u043b\u0443\u0431\u0438\u0442\u044c \u0437\u043d\u0430\u043d\u0438\u044f \u0432\u00a0React \u0438 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0443\u043a\u043e\u0432? \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430 \u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430, \u0433\u0434\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443\u0442, \u043a\u0430\u043a\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c @testing\u2011library\/react\u2011hooks \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u00a0\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u043c\u0438:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/otus.pw\/mxyh\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a\u00a0\u0441\u0442\u0430\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c JavaScript\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c: \u043f\u043b\u0430\u043d \u043e\u0442\u00a0\u0434\u0436\u0443\u043d\u0430 \u0434\u043e\u00a0\u043c\u0438\u0434\u043b\u0430<\/a>\u00a0\u2014 10\u00a0\u0438\u044e\u043b\u044f \u0432 20:00 <\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/P91u\/\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u0447\u0435\u043c JavaScript\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0431\u044d\u043a\u0435\u043d\u0434? \u041e\u0442\u00a0fetch \u0434\u043e\u00a0Node.js<\/a>\u00a0\u2014 23\u00a0\u0438\u044e\u043b\u044f \u0432 20:00 <\/p>\n<\/li>\n<\/ol>\n<p><em>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 <\/em><a href=\"https:\/\/otus.pw\/e4xd\/\" rel=\"noopener noreferrer nofollow\"><em>\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439<\/em><\/a><em> <\/em><a href=\"https:\/\/otus.pw\/e4xd\/\" rel=\"noopener noreferrer nofollow\"><em>\u0442\u0435\u0441\u0442<\/em><\/a><em> \u0438 \u0443\u0437\u043d\u0430\u0439\u0442\u0435, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u044b \u0443\u0436\u0435 \u0432\u043b\u0430\u0434\u0435\u0435\u0442\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c React\u2011\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u0445\u0443\u043a\u043e\u0432. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u044b\u044f\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u043b\u044f\u00a0\u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f.<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/923214\/\"> https:\/\/habr.com\/ru\/articles\/923214\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-465844","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=465844"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465844\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=465844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=465844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=465844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}