|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import { expect } from '@jest/globals' |
| 4 | +import { render, screen } from '@testing-library/react' |
| 5 | + |
| 6 | +import { GregorianCalendar, parseDate } from '@internationalized/date' |
| 7 | +import { DateSegment } from '@react-stately/datepicker' |
| 8 | + |
| 9 | +import { |
| 10 | + DateField, |
| 11 | + DateSelects, |
| 12 | + createCalendar, |
| 13 | + getMonthOptions, |
| 14 | + getYearOptions, |
| 15 | + sortDateSegments |
| 16 | +} from '../../src' |
| 17 | +import { DateValue } from 'react-aria-components' |
| 18 | + |
| 19 | +describe('<DateField />', () => { |
| 20 | + describe('renders', () => { |
| 21 | + it('It renders', () => { |
| 22 | + render( |
| 23 | + <DateField |
| 24 | + defaultValue={parseDate('2023-03-02')} |
| 25 | + value={parseDate('2024-04-20')} |
| 26 | + label='Date input' |
| 27 | + onChange={console.log} |
| 28 | + error='Some error' |
| 29 | + isDateUnavailable={(date: DateValue) => false} |
| 30 | + maxEnd={parseDate('2025-03-20')} |
| 31 | + minStart={parseDate('2020-03-20')} |
| 32 | + name='date_field' |
| 33 | + required |
| 34 | + /> |
| 35 | + ) |
| 36 | + }) |
| 37 | + it('It renders with year and month selectors', () => { |
| 38 | + render( |
| 39 | + <DateField |
| 40 | + defaultValue={parseDate('2023-03-02')} |
| 41 | + value={parseDate('2024-04-20')} |
| 42 | + label='Date input' |
| 43 | + onChange={console.log} |
| 44 | + error='Some error' |
| 45 | + isDateUnavailable={(date: DateValue) => false} |
| 46 | + maxEnd={parseDate('2025-03-20')} |
| 47 | + minStart={parseDate('2020-03-20')} |
| 48 | + name='date_field' |
| 49 | + required |
| 50 | + months={[1, 2, 3, 4]} |
| 51 | + years={[2023, 2024]} |
| 52 | + /> |
| 53 | + ) |
| 54 | + }) |
| 55 | + }) |
| 56 | + describe('createCalendar', () => { |
| 57 | + it('should return gregorian calendar', () => { |
| 58 | + const calendar = createCalendar('gregory') |
| 59 | + |
| 60 | + expect(calendar).toBeInstanceOf(GregorianCalendar) |
| 61 | + }) |
| 62 | + it('should throw with other any other identifier', () => { |
| 63 | + expect(() => createCalendar('fake')).toThrow('Unsupported calendar fake') |
| 64 | + }) |
| 65 | + }) |
| 66 | + describe('sortDateSegments', () => { |
| 67 | + it('shoud return sorted segments in the format: YYYY / MM / dd', () => { |
| 68 | + const segments: DateSegment[] = [ |
| 69 | + { |
| 70 | + type: 'day', |
| 71 | + isEditable: true, |
| 72 | + isPlaceholder: false, |
| 73 | + placeholder: 'dd', |
| 74 | + text: '25' |
| 75 | + }, |
| 76 | + { |
| 77 | + type: 'literal', |
| 78 | + isEditable: false, |
| 79 | + isPlaceholder: true, |
| 80 | + placeholder: '/', |
| 81 | + text: '/' |
| 82 | + }, |
| 83 | + { |
| 84 | + type: 'month', |
| 85 | + isEditable: true, |
| 86 | + isPlaceholder: false, |
| 87 | + placeholder: 'mm', |
| 88 | + text: '03' |
| 89 | + }, |
| 90 | + { |
| 91 | + type: 'literal', |
| 92 | + isEditable: false, |
| 93 | + isPlaceholder: true, |
| 94 | + placeholder: '/', |
| 95 | + text: '/' |
| 96 | + }, |
| 97 | + { |
| 98 | + type: 'year', |
| 99 | + isEditable: true, |
| 100 | + isPlaceholder: false, |
| 101 | + placeholder: 'yyyy', |
| 102 | + text: '2023' |
| 103 | + } |
| 104 | + ] |
| 105 | + |
| 106 | + const sorted = sortDateSegments(segments).map(s => s.type) |
| 107 | + |
| 108 | + expect(sorted).toStrictEqual([ |
| 109 | + 'year', |
| 110 | + 'literal', |
| 111 | + 'month', |
| 112 | + 'literal', |
| 113 | + 'day' |
| 114 | + ]) |
| 115 | + }) |
| 116 | + }) |
| 117 | + describe('getYearOptions', () => { |
| 118 | + it('should return only the list of years', () => { |
| 119 | + const years = [2022, 2023, 2024] |
| 120 | + const date = parseDate('2023-03-20') |
| 121 | + |
| 122 | + const result = getYearOptions(years, date) |
| 123 | + |
| 124 | + expect(result).toStrictEqual([ |
| 125 | + { id: '2022', label: '2022', disabled: false }, |
| 126 | + { id: '2023', label: '2023', disabled: false }, |
| 127 | + { id: '2024', label: '2024', disabled: false } |
| 128 | + ]) |
| 129 | + }) |
| 130 | + it('should return the list of years and the current year disabled', () => { |
| 131 | + const years = [2022, 2023, 2024] |
| 132 | + const date = parseDate('2021-03-20') |
| 133 | + |
| 134 | + const result = getYearOptions(years, date) |
| 135 | + |
| 136 | + expect(result).toStrictEqual([ |
| 137 | + { id: '2022', label: '2022', disabled: false }, |
| 138 | + { id: '2023', label: '2023', disabled: false }, |
| 139 | + { id: '2024', label: '2024', disabled: false }, |
| 140 | + { id: '2021', label: '2021', disabled: true } |
| 141 | + ]) |
| 142 | + }) |
| 143 | + }) |
| 144 | + describe('getMonthOptions', () => { |
| 145 | + it('should return month options', () => { |
| 146 | + const months = [1, 2, 3, 4] |
| 147 | + |
| 148 | + const result = getMonthOptions(months) |
| 149 | + |
| 150 | + expect(result).toStrictEqual([ |
| 151 | + { id: '1', label: 'January' }, |
| 152 | + { id: '2', label: 'February' }, |
| 153 | + { id: '3', label: 'March' }, |
| 154 | + { id: '4', label: 'April' } |
| 155 | + ]) |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +describe('<DateSelects />', () => { |
| 161 | + it('renders with months and years', () => { |
| 162 | + render( |
| 163 | + <DateSelects |
| 164 | + value={parseDate('2023-03-20')} |
| 165 | + onDateChange={console.log} |
| 166 | + months={[1, 2]} |
| 167 | + years={[2023]} |
| 168 | + /> |
| 169 | + ) |
| 170 | + }) |
| 171 | +}) |
0 commit comments