我在输入上有一个 onChange 处理程序,我正在尝试根据我在 Dom 测试库文档中阅读的内容(此处和此处)来测试该处理程序。
我的代码中的一个区别是,我使用 props,而不是使用本地状态来控制输入值。所以 onChange 函数实际上是调用另一个函数(也通过 props 接收),它更新已“提升”到另一个组件的状态。最终,输入值作为 prop 被组件接收,并更新输入值。
我正在模拟 props 并尝试做一些简单的测试来证明 onChange 处理程序按预期工作。
我希望在更改处理程序中调用的函数被调用的次数与测试中使用 fireEvent.change 的次数相同,这适用于:
const { input } = setup();
fireEvent.change(input, { target: { value: "" } });
expect(handleInstantSearchInputChange).toHaveBeenCalledTimes(1);
Run Code Online (Sandbox Code Playgroud)
我希望 input.value 是从原始模拟道具设置中读取的,这适用于:
const { input } = setup();
expect(input.value).toBe("bacon");
Run Code Online (Sandbox Code Playgroud)
但是,我正在做一些愚蠢的事情(看起来根本不理解模拟函数),并且我无法弄清楚为什么以下块不更新 input.value,并继续从读取 input.value 设置原始的模拟道具设置。
由于期望“”/收到“培根”<=在原始道具中设置,因此失败
fireEvent.change(input, { target: { value: "" } });
expect(input.value).toBe("");
Run Code Online (Sandbox Code Playgroud)
问题:如何编写测试来证明 input.value 已根据下面的代码更改?我假设我需要模拟handleInstantSearchInputChange函数来执行某些操作,但我还不知道我在这里做什么。
感谢您提供有关如何执行和/或更好地理解这一点的任何建议。
测试文件
import React from "react";
import InstantSearchForm …Run Code Online (Sandbox Code Playgroud) 我正在为我的反应应用程序使用反应测试库。在一个测试用例中,我需要在文本框和焦点中填充一些值。
这是测试脚本 -
it('searchbox wrapper',async()=>{
let wrapper=getSearchBoxWrapperInstance('')
let inputBox=wrapper.findByTestId('inputText');
inputBox.value='12345';
fireEvent(inputBox,'focusOut');
})
Run Code Online (Sandbox Code Playgroud)
当我运行测试用例时,出现以下错误 -
TypeError: element.dispatchEvent is not a function
79 | let inputBox=wrapper.findByTestId('inputText');
80 | inputBox.value='12345';
> 81 | fireEvent(inputBox,'focusOut');
| ^
82 | //fireEvent('Blur',
83 |
84 | //await (() => wrapper.getByText('')))
at fireEvent (node_modules/dom-testing-library/dist/events.js:533:18)
Run Code Online (Sandbox Code Playgroud)
如果我可以提供更多信息,请告诉我
我有一些带有本机基础代码的react-native/expo,可以在手机或模拟器上正常运行。我尝试使用jest和react-native-testing-library为其创建一个测试。这样做时,无论里面有什么from native-base未渲染且无法在测试中找到。
有没有人经历过这个并且知道解决方案,以便在测试期间呈现 Content 的子项?
下面是一个示例代码来说明我的意思。非常感谢你的帮助。
import { render } from 'react-native-testing-library';
import {
Content, Container, Text
} from 'native-base';
class App extends React.Component {
render() {
return (
<Container>
<Content>
<Text testID="textId">Hello</Text>
</Content>
</Container>
);
}
}
describe('Testing Content', () => {
const { queryByTestId } = render(<App />)
it('renders text inside content', () => {
expect(queryByTestId('textId')).not.toBeNull()
});
})
Run Code Online (Sandbox Code Playgroud)
软件包的版本是:
"expo": "^32.0.0",
"react": "16.5.0",
"native-base": "^2.12.1",
"jest-expo": "^32.0.0",
"react-native-testing-library": "^1.7.0"
Run Code Online (Sandbox Code Playgroud) 我是编写单元测试的新手,我正在尝试使用testing-library/react和向我的反应应用程序编写单元测试jest
这是测试代码“Home.test.js”
import React from 'react';
import {render, cleanup} from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import Home from "../src/Home";
afterEach(cleanup);
describe("Tests for HomePage", function() {
it("should render without throwing an error", function() {
const { homePage } = render(<Home />);
//check something here
});
});
Run Code Online (Sandbox Code Playgroud)
这是我在组件“Home.js”中的代码
import * as React from "react";
import { Panel, Shell, Button } from "@myorg/core";
import { home_icon, new_icon } from "@myorg/icons";
function Home(props) {
const openDialog = React.useCallback(() => {
//do something
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 jest 和 react-testing-library 测试我的 LoginForm 组件。当登录表单成功提交时,我的handleLoginSuccess函数应该在localStorage上设置“用户”项,并使用history.push()将用户导航回主页。这在开发环境中的浏览器中有效,但是当我使用 Jest 渲染组件并模拟 API 时,localStorage 会更新,但不会导航到“/”。
我尝试在调用history.push()之前设置localStorage。我不确定在这种情况下是什么负责重新渲染,以及为什么它在开发中有效但在测试中无效。
登录.test.jsx
import 'babel-polyfill'
import React from 'react'
import {withRouter} from 'react-router'
import {Router} from 'react-router-dom'
import {createMemoryHistory} from 'history'
import {render, fireEvent} from '@testing-library/react'
import Login from '../../pages/Login'
import API from '../../util/api'
jest.mock('../../util/api')
function renderWithRouter(
ui,
{route = '/', history = createMemoryHistory({initialEntries: [route]})} = {},
) {
return {
...render(<Router history={history}>{ui}</Router>),
// adding `history` to the returned utilities to allow us
// to reference it in our …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jest 运行测试,但是收到此错误:
jest.mock 不是一个函数
我究竟做错了什么?这是我的 CodeSandbox:https://codesandbox.io/s/polling-hook-demo-9yo8c
请注意,我在 package.json 中配置 Jest:
"setupFilesAfterEnv": [
"./src/test/setupTestsAfterEnv.ts"
]
Run Code Online (Sandbox Code Playgroud)
setupTestsAfterEnv.ts 使用 jest-dom 扩展 Jest Expect:
import "@testing-library/jest-dom/extend-expect";
Run Code Online (Sandbox Code Playgroud)
我也在使用反应测试库。
更新
这似乎是我的 CodeSandbox 配置的问题。我在本地尝试了与 create-react-app 相同的操作,问题就消失了!
这是我的 Github 存储库,其中 Jest 运行得非常好: https: //github.com/nareshbhatia/hooked-on-polling。
然后,我将此存储库导入到新的 CodeSandbox 中: https: //codesandbox.io/s/hooked-on-polling-2em7g。当我在此沙箱中运行测试时,我再次收到相同的错误:jest.mock 不是函数
只是想确保我按照惯用方式执行此操作,并且我看到的示例似乎没有显示这一点...当我单击按钮时,我想检查 aria 属性是否已更新。为了让测试正常工作,我需要重新查询该元素。这是最好的方法,还是我应该等待一些事情?
test("should set aria-selected on tab after click", () => {
let secondTab = getAllByRole("tab")[1];
fireEvent.click(secondTab);
secondTab = getAllByRole("tab")[1]; // Is this proper?
expect(secondTab).toHaveAttribute("aria-selected", "true");
Run Code Online (Sandbox Code Playgroud) 我正在使用 jest 和react-testing-library 测试一个react 项目。
该项目有不同的 navigator.useragent 代码包含 android / mobile / blackberry 。
为了测试代码,我必须模拟 navigation.useragent。
我如何才能实现嘲笑。
我将 Jest 与react-testing-library一起使用,并在模拟推进计时器时遇到此警告:
console.error
Warning: An update to TimerProvider inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
Run Code Online (Sandbox Code Playgroud)
经过一番搜索,我发现了一个视频,建议将任何调用包装在函数jest.advanceTimersByTime中act()。
act(() => jest.advanceTimesByTime(1000);
Run Code Online (Sandbox Code Playgroud)
然而,我正在使用 TypeScript,现在对如何解决由此产生的类型错误感到困惑:
TS2769:Type 'typeof jest' is not assignable to type 'void'.
Run Code Online (Sandbox Code Playgroud)
如何正确修复此类型错误?
如何从 span 元素中找到 by text 的复选框?我无法修改我的 HTML。我希望在输入元素上测试 fireEvent。
`
<label>
<div>
<input type="checkbox" class="checkbox-extra-style">
<span> SPAN A</span>
</div>
</label>
<label>
<div>
<input type="checkbox" class="checkbox-extra-style">
<span> SPAN B</span>
</div>
</label>
Run Code Online (Sandbox Code Playgroud)
`` ....
非常感谢您的帮助。
reactjs ×7
jestjs ×6
javascript ×2
codesandbox ×1
expo ×1
html ×1
input ×1
native-base ×1
react-native ×1
react-router ×1
testing ×1
typescript ×1
unit-testing ×1