|
| 1 | +import {TestBed} from '@angular/core/testing'; |
| 2 | +import { |
| 3 | + DateAdapter, |
| 4 | + MAT_DATE_FORMATS, |
| 5 | + MatDateFormats, |
| 6 | + provideNativeDateAdapter, |
| 7 | +} from '@angular/material/core'; |
| 8 | +import {generateOptions, parseInterval} from './util'; |
| 9 | + |
| 10 | +describe('timepicker utilities', () => { |
| 11 | + describe('parseInterval', () => { |
| 12 | + it('should parse null', () => { |
| 13 | + expect(parseInterval(null)).toBe(null); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should parse a number', () => { |
| 17 | + expect(parseInterval(75)).toBe(75); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should parse a number in a string', () => { |
| 21 | + expect(parseInterval('75')).toBe(75); |
| 22 | + expect(parseInterval('75.50')).toBe(75.5); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle invalid strings', () => { |
| 26 | + expect(parseInterval('')).toBe(null); |
| 27 | + expect(parseInterval(' ')).toBe(null); |
| 28 | + expect(parseInterval('abc')).toBe(null); |
| 29 | + expect(parseInterval('1a')).toBe(null); |
| 30 | + expect(parseInterval('m1')).toBe(null); |
| 31 | + expect(parseInterval('10.')).toBe(null); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should parse hours', () => { |
| 35 | + expect(parseInterval('3h')).toBe(10_800); |
| 36 | + expect(parseInterval('4.5h')).toBe(16_200); |
| 37 | + expect(parseInterval('11h')).toBe(39_600); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should parse minutes', () => { |
| 41 | + expect(parseInterval('3m')).toBe(180); |
| 42 | + expect(parseInterval('7.5m')).toBe(450); |
| 43 | + expect(parseInterval('90m')).toBe(5_400); |
| 44 | + expect(parseInterval('100.5m')).toBe(6_030); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should parse seconds', () => { |
| 48 | + expect(parseInterval('3s')).toBe(3); |
| 49 | + expect(parseInterval('7.5s')).toBe(7.5); |
| 50 | + expect(parseInterval('90s')).toBe(90); |
| 51 | + expect(parseInterval('100.5s')).toBe(100.5); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should parse uppercase units', () => { |
| 55 | + expect(parseInterval('3H')).toBe(10_800); |
| 56 | + expect(parseInterval('3M')).toBe(180); |
| 57 | + expect(parseInterval('3S')).toBe(3); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('generateOptions', () => { |
| 62 | + let adapter: DateAdapter<Date>; |
| 63 | + let formats: MatDateFormats; |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + TestBed.configureTestingModule({providers: [provideNativeDateAdapter()]}); |
| 67 | + adapter = TestBed.inject(DateAdapter); |
| 68 | + formats = TestBed.inject(MAT_DATE_FORMATS); |
| 69 | + adapter.setLocale('en-US'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should generate a list of options', () => { |
| 73 | + const min = new Date(2024, 0, 1, 9, 0, 0, 0); |
| 74 | + const max = new Date(2024, 0, 1, 22, 0, 0, 0); |
| 75 | + const options = generateOptions(adapter, formats, min, max, 3600).map(o => o.label); |
| 76 | + expect(options).toEqual([ |
| 77 | + '9:00 AM', |
| 78 | + '10:00 AM', |
| 79 | + '11:00 AM', |
| 80 | + '12:00 PM', |
| 81 | + '1:00 PM', |
| 82 | + '2:00 PM', |
| 83 | + '3:00 PM', |
| 84 | + '4:00 PM', |
| 85 | + '5:00 PM', |
| 86 | + '6:00 PM', |
| 87 | + '7:00 PM', |
| 88 | + '8:00 PM', |
| 89 | + '9:00 PM', |
| 90 | + '10:00 PM', |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should generate a list of options with a sub-hour interval', () => { |
| 95 | + const min = new Date(2024, 0, 1, 9, 0, 0, 0); |
| 96 | + const max = new Date(2024, 0, 1, 22, 0, 0, 0); |
| 97 | + const options = generateOptions(adapter, formats, min, max, 43 * 60).map(o => o.label); |
| 98 | + expect(options).toEqual([ |
| 99 | + '9:00 AM', |
| 100 | + '9:43 AM', |
| 101 | + '10:26 AM', |
| 102 | + '11:09 AM', |
| 103 | + '11:52 AM', |
| 104 | + '12:35 PM', |
| 105 | + '1:18 PM', |
| 106 | + '2:01 PM', |
| 107 | + '2:44 PM', |
| 108 | + '3:27 PM', |
| 109 | + '4:10 PM', |
| 110 | + '4:53 PM', |
| 111 | + '5:36 PM', |
| 112 | + '6:19 PM', |
| 113 | + '7:02 PM', |
| 114 | + '7:45 PM', |
| 115 | + '8:28 PM', |
| 116 | + '9:11 PM', |
| 117 | + '9:54 PM', |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should generate a list of options with a minute interval', () => { |
| 122 | + const min = new Date(2024, 0, 1, 9, 0, 0, 0); |
| 123 | + const max = new Date(2024, 0, 1, 9, 16, 0, 0); |
| 124 | + const options = generateOptions(adapter, formats, min, max, 60).map(o => o.label); |
| 125 | + expect(options).toEqual([ |
| 126 | + '9:00 AM', |
| 127 | + '9:01 AM', |
| 128 | + '9:02 AM', |
| 129 | + '9:03 AM', |
| 130 | + '9:04 AM', |
| 131 | + '9:05 AM', |
| 132 | + '9:06 AM', |
| 133 | + '9:07 AM', |
| 134 | + '9:08 AM', |
| 135 | + '9:09 AM', |
| 136 | + '9:10 AM', |
| 137 | + '9:11 AM', |
| 138 | + '9:12 AM', |
| 139 | + '9:13 AM', |
| 140 | + '9:14 AM', |
| 141 | + '9:15 AM', |
| 142 | + '9:16 AM', |
| 143 | + ]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should generate a list of options with a sub-minute interval', () => { |
| 147 | + const previousFormat = formats.display.timeOptionLabel; |
| 148 | + formats.display.timeOptionLabel = {hour: 'numeric', minute: 'numeric', second: 'numeric'}; |
| 149 | + const min = new Date(2024, 0, 1, 9, 0, 0, 0); |
| 150 | + const max = new Date(2024, 0, 1, 9, 3, 0, 0); |
| 151 | + const options = generateOptions(adapter, formats, min, max, 12).map(o => o.label); |
| 152 | + expect(options).toEqual([ |
| 153 | + '9:00:00 AM', |
| 154 | + '9:00:12 AM', |
| 155 | + '9:00:24 AM', |
| 156 | + '9:00:36 AM', |
| 157 | + '9:00:48 AM', |
| 158 | + '9:01:00 AM', |
| 159 | + '9:01:12 AM', |
| 160 | + '9:01:24 AM', |
| 161 | + '9:01:36 AM', |
| 162 | + '9:01:48 AM', |
| 163 | + '9:02:00 AM', |
| 164 | + '9:02:12 AM', |
| 165 | + '9:02:24 AM', |
| 166 | + '9:02:36 AM', |
| 167 | + '9:02:48 AM', |
| 168 | + '9:03:00 AM', |
| 169 | + ]); |
| 170 | + formats.display.timeOptionLabel = previousFormat; |
| 171 | + }); |
| 172 | + |
| 173 | + it('should generate at least one option if the interval is too large', () => { |
| 174 | + const min = new Date(2024, 0, 1, 0, 0, 0, 0); |
| 175 | + const max = new Date(2024, 0, 1, 23, 59, 0, 0); |
| 176 | + const options = generateOptions(adapter, formats, min, max, 60 * 60 * 24).map(o => o.label); |
| 177 | + expect(options).toEqual(['12:00 AM']); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should generate at least one option if the max is later than the min', () => { |
| 181 | + const min = new Date(2024, 0, 1, 23, 0, 0, 0); |
| 182 | + const max = new Date(2024, 0, 1, 13, 0, 0, 0); |
| 183 | + const options = generateOptions(adapter, formats, min, max, 3600).map(o => o.label); |
| 184 | + expect(options).toEqual(['1:00 PM']); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments