UNPKG

2.56 kBJavaScriptView Raw
1if (typeof require !== 'undefined') {
2 var Validator = require('../src/validator.js');
3 var expect = require('chai').expect;
4} else {
5 var Validator = window.Validator;
6 var expect = window.chai.expect;
7}
8
9describe('in validation rule', function() {
10 it('should fail when the value is not in the set of comma separated values', function() {
11 var validator = new Validator({
12 state: 'fakeState',
13 }, {
14 state: 'in:CA,TX,FL'
15 });
16 expect(validator.passes()).to.be.false;
17 expect(validator.fails()).to.be.true;
18 expect(validator.errors.first('state')).to.equal('The selected state is invalid.');
19 });
20
21 it('should pass when the value is in the set of comma separated values', function() {
22 var validator = new Validator({
23 state: 'CA'
24 }, {
25 state: 'in:CA,TX,FL'
26 });
27 expect(validator.passes()).to.be.true;
28 expect(validator.fails()).to.be.false;
29 });
30
31 it('should pass when the value is undefined', function() {
32 var validator = new Validator({}, {
33 state: 'in:CA,TX,FL'
34 });
35 expect(validator.passes()).to.be.true;
36 expect(validator.fails()).to.be.false;
37 });
38
39 it('should pass when the value is an empty string', function() {
40 var validator = new Validator({
41 state: ''
42 }, {
43 state: 'in:CA,TX,FL'
44 });
45 expect(validator.passes()).to.be.true;
46 expect(validator.fails()).to.be.false;
47 });
48
49 it('should fail when the numeric value is not in the set of comma separated values', function() {
50 var validator = new Validator({
51 quantity: 10
52 }, {
53 quantity: 'in:0,1,2'
54 });
55 expect(validator.passes()).to.be.false;
56 expect(validator.fails()).to.be.true;
57 expect(validator.errors.first('quantity')).to.equal('The selected quantity is invalid.');
58 });
59
60 it('should pass when the value is in the set of comma separated values', function() {
61 var validator = new Validator({
62 quantity: 1
63 }, {
64 quantity: 'in:0,1,2'
65 });
66 expect(validator.passes()).to.be.true;
67 expect(validator.fails()).to.be.false;
68 });
69
70 it('should pass when all values are present', function() {
71 var validator = new Validator({
72 fruits: ['apple', 'strawberry']
73 }, {
74 fruits: 'array|in:apple,strawberry,kiwi'
75 });
76
77 expect(validator.passes()).to.be.true;
78 });
79
80 it('should fail when not all values are present', function() {
81 var validator = new Validator({
82 fruits: ['strawberry', 'kiwi']
83 }, {
84 fruits: 'array|in:apple,strawberry'
85 });
86
87 expect(validator.passes()).to.be.false;
88 });
89});