all files / src/__tests__/ if-unless.spec.js

100% Statements 35/35
100% Branches 20/20
100% Functions 7/7
100% Lines 30/30
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       36× 36× 24× 24×   24×   36× 24× 12×   24×   36×              
import assert from 'assert'
import { absence, date, acceptance, confirmation, email, exclusion,
  format, inclusion, length, numericality, presence, url } from '../index'
import getErrorId from './helper'
 
 
function test (type, func, value, params={}, allValues={}) {
  if (!type || 'if' === type) {
    params.if = function(values) {
      return values.foo === 'foo'
    }
    allValues.foo = ''
  }
  if (!type || 'unless' === type) {
    params.unless = function(values) {
      return values.bar !== 'bar'
    }
    allValues.bar = ''
  }
  return getErrorId(func(params)(value, allValues))
}
 
describe('Validator option: if & unless', function() {
  it('should not return an error', function() {
    let blank = ''
    ;['', 'if', 'unless'].forEach(function(type) {
      // All these tests normally return an error
      assert.ok(!test(type, absence, 'foo'))
      assert.ok(!test(type, acceptance))
      assert.ok(!test(type, confirmation, 'foo', { field: 'bar' }, {}))
      assert.ok(!test(type, email, blank))
      assert.ok(!test(type, date, blank, { format: 'mm/dd/yyyy' }))
      assert.ok(!test(type, exclusion, blank, { in: [blank] }))
      assert.ok(!test(type, format, blank, { with: /^foo$/ }))
      assert.ok(!test(type, inclusion, blank, { in: [] }))
      assert.ok(!test(type, length, blank, { is: 300 }))
      assert.ok(!test(type, numericality, blank))
      assert.ok(!test(type, presence, blank))
      assert.ok(!test(type, url, blank))
    })
  })
})