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

100% Statements 46/46
100% Branches 4/4
100% Functions 10/10
100% Lines 42/42
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     20×                                
import assert from 'assert'
import Validators, { acceptance } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.acceptance'
 
function test (value, params) {
  return getErrorId(acceptance(params)(value))
}
 
describe('Validator: acceptance', function () {
  it('should be invalid when `value` is not included in default values', function () {
    assert.equal(ERROR_ID, test())
    assert.equal(ERROR_ID, test(false))
    assert.equal(ERROR_ID, test(''))
    assert.equal(ERROR_ID, test('foo'))
    assert.equal(ERROR_ID, test('false'))
  })
  it('should be valid when `value` is included in default values', function () {
    assert.ok(!test(1))
    assert.ok(!test('1'))
    assert.ok(!test('true'))
    assert.ok(!test(true))
  })
  it('should be valid when `value` is included in custom values', function () {
    assert.ok(!test(1, { accept: '1' }))
    assert.ok(!test('1', { accept: 1 }))
    assert.ok(!test('foo', { accept: 'foo' }))
    assert.ok(!test(2, { accept: ['2'] }))
    assert.ok(!test('2', { accept: ['foo', 2] }))
  })
  it('should be invalid when `value` is not included in custom values', function () {
    assert.equal(ERROR_ID, test(null, { accept: '1' }))
    assert.equal(ERROR_ID, test('2', { accept: '1' }))
    assert.equal(ERROR_ID, test('2', { accept: ['foo', 3] }))
  })
  it('should use default accept option', function () {
    let defaultValue = Validators.defaultOptions.accept
 
    Validators.defaultOptions.accept = 'foo'
    assert.ok(!test('foo'))
 
    Validators.defaultOptions.accept = ['foo', 2]
    assert.ok(!test(2))
 
    Validators.defaultOptions.accept = 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())
 
    Validators.formatMessage = defaultValue
  })
})