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

100% Statements 36/36
100% Branches 4/4
100% Functions 7/7
100% Lines 32/32
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     15×                    
import assert from 'assert'
import Validators, { presence, required } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.presence'
 
function test (value) {
  return getErrorId(presence()(value))
}
 
describe('Validator: presence', function () {
  it('should be invalid when `value` is empty', function () {
    assert.equal(ERROR_ID, test())
    assert.equal(ERROR_ID, test(null))
    assert.equal(ERROR_ID, test(''))
    assert.equal(ERROR_ID, test('   '))
    assert.equal(ERROR_ID, test(' \n \t '))
    assert.equal(ERROR_ID, test(new FileList()))
    assert.equal(ERROR_ID, test(new FileList({ length: 0 })))
    assert.equal(ERROR_ID, test(new FileList([])))
    assert.equal(ERROR_ID, getErrorId(required()(' \n \t '))) // Alias
  })
  it('should be valid when `value` is not empty', function () {
    assert.ok(!test(1))
    assert.ok(!test('str'))
    assert.ok(!test(' abc '))
    assert.ok(!test(new File()))
    assert.ok(!test(new FileList({ length: 1 })))
    assert.ok(!test(new FileList([{}])))
    assert.ok(!getErrorId(required()(' abc ')))
  })
  it('should use formatMessage', function () {
    let defaultValue = Validators.formatMessage
 
    Validators.formatMessage = function (msg) {
      return Object.assign({}, msg, { id: msg.id + '2' })
    }
    assert.equal(ERROR_ID + '2', test(''))
 
    Validators.formatMessage = defaultValue
  })
})