{"size": 4078, "ext": "js", "max_stars_repo_path": "packages/q3-ui-forms/src/helpers/__tests__/validation.test.js", "max_stars_repo_name": "3merge/q", "max_stars_repo_stars_event_min_datetime": "2020-10-15T18:32:52.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-15T18:32:52.000Z", "max_issues_repo_path": "packages/q3-ui-forms/src/helpers/__tests__/validation.test.js", "max_issues_repo_name": "3merge/q", "max_issues_repo_issues_event_min_datetime": "2019-12-05T20:43:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-18T14:29:33.000Z", "max_forks_repo_path": "packages/q3-ui-forms/src/helpers/__tests__/validation.test.js", "max_forks_repo_name": "3merge/q", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 106, "max_forks_count": null, "avg_line_length": 23.7093023256, "max_line_length": 77, "alphanum_fraction": 0.4732712114}
import * as yup from 'yup';
import {
  tel,
  postal,
  autocomplete,
  checkIfRequired,
  checkIfEmpty,
} from '../validation';

describe('Custom validators', () => {
  describe('Required', () => {
    it('should detect required property', () =>
      expect(
        checkIfRequired({
          schema: { _exclusive: { required: true } },
        }),
      ).toBeTruthy());

    it('should detect empty values', () =>
      expect(checkIfEmpty('')).toBeTruthy());

    it('should detect undefined values casted accidentally to strings', () =>
      expect(checkIfEmpty('undefined')).toBeTruthy());
  });

  describe('Postal code', () => {
    const schema = yup.object().shape({
      postal: yup.string().test(postal).required(),
    });

    it('it should return false', (done) => {
      schema.isValid({ postal: 'LLL' }).then((valid) => {
        expect(valid).toBeFalsy();
        done();
      });
    });

    it('it should return true', (done) => {
      schema.isValid({ postal: 'L1S7E8' }).then((valid) => {
        expect(valid).toBeTruthy();
        done();
      });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ postal: 'L1s 7e8' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true', (done) => {
      schema.isValid({ postal: '90210' }).then((valid) => {
        expect(valid).toBeTruthy();
        done();
      });
    });
  });

  describe('Autocomplete value object', () => {
    const schema = yup.object().shape({
      autocomplete: yup
        .mixed()
        .test(autocomplete)
        .required(),
    });

    it('it should return false', (done) => {
      schema
        .isValid({ autocomplete: 'LLL' })
        .then((valid) => {
          expect(valid).toBeFalsy();
          done();
        });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ autocomplete: { value: 'hey' } })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true if empty and not required', (done) => {
      yup
        .object()
        .shape({
          autocomplete: yup.mixed().test(autocomplete),
        })
        .isValid({ autocomplete: '' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true if undefined and not required', (done) => {
      yup
        .object()
        .shape({
          autocomplete: yup.mixed().test(autocomplete),
        })
        .isValid({ autocomplete: undefined })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });
  });

  describe('Tel value object', () => {
    const schema = yup.object().shape({
      tel: yup.string().test(tel).required(),
    });

    it('it should return false', (done) => {
      schema.isValid({ tel: '093249' }).then((valid) => {
        expect(valid).toBeFalsy();
        done();
      });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ tel: '9053214455' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ tel: '+19053214455' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });
    it('it should return true', (done) => {
      schema
        .isValid({ tel: '905-321-4455x123' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ tel: '905-321-4455' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });

    it('it should return true', (done) => {
      schema
        .isValid({ tel: '(905) 321-4455' })
        .then((valid) => {
          expect(valid).toBeTruthy();
          done();
        });
    });
  });
});
