|
11 | 11 | */
|
12 | 12 |
|
13 | 13 | import {act, fireEvent, render as render_, within} from '@react-spectrum/test-utils';
|
| 14 | +import {parseZonedDateTime, Time} from '@internationalized/date'; |
14 | 15 | import {Provider} from '@react-spectrum/provider';
|
15 | 16 | import React from 'react';
|
16 | 17 | import {theme} from '@react-spectrum/theme-default';
|
17 |
| -import {Time} from '@internationalized/date'; |
18 | 18 | import {TimeField} from '../';
|
19 | 19 | import userEvent from '@testing-library/user-event';
|
20 | 20 |
|
@@ -176,5 +176,64 @@ describe('TimeField', function () {
|
176 | 176 | expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
|
177 | 177 | expect(onKeyUpSpy).toHaveBeenCalledTimes(2);
|
178 | 178 | });
|
| 179 | + |
| 180 | + describe('timeZone', function () { |
| 181 | + it('should have a timeZone when set by defaultValue', function () { |
| 182 | + let {getByRole} = render(<TimeField label="Time" defaultValue={parseZonedDateTime('2023-07-01T00:45-07:00[America/Los_Angeles]')} />); |
| 183 | + let timezone = getByRole('textbox'); |
| 184 | + |
| 185 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 186 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should keep timeZone from defaultValue when minute segment cleared', function () { |
| 190 | + let {getAllByRole, getByRole} = render(<TimeField label="Time" defaultValue={parseZonedDateTime('2023-07-01T01:05-07:00[America/Los_Angeles]')} />); |
| 191 | + let timezone = getByRole('textbox'); |
| 192 | + let segments = getAllByRole('spinbutton'); |
| 193 | + |
| 194 | + userEvent.tab(); |
| 195 | + expect(segments[0]).toHaveFocus(); |
| 196 | + |
| 197 | + userEvent.tab(); |
| 198 | + expect(segments[1]).toHaveFocus(); |
| 199 | + expect(segments[1]).toHaveAttribute('aria-valuetext', '05'); |
| 200 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 201 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 202 | + |
| 203 | + fireEvent.keyDown(document.activeElement, {key: 'Backspace'}); |
| 204 | + fireEvent.keyUp(document.activeElement, {key: 'Backspace'}); |
| 205 | + expect(segments[1]).toHaveAttribute('aria-valuetext', 'Empty'); |
| 206 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 207 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should keep timeZone from defaultValue when minute segment cleared then set', function () { |
| 211 | + let {getAllByRole, getByRole} = render(<TimeField label="Time" defaultValue={parseZonedDateTime('2023-07-01T01:05-07:00[America/Los_Angeles]')} />); |
| 212 | + let timezone = getByRole('textbox'); |
| 213 | + let segments = getAllByRole('spinbutton'); |
| 214 | + |
| 215 | + userEvent.tab(); |
| 216 | + expect(segments[0]).toHaveFocus(); |
| 217 | + |
| 218 | + userEvent.tab(); |
| 219 | + expect(segments[1]).toHaveFocus(); |
| 220 | + expect(segments[1]).toHaveAttribute('aria-valuetext', '05'); |
| 221 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 222 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 223 | + |
| 224 | + fireEvent.keyDown(document.activeElement, {key: 'Backspace'}); |
| 225 | + fireEvent.keyUp(document.activeElement, {key: 'Backspace'}); |
| 226 | + expect(segments[1]).toHaveFocus(); |
| 227 | + expect(segments[1]).toHaveAttribute('aria-valuetext', 'Empty'); |
| 228 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 229 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 230 | + |
| 231 | + fireEvent.keyDown(document.activeElement, {key: 'ArrowUp'}); |
| 232 | + fireEvent.keyUp(document.activeElement, {key: 'ArrowUp'}); |
| 233 | + expect(segments[1]).toHaveAttribute('aria-valuetext', '00'); |
| 234 | + expect(timezone.getAttribute('aria-label')).toBe('time zone, '); |
| 235 | + expect(within(timezone).getByText('PDT')).toBeInTheDocument(); |
| 236 | + }); |
| 237 | + }); |
179 | 238 | });
|
180 | 239 | });
|
0 commit comments