Javascript 输入不';使用React测试库进行测试时更新
我刚开始使用React测试库,第一次测试时遇到了一些问题 目的是测试输入值是否会根据值更改事件进行相应更新,但我发现以下错误:Javascript 输入不';使用React测试库进行测试时更新,javascript,reactjs,unit-testing,jestjs,react-testing-library,Javascript,Reactjs,Unit Testing,Jestjs,React Testing Library,我刚开始使用React测试库,第一次测试时遇到了一些问题 目的是测试输入值是否会根据值更改事件进行相应更新,但我发现以下错误: expect(received).toBe(expected) // Object.is equality Expected: "foo" Received: "" 我不明白我做错了什么,我遵循了文档并根据screen.debug()输入值是正确的: // more code
expect(received).toBe(expected) // Object.is equality
Expected: "foo"
Received: ""
我不明白我做错了什么,我遵循了文档并根据screen.debug()
输入值是正确的:
// more code
<input
id="autocomplete"
name="where"
placeholder="Where do you want to eat?"
type="text"
value="foo"
/>
// more code
//更多代码
//更多代码
以下是完整的测试文件:
import React from 'react';
import { render, fireEvent, cleanup, screen } from '@testing-library/react';
import { restContext } from './../../../context/restaurant/restContext';
import DisplaySearchBar from '../../../layout/DisplaySearchBar/DisplaySearchBar';
jest.mock('../../Alert/Alert', () => ({
default: () => null,
__esModule: true,
}));
const props = {
handleScriptLoad: jest.fn(),
};
let value = {
restaurants: [],
getRestaurants: jest.fn(),
setAlert: jest.fn(),
};
let wrapper = (
<restContext.Provider value={value}>
<DisplaySearchBar {...props} />
</restContext.Provider>
);
afterEach(cleanup);
describe('Search', () => {
test('1- input "where" updates its value when input simulated', () => {
render(wrapper);
let input = screen.getByPlaceholderText('What do you want to eat?');
fireEvent.change(input, {
target: { value: 'foo', name: 'where' },
});
screen.debug();
expect(input.value).toBe('foo');
});
});
从“React”导入React;
从'@testing library/react'导入{render,firevent,cleanup,screen};
从“/../../../context/restaurant/restContext”导入{restContext};
从“../../../layout/DisplaySearchBar/DisplaySearchBar”导入DisplaySearchBar;
jest.mock('../../Alert/Alert',()=>({
默认值:()=>null,
__艾斯莫杜勒:没错,
}));
常量道具={
handleScriptLoad:jest.fn(),
};
let值={
餐厅:[],
getRestaurants:jest.fn(),
setAlert:jest.fn(),
};
让包装器=(
);
每次之后(清理);
描述('搜索',()=>{
测试('1-输入“其中”在模拟输入时更新其值',()=>{
渲染(包装);
让输入=screen.getByPlaceholderText('你想吃什么?');
fireEvent.change(输入{
目标:{value:'foo',name:'where'},
});
screen.debug();
expect(input.value).toBe('foo');
});
});
谢谢你的帮助 测试中的占位符文本(“你想吃什么?”)与screen.debug()中的占位符=“你想吃什么?”不同