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

100% Statements 36/36
100% Branches 20/20
100% Functions 7/7
100% Lines 31/31
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                               39× 39× 26× 26×   26×   39× 26× 13×   26×   39×              
import assert from 'assert'
import {
  absence,
  date,
  acceptance,
  confirmation,
  email,
  exclusion,
  file,
  format,
  inclusion,
  length,
  numericality,
  presence,
  url
} from '../index'
import getErrorId from './helper'
 
function test (type, func, value, params = {}, allValues = {}) {
  if (!type || type === 'if') {
    params.if = function (values) {
      return values.foo === 'foo'
    }
    allValues.foo = ''
  }
  if (!type || type === 'unless') {
    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, file, blank, { minFiles: 9 }))
      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))
    })
  })
})