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

100% Statements 37/37
100% Branches 4/4
100% Functions 8/8
100% Lines 33/33
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     13×                            
import assert from 'assert'
import Validators, { exclusion } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.exclusion'
 
function test (value, params) {
  return getErrorId(exclusion(params)(value))
}
 
describe('Validator: exclusion', function() {
  it('should be invalid when `value` is in the list', function() {
    assert.equal(ERROR_ID, test(1,     { in: [9, 8, '1'] }))
    assert.equal(ERROR_ID, test('1',   { in: [9, 8, 1] }))
    assert.equal(ERROR_ID, test('foo', { within: 'foo' }))
    assert.equal(ERROR_ID, test('foo', { within: ['foo'], caseSensitive: true }))
    assert.equal(ERROR_ID, test('FOO', { within: ['foo'], caseSensitive: false }))
  })
  it('should be valid when `value` is not in the list', function() {
    assert.ok(!test(1,     { in: [] }))
    assert.ok(!test('1',   { in: [2, 3, 4] }))
    assert.ok(!test('foo', { within: 'foobar' }))
    assert.ok(!test('foo', { within: ['FOO'], caseSensitive: true }))
    assert.ok(!test('FOO', { within: ['bar'], caseSensitive: false }))
  })
  it('should use default caseSensitive option', function() {
    let defaultValue = Validators.defaultOptions.caseSensitive
 
    Validators.defaultOptions.caseSensitive = true
    assert.ok(!test('foo', { within: 'FOO' }))
 
    Validators.defaultOptions.caseSensitive = false
    assert.ok(!test('foo', { within: 'fooo' }))
 
    Validators.defaultOptions.caseSensitive = defaultValue;
  })
  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('foo', { within: 'foo' }))
 
    Validators.formatMessage = defaultValue;
  })
})