如何解决Jest和React测试库:间谍函数未调用
我正在进行一项测试,该测试测试选择输入上的更改事件是否会触发所选择的正确语言的正确功能(这是一个选择输入,用户可以选择不同语言的选项)。
这是我的测试:由于某种原因,未调用函数,我无法弄清楚!!
it('sets the locale cookie and makes a post request if the selected locale is different from the previous',() => {
jest.spyOn(Cookies,'get').mockImplementation(() => 'en');
jest.spyOn(Cookies,'set');
jest.spyOn(axios,'post').mockImplementation(() => Promise.resolve());
const { wrapper,getByTestId } = render(component);
const selectInput = getByTestId('inputSelect');
fireEvent.change(selectInput,{ target: { currentTarget: 'es' } });
expect(Cookies.set).toHaveBeenCalledWith('locale','es');
expect(axios.post).toHaveBeenCalledWith('/toggle_locale',{ locale: 'es' });
});
这是我正在测试的组件的元素:
const toggleValue = (e: SyntheticEvent<HTMLInputElement>) => {
setValue(e.currentTarget.value);
if (onChange) {
onChange(e);
}
};
<select
id={id}
name={name}
aria-label={label || ariaLabel}
value={value}
onChange={toggleValue}
data-testid="inputSelect"
>
{options.map((option: Option) => (
<option id={option.id} value={option.value} key={option.value}>
{renderHTML(option.label)}
</option>
))}
</select>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。