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

100% Statements 33/33
100% Branches 4/4
100% Functions 7/7
100% Lines 29/29
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     14×                    
import assert from 'assert'
import Validators, { absence } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.absence'
 
function test (value) {
  return getErrorId(absence()(value))
}
 
describe('Validator: absence', function () {
  it('should be invalid when `value` is not empty', function () {
    assert.equal(ERROR_ID, test(1))
    assert.equal(ERROR_ID, test('str'))
    assert.equal(ERROR_ID, test(' abc '))
    assert.equal(ERROR_ID, test(new File()))
    assert.equal(ERROR_ID, test(new FileList({ length: 1 })))
    assert.equal(ERROR_ID, test(new FileList([{}])))
  })
  it('should be valid when `value` is empty', function () {
    assert.ok(!test())
    assert.ok(!test(''))
    assert.ok(!test('   '))
    assert.ok(!test(' \n \t '))
    assert.ok(!test(new FileList()))
    assert.ok(!test(new FileList([])))
    assert.ok(!test(new FileList({ length: 0 })))
  })
  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(1))
 
    Validators.formatMessage = defaultValue
  })
})