失败的测试切换 React Native 与测试库
Failed Testing Switch React Native with Testing Library
我在尝试使用 fireEvent“onChange”模拟事件时遇到以下错误:“类型错误:无法读取未定义的 属性 'value'”
我的组件:
const [isActive, setIsActive] = useState<boolean>(policy_value);
const handleChangeSwitch = () => {
const value = !isActive;
settingsService.updatePolicy(id, value);
setIsActive(!isActive);
};
<Switch value={isActive} onChange={handleChangeSwitch} testID="policy-switch"/>
我的测试:
const { getByTestId } = render(<Policy data={mockPolicy} />);
let switchButton = getByTestId("policy-switch")
fireEvent(switchButton, "onChange", { value: true });
expect(settingsService.updatePolicy).toHaveBeenCalled();
我试过了:
fireEvent(switchButton, "onChange", { value: true });
fireEvent(switchButton, "onChange", true);
fireEvent(switchButton, "onChange");
我通过将“onChange”更改为“onValueChange”解决了这个问题
<Switch value={isActive} onValueChange={handleChangeSwitch} testID="policy-switch"/>
const { getByTestId } = render(<Policy data={mockPolicy} />);
let switchButton = getByTestId("policy-switch")
fireEvent(switchButton, "onValueChange", true);
expect(settingsService.updatePolicy).toHaveBeenCalled();
我在尝试使用 fireEvent“onChange”模拟事件时遇到以下错误:“类型错误:无法读取未定义的 属性 'value'”
我的组件:
const [isActive, setIsActive] = useState<boolean>(policy_value);
const handleChangeSwitch = () => {
const value = !isActive;
settingsService.updatePolicy(id, value);
setIsActive(!isActive);
};
<Switch value={isActive} onChange={handleChangeSwitch} testID="policy-switch"/>
我的测试:
const { getByTestId } = render(<Policy data={mockPolicy} />);
let switchButton = getByTestId("policy-switch")
fireEvent(switchButton, "onChange", { value: true });
expect(settingsService.updatePolicy).toHaveBeenCalled();
我试过了:
fireEvent(switchButton, "onChange", { value: true });
fireEvent(switchButton, "onChange", true);
fireEvent(switchButton, "onChange");
我通过将“onChange”更改为“onValueChange”解决了这个问题
<Switch value={isActive} onValueChange={handleChangeSwitch} testID="policy-switch"/>
const { getByTestId } = render(<Policy data={mockPolicy} />);
let switchButton = getByTestId("policy-switch")
fireEvent(switchButton, "onValueChange", true);
expect(settingsService.updatePolicy).toHaveBeenCalled();