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

100% Statements 26/26
100% Branches 4/4
100% Functions 7/7
100% Lines 22/22
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                        
import assert from 'assert'
import Validators, { email } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.email'
 
function test (value, params) {
  return getErrorId(email(params)(value))
}
 
describe('Validator: email', function() {
  it('should be invalid when `value` is not a valid email', function() {
    assert.equal(ERROR_ID, test(''))
    assert.equal(ERROR_ID, test('foo'))
    assert.equal(ERROR_ID, test('foo@bar'))
    assert.equal(ERROR_ID, test('f@b.'))
  })
  it('should be valid when `value` is a valid email', function() {
    assert.ok(!test('a@b.com'))
    assert.ok(!test('foo@bar.net'))
  })
  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('foo'))
 
    Validators.formatMessage = defaultValue;
  })
})