|
| 1 | +describe('validators.each', function() { |
| 2 | + var each = validate.validators.each.bind(validate.validators.each); |
| 3 | + |
| 4 | + var isPositive = function(number) { |
| 5 | + if (number > 0) { |
| 6 | + return undefined; |
| 7 | + } else { |
| 8 | + return 'negative'; |
| 9 | + } |
| 10 | + }; |
| 11 | + |
| 12 | + afterEach(function() { |
| 13 | + delete validate.validators.each.message; |
| 14 | + delete validate.validators.each.options; |
| 15 | + }); |
| 16 | + |
| 17 | + it("allows undefined values", function() { |
| 18 | + expect(each(null, {})).not.toBeDefined(); |
| 19 | + expect(each(undefined, {})).not.toBeDefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("does not allow values that aren't arrays", function() { |
| 23 | + expect(each({}, {})).toBeDefined(); |
| 24 | + expect(each(function () {}, {})).toBeDefined(); |
| 25 | + expect(each("", {})).toBeDefined(); |
| 26 | + expect(each(1, {})).toBeDefined(); |
| 27 | + expect(each(true, {})).toBeDefined(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("has a default error message", function() { |
| 31 | + expect(each({}, {})).toEqual("must be an array"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("allows for a message to be attached to the validator", function() { |
| 35 | + var validatorMessage = "validatorMessage"; |
| 36 | + validate.validators.each.message = validatorMessage; |
| 37 | + expect(each({}, {})).toEqual(validatorMessage); |
| 38 | + }); |
| 39 | + |
| 40 | + it("allows for a message to be passed as an option to override ", function() { |
| 41 | + var optionMessage = "optionMessage"; |
| 42 | + validate.validators.each.message = "validatorMessage"; |
| 43 | + expect(each({}, {message : optionMessage})).toEqual(optionMessage); |
| 44 | + }); |
| 45 | + |
| 46 | + it("accepts the value if no validator function is provided", function () { |
| 47 | + expect(each([], {})).not.toBeDefined(); |
| 48 | + expect(each([], {validator : {}})).not.toBeDefined(); |
| 49 | + expect(each([], {validator : ""})).not.toBeDefined(); |
| 50 | + expect(each([], {validator : 1})).not.toBeDefined(); |
| 51 | + expect(each([], {validator : []})).not.toBeDefined(); |
| 52 | + expect(each([], {validator : true})).not.toBeDefined(); |
| 53 | + expect(each([], {validator : null})).not.toBeDefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("accepts an empty array", function () { |
| 57 | + expect(each([], {validator : function () {}})).not.toBeDefined(); |
| 58 | + expect(each([], {validator : function () {return 'error';}})).not.toBeDefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("accepts a valid array", function () { |
| 62 | + var array = [1, 2, 3, 4, 5]; |
| 63 | + expect(each(array, {validator : isPositive})).not.toBeDefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns an array of errors if anything fails", function () { |
| 67 | + var array = [-1, 2, 3, 4, 5]; |
| 68 | + expect(each(array, {validator : isPositive})).toEqual(['negative', undefined, undefined, undefined, undefined]); |
| 69 | + }); |
| 70 | + |
| 71 | +}); |
0 commit comments