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

100% Statements 128/128
100% Branches 4/4
100% Functions 23/23
100% Lines 125/125
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151     89×                                                  
import assert from 'assert'
import { numericality } from '../index'
import getErrorId from './helper'
 
const ERROR_NOT_A_NUMBER_ID       = 'form.errors.notANumber'
const ERROR_EQUAL_TO_ID           = 'form.errors.equalTo'
const ERROR_OTHER_THAN_ID         = 'form.errors.otherThan'
const ERROR_GREATER_THAN_ID       = 'form.errors.greaterThan'
const ERROR_GREATER_THAN_OR_EQ_ID = 'form.errors.greaterThanOrEqualTo'
const ERROR_LESS_THAN_ID          = 'form.errors.lessThan'
const ERROR_LESS_THAN_OR_EQ_ID    = 'form.errors.lessThanOrEqualTo'
const ERROR_EVEN_ID               = 'form.errors.even'
const ERROR_ODD_ID                = 'form.errors.odd'
 
function test (value, params) {
  return getErrorId(numericality(params)(value))
}
 
describe('Validator: numericality', function () {
  it('should be invalid when `value` is not a number', function() {
    assert.equal(ERROR_NOT_A_NUMBER_ID, test(''))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test(null))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('foobar'))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('12foobar'))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('foobar12'))
  })
  it('should be invalid when `value` is not a integer', function() {
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('',             { int: true }))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('foobar',       { int: true }))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('12.345',       { int: true }))
    assert.equal(ERROR_NOT_A_NUMBER_ID, test('0.01',         { integer: true }))
  })
  it('should be invalid when `value` is not = to equalTo', function() {
    assert.equal(ERROR_EQUAL_TO_ID, test(1,                  { '=': 0 }))
    assert.equal(ERROR_EQUAL_TO_ID, test('1',                { '=': 0 }))
    assert.equal(ERROR_EQUAL_TO_ID, test('18',               { '=': 12 }))
    assert.equal(ERROR_EQUAL_TO_ID, test('12.345',           { '=': 12.34 }))
    assert.equal(ERROR_EQUAL_TO_ID, test('0.01',             { 'equalTo': '12.34' }))
  })
  it('should be invalid when `value` is = to otherThan', function() {
    assert.equal(ERROR_OTHER_THAN_ID, test(1,                { '!=': 1 }))
    assert.equal(ERROR_OTHER_THAN_ID, test('1',              { '!=': 1 }))
    assert.equal(ERROR_OTHER_THAN_ID, test('18',             { '!=': 18 }))
    assert.equal(ERROR_OTHER_THAN_ID, test('12.345',         { '!=': 12.345 }))
    assert.equal(ERROR_OTHER_THAN_ID, test('0.01',           { 'otherThan': '0.01' }))
  })
  it('should be invalid when `value` is not > to greaterThan', function() {
    assert.equal(ERROR_GREATER_THAN_ID, test(-1,             { '>': 0 }))
    assert.equal(ERROR_GREATER_THAN_ID, test('0',            { '>': 0 }))
    assert.equal(ERROR_GREATER_THAN_ID, test('12',           { '>': 12 }))
    assert.equal(ERROR_GREATER_THAN_ID, test('12.34',        { '>': 12.34 }))
    assert.equal(ERROR_GREATER_THAN_ID, test('11.34',        { 'greaterThan': '12.34' }))
  })
  it('should be invalid when `value` is not >= to greaterThanOrEqualTo', function() {
    assert.equal(ERROR_GREATER_THAN_OR_EQ_ID, test(-1,       { '>=': 0 }))
    assert.equal(ERROR_GREATER_THAN_OR_EQ_ID, test('-0.01',  { '>=': 0 }))
    assert.equal(ERROR_GREATER_THAN_OR_EQ_ID, test('11.9',   { '>=': 12 }))
    assert.equal(ERROR_GREATER_THAN_OR_EQ_ID, test('12.339', { '>=': 12.34 }))
    assert.equal(ERROR_GREATER_THAN_OR_EQ_ID, test('12.339', { 'greaterThanOrEqualTo': '12.34' }))
  })
  it('should be invalid when `value` is not < to lessThan', function() {
    assert.equal(ERROR_LESS_THAN_ID, test(1,                 { '<': 0 }))
    assert.equal(ERROR_LESS_THAN_ID, test('0',               { '<': 0 }))
    assert.equal(ERROR_LESS_THAN_ID, test('12',              { '<': 12 }))
    assert.equal(ERROR_LESS_THAN_ID, test('12.34',           { '<': 12.34 }))
    assert.equal(ERROR_LESS_THAN_ID, test('13.34',           { 'lessThan': '12.34' }))
  })
  it('should be invalid when `value` is not <= to lessThanOrEqualTo', function() {
    assert.equal(ERROR_LESS_THAN_OR_EQ_ID, test(1,           { '<=': 0 }))
    assert.equal(ERROR_LESS_THAN_OR_EQ_ID, test('0.01',      { '<=': 0 }))
    assert.equal(ERROR_LESS_THAN_OR_EQ_ID, test('12.1',      { '<=': 12 }))
    assert.equal(ERROR_LESS_THAN_OR_EQ_ID, test('12.341',    { '<=': 12.34 }))
    assert.equal(ERROR_LESS_THAN_OR_EQ_ID, test('13.34',     { 'lessThanOrEqualTo': '12.34' }))
  })
  it('should be invalid when `value` is not even', function() {
    assert.equal(ERROR_EVEN_ID, test('1',                    { even: true }))
    assert.equal(ERROR_EVEN_ID, test('3',                    { even: true }))
    assert.equal(ERROR_EVEN_ID, test('-1',                   { even: true }))
    assert.equal(ERROR_EVEN_ID, test('15.9',                 { even: true }))
  })
  it('should be invalid when `value` is not odd', function() {
    assert.equal(ERROR_ODD_ID, test('2',                     { odd: true }))
    assert.equal(ERROR_ODD_ID, test('4',                     { odd: true }))
    assert.equal(ERROR_ODD_ID, test('-2',                    { odd: true }))
    assert.equal(ERROR_ODD_ID, test('14.9',                  { odd: true }))
  })
 
  it('should be valid when `value` is a number', function() {
    assert.ok(!test(0))
    assert.ok(!test(1))
    assert.ok(!test('12'))
    assert.ok(!test('12.34'))
  })
  it('should be valid when `value` is an integer', function() {
    assert.ok(!test(1,        { int: true }))
    assert.ok(!test('1',      { int: true }))
    assert.ok(!test('12',     { int: true }))
    assert.ok(!test('-3',     { integer: true }))
  })
  it('should be valid when `value` is = to equalTo', function() {
    assert.ok(!test(1,        { '=': 1 }))
    assert.ok(!test('1',      { '=': 1 }))
    assert.ok(!test('12.345', { '=': 12.345 }))
    assert.ok(!test('0.01',   { 'equalTo': 0.01 }))
  })
  it('should be invalid when `value` is != to otherThan', function() {
    assert.ok(!test(1,        { '!=': 0 }))
    assert.ok(!test('1',      { '!=': 0 }))
    assert.ok(!test('18',     { '!=': 12 }))
    assert.ok(!test('12.345', { '!=': 12.34 }))
    assert.ok(!test('0.01',   { 'otherThan': '12.34' }))
  })
  it('should be valid when `value` is > to greaterThan', function() {
    assert.ok(!test(0.01,     { '>': 0 }))
    assert.ok(!test('1',      { '>': 0 }))
    assert.ok(!test('12.1',   { '>': 12 }))
    assert.ok(!test('12.35',  { 'greaterThan': 12.34 }))
  })
  it('should be valid when `value` is >= to greaterThanOrEqualTo', function() {
    assert.ok(!test(0,        { '>=': 0 }))
    assert.ok(!test('0',      { '>=': 0 }))
    assert.ok(!test('12',     { '>=': 12 }))
    assert.ok(!test('12.34',  { 'greaterThanOrEqualTo': 12.34 }))
  })
  it('should be valid when `value` is < to lessThan', function() {
    assert.ok(!test(-1,       { '<': 0 }))
    assert.ok(!test('-0.1',   { '<': 0 }))
    assert.ok(!test('11',     { '<': 12 }))
    assert.ok(!test('12.33',  { 'lessThan': 12.34 }))
  })
  it('should be valid when `value` is <= to lessThanOrEqualTo', function() {
    assert.ok(!test(0,        { '<=': 0 }))
    assert.ok(!test('-0.01',  { '<=': 0 }))
    assert.ok(!test('11.9',   { '<=': 12 }))
    assert.ok(!test('12.34',  { 'lessThanOrEqualTo': 12.34 }))
  })
  it('should be valid when `value` is even', function() {
    assert.ok(!test('0',      { even: true }))
    assert.ok(!test('2',      { even: true }))
    assert.ok(!test('-2',     { even: true }))
    assert.ok(!test('2.3',    { even: true }))
    assert.ok(!test('-2.3',   { even: true }))
  })
  it('should be valid when `value` is odd', function() {
    assert.ok(!test('1',      { odd: true }))
    assert.ok(!test('-1',     { odd: true }))
    assert.ok(!test('1.2',    { odd: true }))
    assert.ok(!test('-1.2',   { odd: true }))
  })
})