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

100% Statements 27/27
100% Branches 4/4
100% Functions 7/7
100% Lines 24/24
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     12×                
import assert from 'assert'
import { format } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.invalid'
 
function test (value, params) {
  return getErrorId(format(params)(value))
}
 
describe('Validator: format', function() {
  it('should be invalid when `value` doesn\'t match the with option', function() {
    assert.equal(ERROR_ID, test('',     { with: /123/ }))
    assert.equal(ERROR_ID, test(12,     { with: /123/ }))
    assert.equal(ERROR_ID, test('foo',  { with: /bar/ }))
  })
  it('should be invalid when `value` doesn\'t match the without option', function() {
    assert.equal(ERROR_ID, test('',     { without: /.?/ }))
    assert.equal(ERROR_ID, test(123,    { without: /\d+/ }))
    assert.equal(ERROR_ID, test('foo',  { without: /\w+/ }))
  })
  it('should be valid when `value` match the with option', function() {
    assert.ok(!test('',                 { with: /.?/ }))
    assert.ok(!test(123,                { with: /\d+/ }))
    assert.ok(!test('foo',              { with: /\w+/ }))
  })
  it('should be valid when `value` match the without option', function() {
    assert.ok(!test('',                 { without: /123/ }))
    assert.ok(!test(12,                 { without: /123/ }))
    assert.ok(!test('foo',              { without: /bar/ }))
  })
})