- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 特性
- ReactJS - 優點與缺點
- ReactJS - 架構
- ReactJS - 建立 React 應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在 Expense Manager APP 中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理 API
- ReactJS - 無狀態元件
- ReactJS - 使用 React Hooks 進行狀態管理
- ReactJS - 使用 React Hooks 進行元件生命週期管理
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http 客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - Map
- ReactJS - 表格
- ReactJS - 使用 Flux 管理狀態
- ReactJS - 測試
- ReactJS - CLI 命令
- ReactJS - 構建和部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks 簡介
- ReactJS - 使用 useState
- ReactJS - 使用 useEffect
- ReactJS - 使用 useContext
- ReactJS - 使用 useRef
- ReactJS - 使用 useReducer
- ReactJS - 使用 useCallback
- ReactJS - 使用 useMemo
- ReactJS - 自定義 Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - 片段
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - 無 ES6 ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - Render Props
- ReactJS - 靜態型別檢查
- ReactJS - Strict Mode
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 行內樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - TestRenderer.act() 方法
React 測試對於確保我們的元件按預期執行非常重要。因此,我們將研究 TestRenderer.act() 函式,這是一個使測試更容易的有用工具。
TestRenderer.act() 是 'react-test-renderer' 庫中提供的函式。此方法允許我們準備 React 元件以進行斷言。它類似於 'react-dom/test-utils' 中的 act() 輔助函式,它使測試 TestRenderer 元件變得容易。
語法
TestRenderer.act(callback);
首先,我們將需要匯入必要的函式:
import { create, act } from 'react-test-renderer';
讓我們考慮一個例子,其中我們有一個名為 App 的元件,它接收一個名為 value 的 prop:
import App from './app.js';
// render the component
let root;
act(() => {
root = create(<App value={1} />);
});
// make assertions on root
expect(root.toJSON()).toMatchSnapshot();
在此程式碼中,我們使用值為 1 的 prop 建立了 App 元件的例項。act() 方法包裝了此建立過程,以確保元件已準備好進行測試。
現在,我們將使用不同的 props 更新元件:
// update with some different props
act(() => {
root.update(<App value={2} />);
});
// make assertions on root
expect(root.toJSON()).toMatchSnapshot();
再次使用 act(),我們可以安全地使用新的 props 更新元件並在更新後的元件上進行斷言。
示例
讓我們透過使用 TestRenderer.act(callback) 函式建立小型 React 應用來使用此資訊。我們將展示此函式如何在許多測試場景中很有用。
示例 - 基本元件
在這個第一個應用中,我們將檢查一個簡單的東西 - 我們稱之為“BasicComponent”的 React 程式的基本部分。它就像我們專案的一小部分。這部分不需要任何額外資訊。我們想看看當我們在螢幕上顯示它時它是否看起來正確。我們檢查的方式是建立它並確保它看起來完全符合我們的預期。
BasicComponent.js
import React from 'react';
const BasicComponent = () => {
return (
<div>
<h1>Hello, I'm a Basic Component!</h1>
<p>This is a simple React component for testing.</p>
</div>
);
};
export default BasicComponent;
App.js
import { create, act } from 'react-test-renderer';
// Import the component
import BasicComponent from './BasicComponent.js';
let root;
act(() => {
root = create(<BasicComponent />);
});
expect(root.toJSON()).toMatchSnapshot();
輸出
在這個例子中,BasicComponent 是一個基本的 React 函式式元件,它建立一個包含 h1 標題和 p 段落的 div。此元件用於測試,其輸出將與我們測試程式碼中的快照進行比較。
示例 - 帶有 Props 的元件
第二個應用程式用於測試名為 ComponentWithProps 的 React 元件,該元件接收一個名為 name 的 prop,其值為“John”。此示例演示如何使用 TestRenderer.act() 建立具有特定特性的元件例項,並確認呈現的結果與所需的快照匹配。
ComponentWithProps.js
import React from 'react';
const ComponentWithProps = (props) => {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This component receives a prop named "name" and displays a greeting.</p>
</div>
);
};
export default ComponentWithProps;
App.js
import { create, act } from 'react-test-renderer';
// Import the component
import ComponentWithProps from './ComponentWithProps.js';
let root;
act(() => {
root = create(<ComponentWithProps name="John" />);
});
expect(root.toJSON()).toMatchSnapshot();
輸出
在上面的程式碼中,ComponentWithProps 是另一個簡單的函式式元件,它接收一個名為 name 的 prop。此 prop 用於建立一個顯示在 h1 標題中的問候語。該段落還對元件進行了簡要說明。
示例 - 動態元件
在這個程式中,我們將演示如何測試名為 DynamicComponent 的動態 React 元件。此元件的生命週期將包括狀態更新或 prop 更改等更改。測試使用設定元件並透過更新它來重新建立動態更改。這展示了 TestRenderer.act() 如何處理動態元件行為。
DynamicComponent.js
import React, { useState, useEffect } from 'react';
const DynamicComponent = () => {
const [count, setCount] = useState(0);
useEffect(() => {
// Dynamic changes after component mount
const interval = setInterval(() => {
setCount((prevCount) => prevCount + 1);
}, 1000);
// Clean up interval on component unmount
return () => clearInterval(interval);
}, []);
return (
<div>
<h1>Dynamic Component</h1>
<p>This component changes dynamically. Count: {count}</p>
</div>
);
};
export default DynamicComponent;
App.js
import { create, act } from 'react-test-renderer';
// Import the component
import DynamicComponent from './DynamicComponent.js';
let root;
act(() => {
root = create(<DynamicComponent />);
});
// dynamic changes
act(() => {
root.update(<DynamicComponent />);
});
expect(root.toJSON()).toMatchSnapshot();
輸出
在這個例子中,DynamicComponent 是一個使用 useState 和 useEffect hook 的函式式元件。它使用 setInterval 啟動一個計時器,在初始化計數狀態後每秒遞增計數一次。這代表了元件隨時間推移的動態變化。
此外,上述示例展示瞭如何使用 TestRenderer.act() 測試各種型別的 React 元件。透過遵循這些模式,我們可以簡化測試過程並確保 React 應用程式的可靠性。
注意
在使用 react-test-renderer 時,使用 TestRenderer.act() 非常重要,以確保與 React 內部行為的相容性,並獲得正確且可靠的測試結果。
總結
TestRenderer.act() 是 react-test-renderer 庫中提供的函式。其主要目的是幫助測試 React 元件。它確保我們的測試以與 React 更新使用者介面非常相似的方式執行。因此,我們已經看到了它的工作原理以及如何在不同場景中使用此函式。