[英]I'm trying to test a function that takes another function as a parameter
I am using chai to try and unit test a validation function I have found online.我正在使用 chai 尝试对我在网上找到的验证 function 进行单元测试。 This validation function is being used inside of a 'react-final-form' component.
此验证 function 正在“react-final-form”组件内部使用。
Here is where I got this validator function from:这是我从以下位置获得此验证器 function 的地方:
https://youtu.be/OEg8jm-NbQ0?t=567 https://youtu.be/OEg8jm-NbQ0?t=567
import chai, { expect } from "chai";
import chaiEnzyme from "chai-enzyme";
chai.use(chaiEnzyme());
const required = (value) => value === '' ? 'This is required.' : undefined;
const url = (value) => value && !(/^\/[a-z0-9]+$|[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi).test(value) ? 'This does not appear to be a link.': undefined;
const composeValidators = (...validators) =>
(value) =>
validators.reduce((error, validator) => error || validator(value), undefined);
let value = '';
describe("Forms", () => {
describe("Final form", () => {
describe("Utils", () => {
it("Returns correct error message when form field value is empty and field is required", () => {
expect(composeValidators(required)).to.equal('This is required.');
});
it("Returns correct error message when form field value is not empty and field should be a url", () => {
value = 'not empty';
expect(composeValidators(url)).to.equal('This does not appear to be a link.');
});
});
});
});
Currently both assertions are returning [function] instead of the string value I am expecting and I'm not sure why.目前这两个断言都返回 [function] 而不是我期望的字符串值,我不知道为什么。 Any ideas on how to fix this test would be much appreciated.
任何有关如何修复此测试的想法将不胜感激。
I do love final form.我确实喜欢最终形式。 It really helped drill home how to use currying functions.
它确实有助于深入了解如何使用柯里化函数。 So, the example you grabbed from final form I've personally used with fields.
因此,您从我个人使用的最终表单中获取的示例与字段。
composeValidators(required)(value)
Currying is an odd concept that you can get by without needing it so don't fret over not understanding it at first glance. Currying 是一个奇怪的概念,你不需要它就可以理解它,所以不要担心乍一看不理解它。
Let's look at the signature:让我们看一下签名:
const composeValidators = (...validators) => (value) =>
The first function takes X number of props or validation rules.第一个 function 采用 X 个道具或验证规则。 You've got that part down.
你已经把那部分记下来了。 The second function, now, expects a value.
现在,第二个 function 需要一个值。 You probably saw it used with Field's validate props.
您可能看到它与 Field 的 validate 道具一起使用。 If you look at the documentation for FieldProps (see link below), you'll see it takes a function and passes it 3 arguments, value, allValues, and meta, not just value.
如果您查看 FieldProps 的文档(请参阅下面的链接),您会看到它需要一个 function 并传递给它 3 arguments、value、allValues 和 meta,而不仅仅是 value。 This can help you write better validation rules that take into account more information about the field.
这可以帮助您编写更好的验证规则,以考虑有关该字段的更多信息。 Anyway, when final-form uses this, it takes the form of:
无论如何,当 final-form 使用它时,它采用以下形式:
composeValidators(required)(value, allValues, meta)
I don't expect this to make sense right away but it should help you think about one specific use case of currying has some advanced options.我不希望这立即有意义,但它应该可以帮助您考虑一个特定的柯里化用例有一些高级选项。 Enjoy Final Form!
享受最终形式!
https://final-form.org/docs/react-final-form/types/FieldProps https://final-form.org/docs/react-final-form/types/FieldProps
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.