2026-09-04 19:06:19 +02:00
|
|
|
import { render, screen, within } from '@testing-library/react';
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
import App from '../src/App';
|
|
|
|
|
import { useAuth } from '../src/store/auth';
|
|
|
|
|
import { setAccessToken } from '../src/api/client';
|
|
|
|
|
import type { User, Conversation } from '../src/api/types';
|
|
|
|
|
|
|
|
|
|
const TRAINER: User = {
|
|
|
|
|
id: 'trainer-1',
|
|
|
|
|
email: 'trener@test.local',
|
|
|
|
|
firstName: 'Marek',
|
|
|
|
|
lastName: 'Trener',
|
|
|
|
|
age: 35,
|
|
|
|
|
heightCm: 185,
|
|
|
|
|
phone: null,
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
availability: null,
|
|
|
|
|
priorities: null,
|
|
|
|
|
exclusions: null,
|
|
|
|
|
developMuscles: [],
|
|
|
|
|
canDoMuscles: [],
|
|
|
|
|
difficultMuscles: [],
|
|
|
|
|
avoidMuscles: [],
|
|
|
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CONVERSATIONS: Conversation[] = [
|
|
|
|
|
{
|
|
|
|
|
coachingId: 'coaching-1',
|
2026-09-11 17:39:12 +02:00
|
|
|
role: 'trainer',
|
2026-09-04 19:06:19 +02:00
|
|
|
other: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak' },
|
|
|
|
|
lastMessage: { body: 'Dobrze się czuję', createdAt: '2026-08-24T10:00:00.000Z', senderId: 'trainee-1' },
|
|
|
|
|
unreadCount: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
coachingId: 'coaching-2',
|
2026-09-11 17:39:12 +02:00
|
|
|
role: 'trainer',
|
2026-09-04 19:06:19 +02:00
|
|
|
other: { id: 'trainee-2', firstName: 'Piotr', lastName: 'Kowal' },
|
|
|
|
|
lastMessage: null,
|
|
|
|
|
unreadCount: 0,
|
|
|
|
|
},
|
2026-09-11 17:39:12 +02:00
|
|
|
{
|
|
|
|
|
coachingId: 'coaching-3',
|
|
|
|
|
role: 'trainee',
|
|
|
|
|
other: { id: 'trainer-2', firstName: 'Tomasz', lastName: 'Kowalczyk' },
|
|
|
|
|
lastMessage: { body: 'Hej, omówmy plan', createdAt: '2026-09-01T09:00:00.000Z', senderId: 'trainer-2' },
|
|
|
|
|
unreadCount: 1,
|
|
|
|
|
},
|
2026-09-04 19:06:19 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
|
|
|
if (status === 204) return new Response(null, { status });
|
|
|
|
|
return new Response(JSON.stringify(body), {
|
|
|
|
|
status,
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mockApi(conversations: Conversation[]) {
|
|
|
|
|
vi.stubGlobal(
|
|
|
|
|
'fetch',
|
|
|
|
|
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
|
|
const url = String(input);
|
|
|
|
|
if (url.includes('/auth/login')) return jsonResponse(200, { user: TRAINER, accessToken: 'tok-1' });
|
|
|
|
|
if (url.includes('/auth/refresh'))
|
|
|
|
|
return jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'no cookie' } });
|
|
|
|
|
if (url.includes('/messages/conversations')) return jsonResponse(200, { conversations });
|
|
|
|
|
if (url.includes('/me')) return jsonResponse(200, { user: TRAINER, memberships: [] });
|
|
|
|
|
if (url.includes('/dashboard'))
|
|
|
|
|
return jsonResponse(200, { killstreak: 0, activePlan: null, nextTraining: null, progress: null });
|
|
|
|
|
if (url.includes('/trainer/trainees')) return jsonResponse(200, { trainees: [] });
|
|
|
|
|
if (url.includes('/trainer/invitations')) return jsonResponse(200, { invitations: [] });
|
|
|
|
|
if (url.includes('/gyms/invitations')) return jsonResponse(200, { invitations: [] });
|
|
|
|
|
if (url.includes('/me/active-session')) return jsonResponse(200, { session: null });
|
|
|
|
|
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
|
|
|
|
if (url.includes('/exercises')) return jsonResponse(200, { exercises: [] });
|
|
|
|
|
if (url.includes('/plans')) return jsonResponse(200, { plans: [] });
|
|
|
|
|
if (url.includes('/calendar')) return jsonResponse(200, { month: '2026-08', days: [] });
|
|
|
|
|
return jsonResponse(404, { error: { code: 'NOT_FOUND', message: 'not found' } });
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loginToMessages(user: userEvent.User) {
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'FitOS' })).toBeInTheDocument();
|
|
|
|
|
const email = document.querySelector('input[type="email"]') as HTMLInputElement;
|
|
|
|
|
const password = document.querySelector('input[type="password"]') as HTMLInputElement;
|
|
|
|
|
await user.type(email, 'trener@test.local');
|
|
|
|
|
await user.type(password, 'Password123!');
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Dashboard' })).toBeInTheDocument();
|
|
|
|
|
await user.click(screen.getByRole('link', { name: /Komunikator/ }));
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Komunikator' })).toBeInTheDocument();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderApp() {
|
|
|
|
|
const queryClient = new QueryClient({
|
|
|
|
|
defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } },
|
|
|
|
|
});
|
|
|
|
|
return render(
|
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
|
<MemoryRouter initialEntries={['/']}>
|
|
|
|
|
<App />
|
|
|
|
|
</MemoryRouter>
|
|
|
|
|
</QueryClientProvider>,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
await new Promise((r) => setTimeout(r, 25));
|
|
|
|
|
setAccessToken(null);
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
useAuth.setState({ user: null, ready: false });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.unstubAllGlobals();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Messages — lista rozmów', () => {
|
|
|
|
|
it('pokazuje rozmowy z podglądem ostatniej wiadomości i badge nieprzeczytanych', async () => {
|
|
|
|
|
mockApi(CONVERSATIONS);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Dobrze się czuję')).toBeInTheDocument();
|
|
|
|
|
// badge nieprzeczytanych wewnątrz kafelka rozmowy
|
|
|
|
|
const annaItem = screen.getByText('Anna Nowak').closest('button') as HTMLElement;
|
|
|
|
|
expect(within(annaItem).getByText('2')).toBeInTheDocument();
|
|
|
|
|
// rozmowa bez wiadomości: placeholder
|
|
|
|
|
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Brak wiadomości')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-11 17:39:12 +02:00
|
|
|
it('oznacza własną ostatnią wiadomość „Ty:" i zachowuje kolejność z API (najnowsze najpierw)', async () => {
|
2026-09-04 23:19:28 +02:00
|
|
|
const convs: Conversation[] = [
|
|
|
|
|
{
|
|
|
|
|
coachingId: 'coaching-2',
|
2026-09-11 17:39:12 +02:00
|
|
|
role: 'trainer',
|
2026-09-04 23:19:28 +02:00
|
|
|
other: { id: 'trainee-2', firstName: 'Piotr', lastName: 'Kowal' },
|
|
|
|
|
lastMessage: { body: 'Do zobaczenia', createdAt: '2026-08-25T09:00:00.000Z', senderId: 'trainer-1' },
|
|
|
|
|
unreadCount: 0,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
coachingId: 'coaching-1',
|
2026-09-11 17:39:12 +02:00
|
|
|
role: 'trainer',
|
2026-09-04 23:19:28 +02:00
|
|
|
other: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak' },
|
|
|
|
|
lastMessage: { body: 'Dobrze się czuję', createdAt: '2026-08-24T10:00:00.000Z', senderId: 'trainee-1' },
|
|
|
|
|
unreadCount: 0,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
mockApi(convs);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
// własna ostatnia wiadomość ma prefiks „Ty:"
|
|
|
|
|
expect(screen.getByText('Ty: Do zobaczenia')).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// nowsza rozmowa (Piotr) wyżej niż starsza (Anna)
|
|
|
|
|
const rows = screen
|
|
|
|
|
.getAllByRole('button')
|
|
|
|
|
.filter((b) => /Anna Nowak|Piotr Kowal/.test(b.textContent ?? ''));
|
|
|
|
|
expect(rows).toHaveLength(2);
|
|
|
|
|
expect(rows[0]).toHaveTextContent('Piotr Kowal');
|
|
|
|
|
expect(rows[1]).toHaveTextContent('Anna Nowak');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-04 19:06:19 +02:00
|
|
|
it('pusty stan gdy brak aktywnych relacji', async () => {
|
|
|
|
|
mockApi([]);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText('Brak rozmów')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-11 17:39:12 +02:00
|
|
|
it('sortowanie wg nieprzeczytanych: rozmowy z nieprzeczytanymi na górze', async () => {
|
|
|
|
|
mockApi(CONVERSATIONS);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
// domyślnie: kolejność z API (Anna, Piotr, Tomasz)
|
|
|
|
|
const rows = () =>
|
|
|
|
|
screen.getAllByRole('button').filter((b) => /Anna Nowak|Piotr Kowal|Tomasz Kowalczyk/.test(b.textContent ?? ''));
|
|
|
|
|
expect(rows()[0]).toHaveTextContent('Anna Nowak');
|
|
|
|
|
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Sortuj: Brak' }));
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Nieprzeczytane' }));
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Sortuj: Nieprzeczytane' })).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// Anna (2 nieprzeczytane) > Tomasz (1) > Piotr (0)
|
|
|
|
|
expect(rows()[0]).toHaveTextContent('Anna Nowak');
|
|
|
|
|
expect(rows()[1]).toHaveTextContent('Tomasz Kowalczyk');
|
|
|
|
|
expect(rows()[2]).toHaveTextContent('Piotr Kowal');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sortowanie wg nazwy: rozmowy alfabetycznie', async () => {
|
|
|
|
|
mockApi(CONVERSATIONS);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Sortuj: Brak' }));
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Nazwa' }));
|
|
|
|
|
|
|
|
|
|
const rows = screen.getAllByRole('button').filter((b) => /Anna Nowak|Piotr Kowal|Tomasz Kowalczyk/.test(b.textContent ?? ''));
|
|
|
|
|
expect(rows[0]).toHaveTextContent('Anna Nowak');
|
|
|
|
|
expect(rows[1]).toHaveTextContent('Piotr Kowal');
|
|
|
|
|
expect(rows[2]).toHaveTextContent('Tomasz Kowalczyk');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('grupowanie wg roli: zakładki Podopieczni/Trenerzy z rozmowami w odpowiednich grupach', async () => {
|
|
|
|
|
mockApi(CONVERSATIONS);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Grupuj wg roli' }));
|
|
|
|
|
|
|
|
|
|
// domyślnie pierwsza grupa: Podopieczni (Anna + Piotr)
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Podopieczni' })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Trenerzy' })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
|
|
|
|
|
expect(screen.queryByText('Tomasz Kowalczyk')).not.toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// przełączenie na Trenerzy
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Trenerzy' }));
|
|
|
|
|
expect(screen.getByText('Tomasz Kowalczyk')).toBeInTheDocument();
|
|
|
|
|
expect(screen.queryByText('Anna Nowak')).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-04 19:06:19 +02:00
|
|
|
it('wyszukiwarka w menu kontekstowym filtruje listę rozmów', async () => {
|
|
|
|
|
mockApi(CONVERSATIONS);
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
renderApp();
|
|
|
|
|
await loginToMessages(user);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// wyszukiwarka jest w menu kontekstowym (nagłówek) za ikoną 🔍
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Wyszukiwarka' }));
|
|
|
|
|
const searchbox = screen.getByRole('searchbox', { name: 'Szukaj rozmowy' });
|
|
|
|
|
await user.type(searchbox, 'anna');
|
|
|
|
|
expect(screen.queryByText('Piotr Kowal')).not.toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
// zapytanie bez trafień pokazuje komunikat
|
|
|
|
|
await user.type(searchbox, 'xxx');
|
|
|
|
|
expect(screen.getByText('Brak wyników dla podanego zapytania.')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
});
|