|
| 1 | +describe('date parser', function () { |
| 2 | + var dateParser; |
| 3 | + |
| 4 | + beforeEach(module('ui.bootstrap.dateparser')); |
| 5 | + beforeEach(inject(function (_dateParser_) { |
| 6 | + dateParser = _dateParser_; |
| 7 | + })); |
| 8 | + |
| 9 | + function expectParse(input, format, date) { |
| 10 | + expect(dateParser.parse(input, format)).toEqual(date); |
| 11 | + } |
| 12 | + |
| 13 | + describe('wih custom formats', function() { |
| 14 | + it('should work correctly for `dd`, `MM`, `yyyy`', function() { |
| 15 | + expectParse('17.11.2013', 'dd.MM.yyyy', new Date(2013, 10, 17, 0)); |
| 16 | + expectParse('31.12.2013', 'dd.MM.yyyy', new Date(2013, 11, 31, 0)); |
| 17 | + expectParse('08-03-1991', 'dd-MM-yyyy', new Date(1991, 2, 8, 0)); |
| 18 | + expectParse('03/05/1980', 'MM/dd/yyyy', new Date(1980, 2, 5, 0)); |
| 19 | + expectParse('10.01/1983', 'dd.MM/yyyy', new Date(1983, 0, 10, 0)); |
| 20 | + expectParse('11-09-1980', 'MM-dd-yyyy', new Date(1980, 10, 9, 0)); |
| 21 | + expectParse('2011/02/05', 'yyyy/MM/dd', new Date(2011, 1, 5, 0)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should work correctly for `yy`', function() { |
| 25 | + expectParse('17.11.13', 'dd.MM.yy', new Date(2013, 10, 17, 0)); |
| 26 | + expectParse('02-05-11', 'dd-MM-yy', new Date(2011, 4, 2, 0)); |
| 27 | + expectParse('02/05/80', 'MM/dd/yy', new Date(2080, 1, 5, 0)); |
| 28 | + expectParse('55/02/05', 'yy/MM/dd', new Date(2055, 1, 5, 0)); |
| 29 | + expectParse('11-08-13', 'dd-MM-yy', new Date(2013, 7, 11, 0)); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should work correctly for `M`', function() { |
| 33 | + expectParse('8/11/2013', 'M/dd/yyyy', new Date(2013, 7, 11, 0)); |
| 34 | + expectParse('07.11.05', 'dd.M.yy', new Date(2005, 10, 7, 0)); |
| 35 | + expectParse('02-5-11', 'dd-M-yy', new Date(2011, 4, 2, 0)); |
| 36 | + expectParse('2/05/1980', 'M/dd/yyyy', new Date(1980, 1, 5, 0)); |
| 37 | + expectParse('1955/2/05', 'yyyy/M/dd', new Date(1955, 1, 5, 0)); |
| 38 | + expectParse('02-5-11', 'dd-M-yy', new Date(2011, 4, 2, 0)); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should work correctly for `MMM`', function() { |
| 42 | + expectParse('30.Sep.10', 'dd.MMM.yy', new Date(2010, 8, 30, 0)); |
| 43 | + expectParse('02-May-11', 'dd-MMM-yy', new Date(2011, 4, 2, 0)); |
| 44 | + expectParse('Feb/05/1980', 'MMM/dd/yyyy', new Date(1980, 1, 5, 0)); |
| 45 | + expectParse('1955/Feb/05', 'yyyy/MMM/dd', new Date(1955, 1, 5, 0)); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should work correctly for `MMMM`', function() { |
| 49 | + expectParse('17.November.13', 'dd.MMMM.yy', new Date(2013, 10, 17, 0)); |
| 50 | + expectParse('05-March-1980', 'dd-MMMM-yyyy', new Date(1980, 2, 5, 0)); |
| 51 | + expectParse('February/05/1980', 'MMMM/dd/yyyy', new Date(1980, 1, 5, 0)); |
| 52 | + expectParse('1949/December/20', 'yyyy/MMMM/dd', new Date(1949, 11, 20, 0)); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should work correctly for `d`', function() { |
| 56 | + expectParse('17.November.13', 'd.MMMM.yy', new Date(2013, 10, 17, 0)); |
| 57 | + expectParse('8-March-1991', 'd-MMMM-yyyy', new Date(1991, 2, 8, 0)); |
| 58 | + expectParse('February/5/1980', 'MMMM/d/yyyy', new Date(1980, 1, 5, 0)); |
| 59 | + expectParse('1955/February/5', 'yyyy/MMMM/d', new Date(1955, 1, 5, 0)); |
| 60 | + expectParse('11-08-13', 'd-MM-yy', new Date(2013, 7, 11, 0)); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('wih predefined formats', function() { |
| 65 | + it('should work correctly for `shortDate`', function() { |
| 66 | + expectParse('9/3/10', 'shortDate', new Date(2010, 8, 3, 0)); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should work correctly for `mediumDate`', function() { |
| 70 | + expectParse('Sep 3, 2010', 'mediumDate', new Date(2010, 8, 3, 0)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should work correctly for `longDate`', function() { |
| 74 | + expectParse('September 3, 2010', 'longDate', new Date(2010, 8, 3, 0)); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should work correctly for `fullDate`', function() { |
| 78 | + expectParse('Friday, September 3, 2010', 'fullDate', new Date(2010, 8, 3, 0)); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('with edge case', function() { |
| 83 | + it('should not work for invalid number of days in February', function() { |
| 84 | + expect(dateParser.parse('29.02.2013', 'dd.MM.yyyy')).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should work for 29 days in February for leap years', function() { |
| 88 | + expectParse('29.02.2000', 'dd.MM.yyyy', new Date(2000, 1, 29, 0)); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should not work for 31 days for some months', function() { |
| 92 | + expect(dateParser.parse('31-04-2013', 'dd-MM-yyyy')).toBeUndefined(); |
| 93 | + expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined(); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments