I am using chai to try and unit test a validation function I have found online. This validation function is being used inside of a 'react-final-form' component.
Here is where I got this validator function from:
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. Any ideas on how to fix this test would be much appreciated.