import { integer, length, maxLength, maxValue, minValue } from './validators';

describe('integer validator', () => {
  it('accepts an integer', () => {
    const returned = integer('123');
    expect(returned).toBe(undefined);
  });

  it("doesn't accept a float", () => {
    const returned = integer('123.5569');
    expect(returned).toBe('Must be a whole number');
  });
});

describe('length validator', () => {
  it('fails for invalid Length', () => {
    const length10 = length(10);
    const tooLong = length10('01234567890');
    const tooShort = length10('aaa');
    expect(tooLong).toBe('Length must be 10');
    expect(tooShort).toBe('Length must be 10');
  });

  it('succeeds for correct length', () => {
    const length10 = length(10);
    const validString = length10('0123456789');
    const validNumber = length10(1234567890);
    expect(validString).toBe(undefined);
    expect(validNumber).toBe(undefined);
  });
});

describe('maxValue validator', () => {
  it('fails for invalid value', () => {
    const maxValue10 = maxValue(10);
    const tooBig = maxValue10(11);
    expect(tooBig).toBe('Maximum value 10 exceeded');
  });

  it('succeeds for valid value', () => {
    const maxValue10 = maxValue(10);
    const maxAllowable = maxValue10(10);
    const lowAllowable = maxValue10(1);
    expect(maxAllowable).toBe(undefined);
    expect(lowAllowable).toBe(undefined);
  });
});

describe('minValue validator', () => {
  it('fails for invalid value', () => {
    const minValue10 = minValue(10);
    const tooSmall = minValue10(9);
    expect(tooSmall).toBe('Minimum value 10 not met');
  });

  it('succeeds for valid value', () => {
    const minValue10 = minValue(10);
    const minAllowable = minValue10(10);
    const highAllowable = minValue10(15);
    expect(minAllowable).toBe(undefined);
    expect(highAllowable).toBe(undefined);
  });
});

describe('maxLength validator', () => {
  it('fails for invalid value', () => {
    const maxLength10 = maxLength(10);
    const stringTooLong = maxLength10('012345678910');
    const numberTooLong = maxLength10(1234567890123);
    expect(stringTooLong).toBe('Maximum length 10 exceeded');
    expect(numberTooLong).toBe('Maximum length 10 exceeded');
  });

  it('succeeds for valid value', () => {
    const maxLength10 = maxLength(10);
    const exactMatch = maxLength10('0123456789');
    const shortValid = maxLength10('0123');
    expect(exactMatch).toBe(undefined);
    expect(shortValid).toBe(undefined);
  });
});
