all files / src/__tests__/ allow-null.spec.js

100% Statements 13/13
83.33% Branches 5/6
100% Functions 4/4
100% Lines 9/9
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 66                                                                                                                    
import assert from 'assert'
import getErrorId from './helper'
 
import Validators, { absence,
  acceptance,
  addValidator,
  confirmation,
  date,
  email,
  exclusion,
  format,
  inclusion,
  length,
  numericality,
  presence,
  url } from '../index'
 
 
function test (func, value, params) {
  return getErrorId(func(params)(value)) || ''
}
 
describe('Validator option: allowNull', function() {
  it('should be invalid when `value` is null', function() {
    assert.ok(test(acceptance, null, { allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(confirmation, null, { allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(date, null, { format: 'mm/dd/yyyy', allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(email, null, { allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(exclusion, null, { in: [null], allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(format, null, { with: /^foo$/, allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(inclusion, null, { in: [], allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(length, null, { is: 300, allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(numericality, null, { allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(presence, null, { allowNull: false }).indexOf('form.errors') === 0)
    // assert.ok(test(url, null, { allowNull: false }).indexOf('form.errors') === 0)
  })
  // it('should be invalid when `value` is blank with allowNull: true', function() {
  //   assert.ok(!test(email, blank, { allowNull: true }))
  //   assert.ok(!test(date, blank, { format: 'mm/dd/yyyy', allowNull: true }))
  //   assert.ok(!test(exclusion, blank, { in: [null], allowNull: true }))
  //   assert.ok(!test(format, blank, { with: /^foo$/, allowNull: true }))
  //   assert.ok(!test(inclusion, blank, { in: [], allowNull: true }))
  //   assert.ok(!test(length, blank, { is: 300, allowNull: true }))
  //   assert.ok(!test(numericality, blank, { allowNull: true }))
  //   assert.ok(!test(url, blank, { allowNull: true }))
  // })
  // it('should use default allowNull option', function() {
  //   let defaultValue = Validators.defaultOptions.allowNull
  //
  //   ;[true, false].forEach(function(allowNull) {
  //     Validators.defaultOptions.allowNull = allowNull
  //
  //     assert.ok(allowNull !== (test(email, null).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(date, null, { format: 'mm/dd/yyyy' }).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(exclusion, null, { in: [null] }).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(format, null, { with: /^foo$/ }).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(inclusion, null, { in: [] }).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(length, null, { is: 300 }).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(numericality, null).indexOf('form.errors') === 0))
  //     assert.ok(allowNull !== (test(url, null).indexOf('form.errors') === 0))
  //   })
  //
  //   Validators.defaultOptions.allowNull = defaultValue
  // })
})