All files / src/validation textValidation.spec.ts

100% Statements 97/97
100% Branches 22/22
100% Functions 0/0
100% Lines 97/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { describe, expect, test } from 'vitest';
 
import { lengthIsEqualTo, maxLength, minLength, onlyAllowedChars } from './textValidation';
 
describe('Text Validation', () => {
  describe('minItems', () => {
    test('on null', () => {
      const text: any = null;
      const validator = minLength(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('on empty string', () => {
      const text = '';
      const validator = minLength(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with too few characters', () => {
      const text = 'abc';
      const validator = minLength(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with enough characters', () => {
      const text = 'testerString';
      const validator = minLength(5);
      expect(validator(text).isValid).toBeTruthy();
    });
  });
 
  describe('maxLength', () => {
    test('on null', () => {
      const text: any = null;
      const validator = maxLength(5);
      expect(validator(text).isValid).toBeTruthy();
    });
    test('on empty string', () => {
      const text = '';
      const validator = maxLength(5);
      expect(validator(text).isValid).toBeTruthy();
    });
    test('with too many characters', () => {
      const text = 'testerString';
      const validator = maxLength(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with correct number of characters', () => {
      const text = 'abc';
      const validator = maxLength(5);
      expect(validator(text).isValid).toBeTruthy();
    });
  });
 
  describe('lengthIsEqualTo', () => {
    test('on null', () => {
      const text: any = null;
      const validator = lengthIsEqualTo(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('on empty string', () => {
      const text = '';
      const validator = lengthIsEqualTo(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with too many characters', () => {
      const text = 'testerString';
      const validator = lengthIsEqualTo(5);
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with correct number of characters', () => {
      const text = 'abc';
      const validator = lengthIsEqualTo(3, 5);
      expect(validator(text).isValid).toBeTruthy();
    });
    test('with incorrect length options', () => {
      const text = 'abc';
      const validator = lengthIsEqualTo(1, 5, 7);
      expect(validator(text).isValid).toBeFalsy();
    });
  });
 
  describe('onlyAllowedChars', () => {
    test('on null', () => {
      const text: any = null;
      const validator = onlyAllowedChars('a');
      expect(validator(text).isValid).toBeTruthy();
    });
    test('on empty string', () => {
      const text = '';
      const validator = onlyAllowedChars('a');
      expect(validator(text).isValid).toBeTruthy();
    });
    test('with invalid characters', () => {
      const text = 'testerString';
      const validator = onlyAllowedChars('b');
      expect(validator(text).isValid).toBeFalsy();
    });
    test('with valid characters', () => {
      const text = 'abc';
      const validator = onlyAllowedChars('abcd');
      expect(validator(text).isValid).toBeTruthy();
    });
  });
});