huridocs/uwazi

View on GitHub
app/react/Documents/utils/specs/documentValidator.spec.js

Summary

Maintainability
A
0 mins
Test Coverage
import validator, { required } from '../documentValidator';

describe('documentValidator', () => {
  const fieldsTemplate = [
    { name: 'field1', label: 'label1', required: true },
    { name: 'field2', label: 'label2', type: 'select', content: 'thesauriId' },
    { name: 'field3', label: 'label3', required: true },
  ];

  const template = { name: 'template1', _id: 'templateId', properties: fieldsTemplate };

  describe('required', () => {
    it('should return false on an empty string', () => {
      expect(required('')).toBe(false);
      expect(required('  ')).toBe(false);
      expect(required('value')).toBe(true);
    });
  });

  describe('generate', () => {
    it('should should generate an validation based on the template passed', () => {
      const validationObject = validator.generate(template);
      expect(validationObject.title).toEqual({ required });
      expect(validationObject['metadata.field1']).toEqual({ required });
      expect(validationObject['metadata.field3']).toEqual({ required });
    });
  });
});