Initial commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import SleepButton from '../../src/components/tracker/SleepButton';
|
||||
import { useSleepSession } from '../../src/hooks/useSleepSession';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
|
||||
vi.mock('../../src/hooks/useSleepSession', () => ({
|
||||
useSleepSession: vi.fn(),
|
||||
}));
|
||||
|
||||
const createTestQueryClient = () => new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
|
||||
describe('SleepButton', () => {
|
||||
let queryClient: any;
|
||||
|
||||
beforeEach(() => {
|
||||
queryClient = createTestQueryClient();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
const renderWithProviders = (ui: React.ReactElement) => {
|
||||
return render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{ui}
|
||||
</QueryClientProvider>
|
||||
);
|
||||
};
|
||||
|
||||
it('renders "Start Sleep" when no active session', async () => {
|
||||
(useSleepSession as any).mockReturnValue({
|
||||
activeSession: null,
|
||||
isCreating: false,
|
||||
isEnding: false,
|
||||
});
|
||||
|
||||
renderWithProviders(<SleepButton time="2024-01-01T12:00" />);
|
||||
expect(screen.getByRole('button', { name: /Start Sleep/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders "Stop Sleep" when session is active', async () => {
|
||||
(useSleepSession as any).mockReturnValue({
|
||||
activeSession: { id: 1, start_time: '2024-01-01T12:00' },
|
||||
isCreating: false,
|
||||
isEnding: false,
|
||||
});
|
||||
|
||||
renderWithProviders(<SleepButton time="2024-01-01T12:00" />);
|
||||
expect(screen.getByRole('button', { name: /Stop Sleep/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls endSession when session is active and button is clicked', async () => {
|
||||
const endSessionMock = vi.fn();
|
||||
(useSleepSession as any).mockReturnValue({
|
||||
activeSession: { id: 1, start_time: '2024-01-01T12:00' },
|
||||
isCreating: false,
|
||||
isEnding: false,
|
||||
endSession: endSessionMock,
|
||||
});
|
||||
|
||||
renderWithProviders(<SleepButton time="2024-01-01T14:00" />);
|
||||
const button = screen.getByRole('button', { name: /Stop Sleep/i });
|
||||
fireEvent.click(button);
|
||||
|
||||
await waitFor(() => expect(endSessionMock).toHaveBeenCalled());
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user