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

100% Statements 39/39
100% Branches 8/8
100% Functions 9/9
100% Lines 35/35
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     13× 13×                              
import assert from 'assert'
import Validators, { confirmation } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.confirmation'
 
function test (value, params, allValues) {
  params = Object.assign({ field: 'password_confirmation' }, params || {})
  return getErrorId(confirmation(params)(value, allValues || { password_confirmation: 'validator' }))
}
 
describe('Validator: confirmation', function () {
  it('should be invalid when `value` != confirmation', function () {
    assert.equal(ERROR_ID, test('val'))
    assert.equal(ERROR_ID, test('val', { caseSensitive: false }))
    assert.equal(ERROR_ID, test('VALIDATOR', { caseSensitive: true }))
    assert.equal(ERROR_ID, test('valiDator', { caseSensitive: true }))
  })
  it("should be invalid when confirmation field doesn't exists", function () {
    assert.equal(ERROR_ID, test('validator', { field: 'passwordconfirmation' }))
  })
  it('should be valid when `value` = confirmation', function () {
    assert.ok(!test('', { field: 'foo' }, {}))
    assert.ok(!test('validator'))
    assert.ok(!test('VALIDATOR', { caseSensitive: false }))
    assert.ok(!test('valiDator', { caseSensitive: false }))
    assert.ok(!test(123, { field: 'foo' }, { foo: '123' }))
  })
  it('should use default caseSensitive option', function () {
    let defaultValue = Validators.defaultOptions.caseSensitive
 
    Validators.defaultOptions.caseSensitive = true
    assert.ok(!test('validator'))
 
    Validators.defaultOptions.caseSensitive = false
    assert.ok(!test('VALIDATOR'))
 
    Validators.defaultOptions.caseSensitive = defaultValue
  })
  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('val'))
 
    Validators.formatMessage = defaultValue
  })
})