This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
/
validate-helpers-spec.js
1227 lines (1060 loc) · 38 KB
/
validate-helpers-spec.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
describe("validate", function() {
describe('extend', function() {
it("extends the first argument with the remaining arguments", function() {
var obj = {};
validate.extend(obj, {foo: "foo"}, {bar: "bar"});
expect(obj).toEqual({foo: "foo", bar: "bar"});
});
it("returns the first argument", function() {
var obj = {};
expect(validate.extend(obj)).toBe(obj);
});
it("extends with the seconds argument first", function() {
var actual = validate.extend({}, {foo: "foo"}, {foo: "bar"});
expect(actual).toEqual({foo: "bar"});
});
});
describe('result', function() {
it("returns the first argument if it's not a function", function() {
var obj = {};
expect(validate.result(obj)).toBe(obj);
});
it("calls the argument if it's a function and returns the result", function() {
var obj = jasmine.createSpy().and.returnValue("some return value");
expect(validate.result(obj)).toEqual("some return value");
});
it("accepts additional arguments as arguments to the function", function() {
var obj = jasmine.createSpy();
validate.result(obj, "foo", "bar", "baz");
expect(obj).toHaveBeenCalledWith("foo", "bar", "baz");
});
});
describe('isNumber', function() {
it("returns true for numbers", function() {
expect(validate.isNumber(0)).toBe(true);
expect(validate.isNumber(1)).toBe(true);
expect(validate.isNumber(Math.PI)).toBe(true);
});
it("returns false for non numbers", function() {
expect(validate.isNumber(null)).toBe(false);
expect(validate.isNumber(true)).toBe(false);
expect(validate.isNumber("1")).toBe(false);
});
});
describe('isInteger', function() {
it("returns true for integers", function() {
expect(validate.isInteger(0)).toBe(true);
expect(validate.isInteger(1)).toBe(true);
});
it("returns false for floats and other types ", function() {
expect(validate.isInteger(Math.PI)).toBe(false);
expect(validate.isInteger(null)).toBe(false);
expect(validate.isInteger("1")).toBe(false);
});
});
describe('isBoolean', function() {
it("returns true for booleans", function() {
expect(validate.isBoolean(true)).toBe(true);
expect(validate.isBoolean(false)).toBe(true);
});
it("returns false for non booleans ", function() {
expect(validate.isBoolean(null)).toBe(false);
expect(validate.isBoolean({})).toBe(false);
expect(validate.isBoolean({foo: "bar"})).toBe(false);
expect(validate.isBoolean([])).toBe(false);
expect(validate.isBoolean("")).toBe(false);
expect(validate.isBoolean(function() {})).toBe(false);
});
});
describe('isObject', function() {
it("returns true for objects", function() {
expect(validate.isObject({})).toBe(true);
expect(validate.isObject({foo: "bar"})).toBe(true);
expect(validate.isObject([])).toBe(true);
expect(validate.isObject(function() {})).toBe(true);
});
it("returns false for non objects", function() {
expect(validate.isObject(null)).toBe(false);
expect(validate.isObject(1)).toBe(false);
expect(validate.isObject("")).toBe(false);
expect(validate.isObject(false)).toBe(false);
});
});
describe('isDefined', function() {
it("returns false for null and undefined", function() {
expect(validate.isDefined(null)).toBe(false);
expect(validate.isDefined(undefined)).toBe(false);
});
it("returns true for other values", function() {
expect(validate.isDefined(true)).toBe(true);
expect(validate.isDefined(0)).toBe(true);
expect(validate.isDefined("")).toBe(true);
});
});
describe("isPromise", function() {
it("returns false for null and undefined", function() {
expect(validate.isPromise(null)).toBe(false);
expect(validate.isPromise(undefined)).toBe(false);
});
it("returns false for objects", function() {
expect(validate.isPromise({})).toBe(false);
});
it("returns true for objects with a then function", function() {
expect(validate.isPromise({then: "that"})).toBe(false);
expect(validate.isPromise({then: function() {}})).toBe(true);
});
});
describe('format', function() {
it("replaces %{...} with the correct value", function() {
var actual = validate.format("Foo is %{foo}, bar is %{bar}", {
foo: "foo",
bar: "bar"
});
expect(actual).toEqual("Foo is foo, bar is bar");
});
it("can replace the same value multiple times", function() {
var actual = validate.format("%{foo} %{foo}", {foo: "foo"});
expect(actual).toEqual("foo foo");
});
it("supports escaping %", function() {
var actual = validate.format("Foo is %%{foo}", {foo: "foo"});
expect(actual).toEqual("Foo is %{foo}");
});
it("handles non strings as the message", function() {
var obj = {foo: "bar"};
expect(validate.format(obj, {attr: "value"})).toBe(obj);
});
});
describe("stringifyValue", function() {
it("simply calls validate.prettify", function() {
spyOn(validate, "prettify").and.returnValue("foobar");
expect(validate.stringifyValue("barfoo")).toEqual("foobar");
expect(validate.prettify).toHaveBeenCalledWith("barfoo");
});
it("calls custom prettify from options", function() {
var options = {prettify: function() {}};
spyOn(options, "prettify").and.returnValue("foobar");
spyOn(validate, "prettify").and.returnValue("baz");
expect(validate.stringifyValue("barfoo", options)).toEqual("foobar");
expect(options.prettify).toHaveBeenCalledWith("barfoo");
expect(validate.prettify).not.toHaveBeenCalled();
});
});
describe('prettify', function() {
it("lower cases the entire string", function() {
expect(validate.prettify("FOO BAR")).toEqual("foo bar");
});
it("replaces underscores with spaces", function() {
expect(validate.prettify("foo_bar_baz")).toEqual("foo bar baz");
});
it("replaces dashes with spaces", function() {
expect(validate.prettify("foo-bar-baz")).toEqual("foo bar baz");
});
it("splits camel cased words", function() {
expect(validate.prettify("fooBar")).toEqual("foo bar");
});
it("replaces periods with spaces if no space follows", function() {
expect(validate.prettify("foo.bar.baz")).toEqual("foo bar baz");
expect(validate.prettify("foo. bar")).toEqual("foo. bar");
expect(validate.prettify("foo .bar")).toEqual("foo .bar");
expect(validate.prettify("foo.bar.")).toEqual("foo bar.");
});
it("replaces backslashes with nothing", function() {
expect(validate.prettify("foo\\.bar\\.baz")).toEqual("foo bar baz");
expect(validate.prettify("foo\\\\.bar")).toEqual("foo bar");
});
it("calls toString on objects", function() {
var object = {
toString: function() { return "Custom string"; }
};
expect(validate.prettify(object)).toEqual("Custom string");
});
it("tries to stringify objects that don't implement toString", function() {
var object = Object.create(null);
object.name = "Foo Bar";
expect(validate.prettify(object)).toEqual("{\"name\":\"Foo Bar\"}");
});
it("throws an error when trying to parse cyclical structures", function() {
var object = Object.create(null);
object.name = "Foo Bar";
object.cycle = object;
expect(function () {
return validate.prettify(object);
}).toThrow();
});
it("doesn't allow too many decimals", function() {
expect(validate.prettify(4711)).toEqual("4711");
expect(validate.prettify(4711.2)).toEqual("4711.2");
expect(validate.prettify(4711.255555)).toEqual("4711.26");
});
it("handles arrays", function() {
var array = ["foo", "bar_baz"];
// It valls it recusively
spyOn(validate, "prettify").and.callThrough();
expect(validate.prettify(array)).toEqual("foo, bar baz");
expect(validate.prettify).toHaveBeenCalledWith(array);
expect(validate.prettify).toHaveBeenCalledWith("foo");
expect(validate.prettify).toHaveBeenCalledWith("bar_baz");
});
});
describe('isString', function() {
it("returns true for strings", function() {
expect(validate.isString("foobar")).toBe(true);
expect(validate.isString("")).toBe(true);
});
it("returns false for non strings", function() {
var obj = {toString: function() { return "foobar"; }};
expect(validate.isString(obj)).toBe(false);
expect(validate.isString(null)).toBe(false);
expect(validate.isString(true)).toBe(false);
});
});
describe('isArray', function() {
var isArray = validate.isArray;
it("returns true for arrays", function() {
expect(isArray([])).toBe(true);
expect(isArray([1])).toBe(true);
expect(isArray([1, 2])).toBe(true);
});
it("returns false for non arrays", function() {
expect(isArray({})).toBe(false);
expect(isArray(null)).toBe(false);
expect(isArray(1)).toBe(false);
expect(isArray(true)).toBe(false);
});
});
describe('isHash', function() {
it("returns true for hashes", function() {
expect(validate.isHash({})).toBe(true);
expect(validate.isHash({foo: "bar"})).toBe(true);
});
it("returns false for non hashes", function() {
expect(validate.isHash([])).toBe(false);
expect(validate.isHash(function() {})).toBe(false);
expect(validate.isHash(null)).toBe(false);
expect(validate.isHash(1)).toBe(false);
expect(validate.isHash("")).toBe(false);
expect(validate.isHash(false)).toBe(false);
});
});
describe('contains', function() {
var contains = validate.contains;
it("returns false when not passing in a target object", function() {
expect(contains(null, "foo")).toBe(false);
expect(contains(undefined, "foo")).toBe(false);
});
describe("arrays", function() {
it("returns true if the value is in the specified array", function() {
expect(contains(["foo", "bar", "baz"], "foo")).toBe(true);
expect(contains(["foo", "bar", "baz"], "bar")).toBe(true);
expect(contains(["foo", "bar", "baz"], "baz")).toBe(true);
});
it("returns false if the value is not in the specified array", function() {
expect(contains(["foo", "bar", "baz"], "quux")).toBe(false);
expect(contains(["foo", "bar", "baz"], false)).toBe(false);
expect(contains(["foo", "bar", "baz"], 0)).toBe(false);
expect(contains(["foo", "bar", "baz"], null)).toBe(false);
});
it("works with empty arrays", function() {
expect(contains([], "foo")).toBe(false);
});
});
describe("objects", function() {
it("returns true if the value is a key in the object", function() {
expect(contains({foo: false, bar: "bar"}, "foo")).toBe(true);
expect(contains({foo: false, bar: "bar"}, "bar")).toBe(true);
});
it("returns false if the value is not a key in the object", function() {
expect(contains({foo: false, bar: "bar"}, "quux")).toBe(false);
expect(contains({foo: false, bar: "bar"}, null)).toBe(false);
expect(contains({foo: false, bar: "bar"}, 1)).toBe(false);
expect(contains({foo: false, bar: "bar"}, true)).toBe(false);
});
it("works with empty objects", function() {
expect(contains({}, "foo")).toBe(false);
});
});
});
describe('capitalize', function() {
var capitalize = validate.capitalize;
it("captializes the first word", function() {
expect(capitalize("foo")).toEqual("Foo");
expect(capitalize("foo bar")).toEqual("Foo bar");
expect(capitalize("foo bar baz")).toEqual("Foo bar baz");
});
it("returns the value for non strings", function() {
var o = {foo: "bar"};
expect(capitalize(o)).toEqual(o);
});
});
describe("pruneEmptyErrors", function() {
it("removes empty errors", function() {
var input = [{
attribute: "name",
value: "test",
validator: "fail",
options: {"someOption": "someValue"},
error: "foobar"
}, {
attribute: "name",
value: "test",
validator: "fail2",
options: true,
error: ["foo", "bar"]
}, {
attribute: "name",
value: "test",
validator: "pass",
options: true,
error: null
}, {
attribute: "name",
value: "test",
validator: "pass",
options: true,
error: []
}, {
attribute: "name",
value: "test",
validator: "pass",
options: true,
error: ""
}];
expect(validate.pruneEmptyErrors(input)).toEqual([{
attribute: "name",
value: "test",
validator: "fail",
options: {"someOption": "someValue"},
error: "foobar"
}, {
attribute: "name",
value: "test",
validator: "fail2",
options: true,
error: ["foo", "bar"]
}]);
});
});
describe("expandMultipleErrors", function() {
it("expands error arrays to multiple entries", function() {
var input = [{
attribute: "name",
value: "test",
validator: "fail",
options: {"someOption": "someValue"},
error: "foobar"
}, {
attribute: "name",
value: "test",
validator: "fail2",
options: true,
error: ["foo", "bar"]
}];
expect(validate.expandMultipleErrors(input)).toEqual([{
attribute: "name",
value: "test",
validator: "fail",
options: {"someOption": "someValue"},
error: "foobar"
}, {
attribute: "name",
value: "test",
validator: "fail2",
options: true,
error: "foo"
}, {
attribute: "name",
value: "test",
validator: "fail2",
options: true,
error: "bar"
}]);
});
});
describe('convertErrorMessages', function() {
var convertErrorMessages = validate.convertErrorMessages;
it("prettifies and prepends the attribute", function() {
var errors = [{
attribute: "foo",
error: "can't be blank",
someOtherProperty: "someOtherProperty",
value: "foobar"
}, {
attribute: "foo_bar",
error: "has some other problem",
value: "foobar"
}];
expect(convertErrorMessages(errors)).toEqual([{
attribute: "foo",
error: "Foo can't be blank",
someOtherProperty: "someOtherProperty",
value: "foobar"
}, {
attribute: "foo_bar",
error: "Foo bar has some other problem",
value: "foobar"
}]);
});
it("calls custom prettify from options", function() {
var errors = [{
attribute: "foo",
error: "can't be blank"
}]
, options = {prettify: function() {}};
spyOn(options, "prettify").and.returnValue("foobar");
spyOn(validate, "prettify").and.returnValue("baz");
expect(convertErrorMessages(errors, options)[0].error).toEqual("Foobar can't be blank");
expect(options.prettify).toHaveBeenCalledWith("foo");
expect(validate.prettify).not.toHaveBeenCalled();
});
it("doesn't modify the input", function() {
var errors = [{
attribute: "foo",
error: "can't be blank",
value: "foobar"
}];
convertErrorMessages(errors);
expect(errors).toEqual([{
attribute: "foo",
error: "can't be blank",
value: "foobar"
}]);
});
it("returns an empty object if there are no errors", function() {
expect(convertErrorMessages([])).toEqual([]);
});
it("doesn't prepend the attribute name if the message starts with a ^", function() {
var errors = [{
attribute: "foo",
error: "^Please don't do that",
value: "foobar"
}];
expect(convertErrorMessages(errors)).toEqual([{
attribute: "foo",
error: "Please don't do that",
value: "foobar"
}]);
});
it("handles an escaped ^", function() {
var errors = [{
attribute: "foo",
error: "\\^ has a weird message^\\^",
value: "foobar"
}];
expect(convertErrorMessages(errors)).toEqual([{
attribute: "foo",
error: "Foo ^ has a weird message^^",
value: "foobar"
}]);
});
it("doesn't prepend the attribute name if fullMessages is false", function() {
var errors = [{
attribute: "foo",
error: "Please don't do that",
value: "foobar"
}];
expect(convertErrorMessages(errors, {fullMessages: false})).toEqual([{
attribute: "foo",
error: "Please don't do that",
value: "foobar"
}]);
});
it("still strips the leading ^ even if fullmessages if false", function() {
var errors = [{
attribute: "foo",
error: "\\^ has a weird message^\\^",
value: "foobar"
}];
expect(convertErrorMessages(errors, {fullMessages: false})).toEqual([{
attribute: "foo",
error: "^ has a weird message^^",
value: "foobar"
}]);
});
it("allow messages to contain %{value}", function() {
var errors = [{
attribute: "foo",
error: "foo %{value}",
someOtherProperty: "someOtherProperty",
value: "foobar"
}];
spyOn(validate, "format").and.callThrough();
spyOn(validate, "stringifyValue").and.returnValue("barfoo");
expect(convertErrorMessages(errors)).toEqual([{
attribute: "foo",
error: "Foo foo barfoo",
someOtherProperty: "someOtherProperty",
value: "foobar"
}]);
expect(validate.format).toHaveBeenCalledWith("Foo foo %{value}", {
value: "barfoo"
});
});
});
describe("groupErrorsByAttribute", function() {
it("groups errors by attribute", function() {
var input = [{
attribute: "foo",
someKey: "someValue"
}, {
attribute: "bar",
someOtherKey: "someOtherValue"
}, {
attribute: "foo",
someThirdKey: "someThirdValue"
}];
expect(validate.groupErrorsByAttribute(input)).toEqual({
foo: [{
attribute: "foo",
someKey: "someValue"
}, {
attribute: "foo",
someThirdKey: "someThirdValue"
}],
bar: [{
attribute: "bar",
someOtherKey: "someOtherValue"
}]
});
});
});
describe("processValidationResults", function() {
var pvr = validate.processValidationResults;
it("allows the validator to return a string", function() {
var results = [{attribute: "name", error: "foobar"}];
expect(pvr(results, {})).toEqual({name: ["Name foobar"]});
});
it("allows the validator to return an array", function() {
var results = [{attribute: "name", error: ["foo", "bar"]}];
expect(pvr(results, {})).toEqual({name: ["Name foo", "Name bar"]});
});
it("supports multiple entries for the same attribute", function() {
var results = [
{attribute: "name", error: ["foo", "bar"]},
{attribute: "name", error: "baz"}
];
expect(pvr(results, {})).toEqual({
name: ["Name foo", "Name bar", "Name baz"]
});
});
it("the correct functions", function() {
spyOn(validate, "pruneEmptyErrors").and.returnValue("pruned");
spyOn(validate, "expandMultipleErrors").and.returnValue("expanded");
spyOn(validate, "convertErrorMessages").and.returnValue([]);
var options = {option: "value"};
expect(pvr("input", options)).toBe(undefined);
expect(validate.pruneEmptyErrors)
.toHaveBeenCalledWith("input", options);
expect(validate.expandMultipleErrors)
.toHaveBeenCalledWith("pruned", options);
expect(validate.convertErrorMessages)
.toHaveBeenCalledWith("expanded", options);
});
it("throws an error for unknown formats", function() {
expect(function() {
pvr([], {format: "foobar"});
}).toThrow(new Error("Unknown format foobar"));
});
});
describe("flattenErrorsToArray", function() {
it("flattens an array of errors objects to just the messages", function() {
var input = [{
error: "error 1",
someKey: "someValue"
}, {
error: "error 2"
}];
expect(validate.flattenErrorsToArray(input)).toEqual([
"error 1",
"error 2"
]);
});
});
describe('isFunction', function() {
var isFunction = validate.isFunction;
it("returns true for functions", function() {
expect(isFunction(function() {})).toBe(true);
});
it("returns false for non functions", function() {
expect(isFunction({})).toBe(false);
expect(isFunction(null)).toBe(false);
expect(isFunction(1)).toBe(false);
expect(isFunction(true)).toBe(false);
});
});
describe('exposeModule', function() {
var exposeModule = validate.exposeModule;
it("supports simple browser inclusion", function() {
var root = {};
exposeModule(validate, root, null, null, null);
expect(root.validate).toBe(validate);
});
it("supports AMD", function() {
var root = {}
, define = function(deps, func) {
expect(deps).toEqual([]);
expect(func()).toBe(validate);
};
var defineSpy = jasmine.createSpy('define').and.callFake(define);
exposeModule(validate, root, null, null, defineSpy);
expect(defineSpy).not.toHaveBeenCalled();
defineSpy.amd = true;
exposeModule(validate, root, null, null, defineSpy);
expect(defineSpy).toHaveBeenCalled();
// It should still expose it through the root
expect(root.validate).toBe(validate);
});
it("supports exports", function() {
var root = {}
, exports = {};
exposeModule(validate, root, exports, null, null);
expect(root).toEqual({});
expect(exports.validate).toBe(validate);
});
it("supports module.exports", function() {
var root = {}
, exports = {}
, module = {exports: true};
exposeModule(validate, root, exports, module, null);
expect(root).toEqual({});
expect(module.exports).toEqual(validate);
expect(module.exports.validate).toEqual(validate);
});
});
describe("warn", function() {
var console = window.console;
beforeEach(function() {
window.console = undefined;
});
afterEach(function() {
window.console = console;
});
it("does nothing if the console isn't defined", function() {
expect(function() { validate.warn("Msg"); }).not.toThrow();
});
it("calls console.warn if defined", function() {
window.console = {
warn: jasmine.createSpy("warn")
};
validate.warn("Msg");
expect(window.console.warn).toHaveBeenCalledWith("[validate.js] Msg");
});
});
describe("error", function() {
var console = window.console;
beforeEach(function() { window.console = undefined; });
afterEach(function() { window.console = console; });
it("does nothing if the console isn't defined", function() {
validate.error("Msg");
expect(function() { validate.error("Msg"); }).not.toThrow();
});
it("calls console.error if defined", function() {
window.console = {
error: jasmine.createSpy("error")
};
validate.error("Msg");
expect(window.console.error).toHaveBeenCalledWith("[validate.js] Msg");
});
});
describe("getDeepObjectValue", function() {
it("supports multiple keys separated using a period", function() {
var attributes = {
foo: {
bar: {
baz: 3
}
}
};
expect(validate.getDeepObjectValue(attributes, "foo.bar.baz")).toBe(3);
});
it("returns undefined if any key is not found", function() {
var attributes = {
foo: {
bar: {
baz: 3
}
}
};
expect(validate.getDeepObjectValue(attributes, "bar.foo")).toBe(undefined);
expect(validate.getDeepObjectValue(attributes, "foo.baz")).toBe(undefined);
});
it("handles the object being non objects", function() {
expect(validate.getDeepObjectValue(null, "foo")).toBe(undefined);
expect(validate.getDeepObjectValue("foo", "foo")).toBe(undefined);
expect(validate.getDeepObjectValue(3, "foo")).toBe(undefined);
expect(validate.getDeepObjectValue([], "foo")).toBe(undefined);
expect(validate.getDeepObjectValue(true, "foo")).toBe(undefined);
});
it("handles the keypath being non strings", function() {
expect(validate.getDeepObjectValue({}, null)).toBe(undefined);
expect(validate.getDeepObjectValue({}, 3)).toBe(undefined);
expect(validate.getDeepObjectValue({}, {})).toBe(undefined);
expect(validate.getDeepObjectValue({}, [])).toBe(undefined);
expect(validate.getDeepObjectValue({}, true)).toBe(undefined);
});
it("handles escapes properly", function() {
var attributes = {
"foo.bar": {
baz: 3
},
"foo\\": {
bar: {
baz: 5
}
}
};
expect(validate.getDeepObjectValue(attributes, "foo.bar.baz"))
.toBe(undefined);
expect(validate.getDeepObjectValue(attributes, "foo\\.bar.baz"))
.toBe(3);
expect(validate.getDeepObjectValue(attributes, "foo\\\\.bar.baz"))
.toBe(5);
expect(validate.getDeepObjectValue(attributes, "\\foo\\\\.bar.baz"))
.toBe(5);
});
});
describe("isDate", function() {
it("returns true for dates", function() {
expect(validate.isDate(new Date())).toBe(true);
});
it("returns false for non dates", function() {
expect(validate.isDate(Date.now())).toBe(false);
expect(validate.isDate({})).toBe(false);
});
});
describe("isEmpty", function() {
it("considers null and undefined values empty", function() {
expect(validate.isEmpty(null)).toBe(true);
expect(validate.isEmpty(undefined)).toBe(true);
});
it("considers functions non empty", function() {
expect(validate.isEmpty(function(){})).toBe(false);
});
it("considers whitespace only strings empty", function() {
expect(validate.isEmpty("")).toBe(true);
expect(validate.isEmpty(" ")).toBe(true);
expect(validate.isEmpty(" ")).toBe(true);
expect(validate.isEmpty("foo")).toBe(false);
});
it("considers empty arrays empty", function() {
expect(validate.isEmpty([])).toBe(true);
expect(validate.isEmpty([1])).toBe(false);
});
it("considers empty objects empty", function() {
expect(validate.isEmpty({})).toBe(true);
expect(validate.isEmpty({foo: "bar"})).toBe(false);
});
it("considers false and 0 non empty", function() {
expect(validate.isEmpty(false)).toBe(false);
expect(validate.isEmpty(0)).toBe(false);
});
it("considers date non empty", function() {
spyOn(validate, "isDate").and.callThrough();
expect(validate.isEmpty(new Date())).toBe(false);
expect(validate.isDate).toHaveBeenCalled();
});
});
describe("collectFormValues", function() {
it("handles empty input", function() {
expect(validate.collectFormValues()).toEqual({});
});
it("handles simple forms", function() {
var form = document.createElement("form");
form.innerHTML = '' +
'<input type="text" name="text" value="example text">' +
'<input type="text" name="empty-text">' +
'<input type="email" name="email" value="example@email.com">' +
'<input type="password" name="password" value="password!">' +
'<input type="checkbox" name="selected-checkbox" value="checkbox" checked>' +
'<input type="checkbox" name="deselected-checkbox" value="checkbox">' +
'<input type="date" name="date" value="2015-03-08">' +
'<input type="hidden" name="hidden" value="hidden">' +
'<input type="number" name="number" value="4711">' +
'<input type="url" name="url" value="http://validatejs.org">' +
'<input type="radio" name="single-checked-radio" value="radio" checked>' +
'<input type="radio" name="single-unchecked-radio" value="radio">' +
'<radiogroup>' +
' <input type="radio" name="checked-radio" value="radio1">' +
' <input type="radio" name="checked-radio" value="radio2" checked>' +
' <input type="radio" name="checked-radio" value="radio3">' +
'</radiogroup>' +
'<radiogroup>' +
' <input type="radio" name="unchecked-radio" value="radio1">' +
' <input type="radio" name="unchecked-radio" value="radio2">' +
' <input type="radio" name="unchecked-radio" value="radio3">' +
'</radiogroup>' +
'<select name="selected-dropdown">' +
' <option>' +
' <option value="option1">' +
' <option value="option2" selected>' +
'</select>' +
'<select name="unselected-dropdown">' +
' <option>' +
' <option value="option1">' +
' <option value="option2">' +
'</select>' +
'<textarea name="textarea-ignored" data-ignored>the textarea</textarea>'+
'<textarea name="textarea">the textarea</textarea>';
expect(validate.collectFormValues(form)).toEqual({
text: "example text",
"empty-text": null,
email: "example@email.com",
password: "password!",
"selected-checkbox": "checkbox",
"deselected-checkbox": null,
date: "2015-03-08",
hidden: "hidden",
number: 4711,
url: "http://validatejs.org",
"single-checked-radio": "radio",
"single-unchecked-radio": null,
"checked-radio": "radio2",
"unchecked-radio": null,
"selected-dropdown": "option2",
"unselected-dropdown": null,
"textarea": "the textarea"
});
});
it("has an option to nullify empty and trim strings", function() {
var form = document.createElement("form");
form.innerHTML = '' +
'<input type="text" name="normal" value="normal">' +
'<input type="text" name="empty">' +
'<input type="text" name="whitespace" value=" ">' +
'<input type="text" name="trimmed" value=" foo ">';
var options = {nullify: false};
expect(validate.collectFormValues(form, options)).toEqual({
normal: "normal",
empty: "",
whitespace: " ",
trimmed: " foo "
});
options = {nullify: true};
expect(validate.collectFormValues(form, options)).toEqual({
normal: "normal",
empty: null,
whitespace: " ",
trimmed: " foo "
});
options = {trim: true};
expect(validate.collectFormValues(form, options)).toEqual({
normal: "normal",
empty: null,
whitespace: null,
trimmed: "foo"
});
});
it("has a way to ignore elements", function() {
var form = document.createElement("form");
form.innerHTML = '' +
'<input type="text" name="ignored" value="ignored" data-ignored>' +
'<select name="ignored-select" data-ignored>' +
' <option value="select" selected>Select</option>' +
'</select>';
expect(validate.collectFormValues(form)).toEqual({});
});
it("uses true/false for checkboxes without a value", function() {
var form = document.createElement("form");
form.innerHTML = '' +
'<input type="checkbox" name="checked" checked>' +