All files / src/validation baseValidation.spec.ts

100% Statements 47/47
100% Branches 10/10
100% Functions 2/2
100% Lines 47/47

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 511x   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 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { describe, expect, test } from 'vitest';
 
import { multiValidation, noValidation, notNull } from './baseValidation';
 
describe('Base Validation', () => {
  test('noValidation always valid', () => {
    expect(noValidation({}).isValid).toBeTruthy();
  });
  test('notNullValidator on notNull', () => {
    const arr: any = {};
    const validator = notNull();
    expect(validator(arr).isValid).toBeTruthy();
  });
  test('notNullValidator on null', () => {
    const arr: any = null;
    const validator = notNull();
    expect(validator(arr).isValid).toBeFalsy();
  });
  test('notNullValidator custom error message', () => {
    const arr: any = null;
    const err = 'tester test test';
    const validator = notNull(err);
    expect(validator(arr).isValid).toBeFalsy();
    expect(validator(arr).errorMessage).toBe(err);
  });
 
  describe('Multi Validation ', () => {
    test('multiple validators exec each validator', () => {
      let count = 0;
      const fakeValidator = () => {
        count++;
        return { isValid: true };
      };
      const valArr = [fakeValidator, fakeValidator, fakeValidator];
      const validator = multiValidation(...valArr);
      validator({});
      expect(count).toBe(valArr.length);
    });
    test('multiple validators exec each validator until failure', () => {
      let count = 0;
      const fakeValidator = () => {
        count++;
        return { isValid: false };
      };
      const validator = multiValidation(fakeValidator, fakeValidator, fakeValidator);
      validator({});
      expect(count).toBe(1);
    });
  });
});