简体   繁体   English

我正在尝试测试将另一个 function 作为参数的 function

[英]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.

相关问题 我正在尝试将事件目标值传递给另一个函数 - I'm trying to pass event target values to another function 对以另一个函数作为参数的函数进行 JSDoc 注释 - Make JSDoc comment for function which takes another function as parameter 我正在尝试获取一个 javascript 函数,它将通过使用回调方法返回一个新数组,并将一个数组和两个回调作为 arg - I'm trying to get a javascript function that will return a new array by using callback method and takes a array and two callbacks as arg 我正在尝试将数组映射到另一个映射函数中,我的代码未同步运行 - I'm trying to map an array inside another map function my code is not running synchronously 以数组为参数的JavaScript函数 - JavaScript function that takes an array as a parameter 我正在尝试将此php函数转换为javascript函数 - I'm trying to turn this php function into a javascript function 我需要编写一个 function,它采用华氏度的单个参数并将其转换为摄氏度 - I need to write a function that takes a single parameter in Fahrenheit and converts it to celsius 如何绑定到以功能为参数的功能 - How to bind to function that takes function as parameter 创建 _.memoize function 将 function 作为参数 - creating _.memoize function that takes in a function as a parameter 如果另一个 function 需要更多时间,我如何在另一个 function 完成后调用 function? - How can i call a function after completion of another function if the another function takes more time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM