all files / src/__tests__/ length.spec.js

100% Statements 52/52
100% Branches 4/4
100% Functions 13/13
100% Lines 48/48
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     25×                                
import assert from 'assert'
import Validators, { length } from '../index'
import getErrorId from './helper'
 
const ERROR_WRONG_LENGTH_ID = 'form.errors.wrongLength'
const ERROR_TOO_LONG_ID     = 'form.errors.tooLong'
const ERROR_TOO_SHORT_ID    = 'form.errors.tooShort'
 
function test (value, params) {
  return getErrorId(length(params)(value))
}
 
describe('Validator: length', function() {
  it('should be invalid when `value.length` is < min', function() {
    assert.equal(ERROR_TOO_SHORT_ID, test('foobar', { min: 7 }))
    assert.equal(ERROR_TOO_SHORT_ID, test('foobar', { minimum: 9 }))
  })
  it('should be invalid when `value.length` is > max', function() {
    assert.equal(ERROR_TOO_LONG_ID, test('f',       { max: 0 }))
    assert.equal(ERROR_TOO_LONG_ID, test('foobar',  { max: 5 }))
    assert.equal(ERROR_TOO_LONG_ID, test('foobar',  { maximum: 2 }))
  })
  it('should be invalid when `value.length` is not included in range', function() {
    assert.equal(ERROR_TOO_SHORT_ID, test('f',      { in: [2, 6] }))
    assert.equal(ERROR_TOO_LONG_ID, test('foobar',  { in: [0, 5] }))
    assert.equal(ERROR_TOO_LONG_ID, test('foobar',  { within: [5, 5] }))
  })
  it('should be invalid when `value.length` is != is', function() {
    assert.equal(ERROR_WRONG_LENGTH_ID, test('foobar', { '=': 5 }))
    assert.equal(ERROR_WRONG_LENGTH_ID, test('foobar', { is: 7 }))
  })
  it('should be valid when `value.length` is >= min', function() {
    assert.ok(!test('',       { min: 0 }))
    assert.ok(!test('foobar', { min: 0 }))
    assert.ok(!test('foobar', { minimum: 5 }))
  })
  it('should be valid when `value.length` is <= max', function() {
    assert.ok(!test('',       { max: 0 }))
    assert.ok(!test('foobar', { max: 6 }))
    assert.ok(!test('foobar', { maximum: 10 }))
  })
  it('should be valid when `value.length` is in range', function() {
    assert.ok(!test('',       { in: [0, 120] }))
    assert.ok(!test('foobar', { in: [6, 8] }))
    assert.ok(!test('foobar', { within: [0, 6] }))
  })
  it('should be valid when `value.length` is = is', function() {
    assert.ok(!test('',       { '=': 0 }))
    assert.ok(!test('foobar', { '=': 6 }))
    assert.ok(!test('foobar', { is: 6 }))
  })
  it('should use formatMessage', function() {
    let defaultValue = Validators.formatMessage
 
    Validators.formatMessage = function(msg) {
      return Object.assign({}, msg, { id: msg.id + '2' })
    }
    assert.equal(ERROR_TOO_SHORT_ID + '2', test('foobar', { min: 7 }))
    assert.equal(ERROR_TOO_LONG_ID + '2', test('f',       { max: 0 }))
    assert.equal(ERROR_WRONG_LENGTH_ID + '2', test('foobar', { is: 7 }))
 
    Validators.formatMessage = defaultValue;
  })
})