|
| 1 | +import parse from 'date-fns/parse'; |
| 2 | +import startOfDay from 'date-fns/start_of_day'; |
| 3 | +import localeEn from '../locales/en-US'; |
| 4 | +import { |
| 5 | + formatDate, |
| 6 | + formatSelectedDate, |
| 7 | + getToday, |
| 8 | + isSelectable, |
| 9 | + moveElementsByN, |
| 10 | + omit, |
| 11 | + pick, |
| 12 | +} from '../utils'; |
| 13 | + |
| 14 | +const objectTest = { a: 'a', b: 'b', c: 'c' }; |
| 15 | +const dateTest = parse('2018-06-21'); |
| 16 | +const june14 = parse('2018-06-14'); |
| 17 | +const june20 = parse('2018-06-20'); |
| 18 | +const june25 = parse('2018-06-25'); |
| 19 | +const june28 = parse('2018-06-28'); |
| 20 | + |
| 21 | +describe('moveElementsByN', () => { |
| 22 | + it('should return the same array if `n` is zero', () => { |
| 23 | + expect(moveElementsByN(0, localeEn.weekdays)).toEqual(localeEn.weekdays); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return the correct array if `n` is different than zero', () => { |
| 27 | + expect(moveElementsByN(3, localeEn.weekdays)).toEqual([ |
| 28 | + 'Wednesday', |
| 29 | + 'Thursday', |
| 30 | + 'Friday', |
| 31 | + 'Saturday', |
| 32 | + 'Sunday', |
| 33 | + 'Monday', |
| 34 | + 'Tuesday', |
| 35 | + ]); |
| 36 | + |
| 37 | + expect(moveElementsByN(5, localeEn.weekdays)).toEqual([ |
| 38 | + 'Friday', |
| 39 | + 'Saturday', |
| 40 | + 'Sunday', |
| 41 | + 'Monday', |
| 42 | + 'Tuesday', |
| 43 | + 'Wednesday', |
| 44 | + 'Thursday', |
| 45 | + ]); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('omit', () => { |
| 50 | + it('should return the same object if the array of keys is empty', () => { |
| 51 | + expect(omit([], objectTest)).toEqual(objectTest); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return a new object without the given keys', () => { |
| 55 | + expect(omit(['a', 'b'], objectTest)).toEqual({ c: 'c' }); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('pick', () => { |
| 60 | + it('should return an empty object if the array of keys is empty', () => { |
| 61 | + expect(pick([], objectTest)).toEqual({}); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return a new object with the given keys', () => { |
| 65 | + expect(pick(['a', 'b'], objectTest)).toEqual({ a: 'a', b: 'b' }); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('formatDate', () => { |
| 70 | + it('should return `undefined` if an invalid date is given', () => { |
| 71 | + expect(formatDate(null, 'DD/MM/YYYY')).toBe(undefined); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should format correctly if a valid date is given', () => { |
| 75 | + expect(formatDate(dateTest, 'DD/MM/YYYY')).toBe('21/06/2018'); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('isSelectable', () => { |
| 80 | + it('should return true if the first date is in the informed range', () => { |
| 81 | + expect(isSelectable(dateTest, june14, june28)).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return false if the first date is not in the informed range', () => { |
| 85 | + expect(isSelectable(dateTest, june14, june20)).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return true if the first date is after the minDate', () => { |
| 89 | + expect(isSelectable(dateTest, june14, null)).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return false if the first date is after the minDate', () => { |
| 93 | + expect(isSelectable(dateTest, june25, null)).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return true if the first date is before the maxDate', () => { |
| 97 | + expect(isSelectable(dateTest, null, june25)).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return false if the first date is before the maxDate', () => { |
| 101 | + expect(isSelectable(dateTest, null, june14)).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return true if we only provide one date to the function', () => { |
| 105 | + expect(isSelectable(dateTest)).toBe(true); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('getToday', () => { |
| 110 | + const today = startOfDay(new Date()); |
| 111 | + |
| 112 | + it('should return the correct result if `today` is not selectable', () => { |
| 113 | + expect(getToday(june14, june28)).toEqual({ |
| 114 | + date: today, |
| 115 | + selectable: false, |
| 116 | + selected: false, |
| 117 | + today: true, |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return the correct result if `today` is selectable', () => { |
| 122 | + expect(getToday(june14)).toEqual({ |
| 123 | + date: today, |
| 124 | + selectable: true, |
| 125 | + selected: false, |
| 126 | + today: true, |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('formatSelectedDate', () => { |
| 132 | + it('should return an empty string if an invalid date is given', () => { |
| 133 | + expect(formatSelectedDate(null, 'DD/MM/YYYY')).toBe(''); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return the correct result if a valid date is given', () => { |
| 137 | + expect(formatSelectedDate(june14, 'DD/MM/YYYY')).toBe('14/06/2018'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return the correct result if a valid array of dates is given', () => { |
| 141 | + expect(formatSelectedDate([june14, june20], 'DD/MM/YYYY')).toBe( |
| 142 | + '14/06/2018 - 20/06/2018' |
| 143 | + ); |
| 144 | + }); |
| 145 | +}); |
0 commit comments