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

100% Statements 34/34
100% Branches 4/4
100% Functions 9/9
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     13×                        
import assert from 'assert'
import Validators, { 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/ }))
  })
  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(123, { without: /\d+/ }))
 
    Validators.formatMessage = defaultValue
  })
})