54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { render, screen, fireEvent } from '@testing-library/react';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import TimeAdjuster from '../../../../src/components/tracker/TimeAdjuster';
|
|
|
|
// Mock time utils to avoid timezone issues in tests
|
|
vi.mock('../../../src/utils/time', () => ({
|
|
toLocalDatetimeInputValue: vi.fn((d) => d.toISOString().slice(0, 16)),
|
|
toUTCFromLocalInput: vi.fn((val) => val), // simple identity for testing
|
|
}));
|
|
|
|
describe('TimeAdjuster', () => {
|
|
const onChange = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('renders with default value if provided', () => {
|
|
const defaultValue = '2024-01-01T12:00';
|
|
render(<TimeAdjuster onTimeChange={onChange} defaultValue={defaultValue} />);
|
|
|
|
const input = screen.getByLabelText(/Time/i) as HTMLInputElement;
|
|
expect(input.value).toBe(defaultValue);
|
|
});
|
|
|
|
it('calls onTimeChange when input value changes', async () => {
|
|
render(<TimeAdjuster onTimeChange={onChange} />);
|
|
|
|
const input = screen.getByLabelText(/Time/i) as HTMLInputElement;
|
|
fireEvent.change(input, { target: { value: '2024-01-01T13:00' } });
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith('2024-01-01T12:00:00.000Z');
|
|
});
|
|
|
|
it('resets to now when reset button is clicked', () => {
|
|
const mockNow = new Date('2024-01-01T10:00:00');
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(mockNow);
|
|
|
|
render(<TimeAdjuster onTimeChange={onChange} />);
|
|
|
|
const resetButton = screen.getByText(/Reset to now/i);
|
|
fireEvent.click(resetButton);
|
|
|
|
const input = screen.getByLabelText(/Time/i) as HTMLInputElement;
|
|
// The component calls toLocalDatetimeInputValue(new Date())
|
|
// Our mock returns the iso string slice.
|
|
expect(input.value).toBe('2024-01-01T10:00');
|
|
expect(onChange).toHaveBeenCalled();
|
|
|
|
vi.useRealTimers();
|
|
});
|
|
});
|