使用 jest 和 testing-library 测试组件内部的函数
Testing a function inside a component with jest and testing-library
我是 testing-library 和 jest 的新手,我正在尝试测试组件内部更改输入值的函数。该组件是一个表单,另一个组件是输入。
export const Form = () => {
const [name, setName] = useState("");
const handleOnSubmit = e => {
e.preventDefault();
const form = e.target;
};
const inputChange = (param) => (e) => {
const inputValue = e.target.value;
setName(inputValue);
};
return (
<form className="form" onSubmit={handleOnSubmit}>
<InputGroup text="name" type="text" value={name} functionality={inputChange("name")}/>
<Button type="submit" disabled={name === undefined}/>
</form>
);
};
export default Form;
InputGroup 组件如下所示
export const InputGroup = ({type, id, value, required, functionality, text}) => {
return (
<label>{text}</label>
<input className="input" type={type} id={id} name={id} value={value}
required={required} onChange={functionality}
/>
);
};
我已经尝试过类似的方法,但我不太确定如何测试直接在组件 Form 上并且正在传递给组件 InputGroup 的函数。
describe("Form", () => {
let value;
let component;
const handleSubmit = jest.fn();
const handleChange = ev => {
ev.preventDefault();
value = ev.currentTarget.value;
}
beforeEach(() => {
component = render(
<Form onSubmit={handleSubmit} functionality={handleChange} />
);
});
it("check error name is triggered", () => {
const input = component.getByText("name");
fireEvent.change(input, {target: {value: "aaa"}});
});
});
我收到一条错误消息“给定元素没有值 setter”,那么如何将 inputChange 函数传递给 InputGroup 组件?
好的,感谢分享InputGroup
。所以你可以很容易地测试InputGroup
。
describe("InputGroup", () => {
it("check error name is triggered", () => {
const fn = jest.fn()
render(<InputGroup functionality={functionality} />)
fireEvent.change(input, {target: {value: "aaa"}});
expect(fn).toHaveBeenLastCalledWith(...)
});
});
只是一个想法。上面的代码可能与发布的 运行 不同。好了,现在进入Form
。步骤可能类似,但可能需要一些时间来解决,但您无需再次测试功能,因为此道具与 Form
无关。从表单的角度来看,它只关心 onSubmit
,因此您可以按照之前发布的类似方式对其进行测试。
我是 testing-library 和 jest 的新手,我正在尝试测试组件内部更改输入值的函数。该组件是一个表单,另一个组件是输入。
export const Form = () => {
const [name, setName] = useState("");
const handleOnSubmit = e => {
e.preventDefault();
const form = e.target;
};
const inputChange = (param) => (e) => {
const inputValue = e.target.value;
setName(inputValue);
};
return (
<form className="form" onSubmit={handleOnSubmit}>
<InputGroup text="name" type="text" value={name} functionality={inputChange("name")}/>
<Button type="submit" disabled={name === undefined}/>
</form>
);
};
export default Form;
InputGroup 组件如下所示
export const InputGroup = ({type, id, value, required, functionality, text}) => {
return (
<label>{text}</label>
<input className="input" type={type} id={id} name={id} value={value}
required={required} onChange={functionality}
/>
);
};
我已经尝试过类似的方法,但我不太确定如何测试直接在组件 Form 上并且正在传递给组件 InputGroup 的函数。
describe("Form", () => {
let value;
let component;
const handleSubmit = jest.fn();
const handleChange = ev => {
ev.preventDefault();
value = ev.currentTarget.value;
}
beforeEach(() => {
component = render(
<Form onSubmit={handleSubmit} functionality={handleChange} />
);
});
it("check error name is triggered", () => {
const input = component.getByText("name");
fireEvent.change(input, {target: {value: "aaa"}});
});
});
我收到一条错误消息“给定元素没有值 setter”,那么如何将 inputChange 函数传递给 InputGroup 组件?
好的,感谢分享InputGroup
。所以你可以很容易地测试InputGroup
。
describe("InputGroup", () => {
it("check error name is triggered", () => {
const fn = jest.fn()
render(<InputGroup functionality={functionality} />)
fireEvent.change(input, {target: {value: "aaa"}});
expect(fn).toHaveBeenLastCalledWith(...)
});
});
只是一个想法。上面的代码可能与发布的 运行 不同。好了,现在进入Form
。步骤可能类似,但可能需要一些时间来解决,但您无需再次测试功能,因为此道具与 Form
无关。从表单的角度来看,它只关心 onSubmit
,因此您可以按照之前发布的类似方式对其进行测试。