Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ model Aluno {
dataCadastro DateTime @default(now())
fotoUrl String?
biometriaHash String? @unique
cep String?
endereco String?
numero String?
bairro String?
cidade String?
estado String?
statusMatricula StatusAluno @default(ATIVA)
nivel Int @default(1)
exp Int @default(0)
Expand Down
6 changes: 3 additions & 3 deletions src/app/aluno/dashboard/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ describe('AlunoDashboardPage', () => {
expect(mockRedirect).toHaveBeenCalledWith('/aluno/login');
});

it('renders not found card when aluno is null in database', async () => {
it('redirects to onboarding when aluno is null in database', async () => {
mockGetUser.mockResolvedValue({
user: { id: '1', email: 'test@test.com' },
error: null,
} as never);
mockPrismaAluno.mockResolvedValue(null);
render(await AlunoDashboardPage());
expect(screen.getByText('Sinto muito!')).toBeTruthy();
await expect(AlunoDashboardPage()).rejects.toThrow('NEXT_REDIRECT');
expect(mockRedirect).toHaveBeenCalledWith('/aluno/onboarding');
});

it('renders dashboard client when aluno is found', async () => {
Expand Down
20 changes: 4 additions & 16 deletions src/app/aluno/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getUser } from '@/utils/supabase/server';
import { prisma } from '@/lib/prisma';
import AlunoDashboardClient from './dashboard-client';
import { redirect } from 'next/navigation';
import { Card, CardHeader, CardContent } from '@/components/ui/card';
import type { Aluno, Treino } from '@/lib/definitions';

export default async function AlunoDashboardPage() {
Expand Down Expand Up @@ -36,22 +35,11 @@ export default async function AlunoDashboardPage() {

const aluno = await alunoPromise;

// No alunos row → first-time OAuth user. Send to onboarding to collect
// required CPF + address. The onboarding action is the sole writer of new
// OAuth-origin rows (replaces the old placeholder-CPF auto-provision).
if (!aluno) {
return (
<div className="flex h-[80vh] items-center justify-center">
<Card className="max-w-md text-center">
<CardHeader>
<h2 className="text-2xl font-semibold leading-none tracking-tight">Sinto muito!</h2>
</CardHeader>
<CardContent className="space-y-2">
<p>Seu perfil de aluno não foi encontrado no novo sistema.</p>
<p>
Por favor, procure o administrador da academia para vincular seu email ({user.email}).
</p>
</CardContent>
</Card>
</div>
);
redirect('/aluno/onboarding');
}

const today = new Date().getDay();
Expand Down
73 changes: 73 additions & 0 deletions src/app/aluno/onboarding/onboarding-client.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import OnboardingClient from './onboarding-client';

vi.mock('@/lib/actions/onboarding', () => ({
completeOnboardingAction: vi.fn(),
}));

vi.mock('@/hooks/use-app-notification', () => ({
useAppNotification: () => ({
success: vi.fn(),
error: vi.fn(),
}),
}));

vi.mock('next/navigation', () => ({
useRouter: () => ({ push: vi.fn() }),
}));

const viacepResponse = (overrides: Record<string, unknown> = {}) => ({
json: async () => ({
logradouro: 'Rua das Flores',
bairro: 'Centro',
localidade: 'Natal',
uf: 'RN',
...overrides,
}),
});

describe('OnboardingClient — ViaCEP lookup', () => {
const originalFetch = global.fetch;
const fetchMock = vi.fn();

beforeEach(() => {
global.fetch = fetchMock;
});
afterEach(() => {
global.fetch = originalFetch;
vi.clearAllMocks();
});

it('fills endereco/bairro/cidade/estado from ViaCEP on valid CEP blur', async () => {
fetchMock.mockResolvedValue(viacepResponse());
render(<OnboardingClient />);

const cepInput = screen.getByPlaceholderText('xxxxx-xxx');
fireEvent.change(cepInput, { target: { value: '59000000' } });
fireEvent.blur(cepInput);

await waitFor(() => {
expect(screen.getByPlaceholderText('Rua / Avenida')).toHaveValue('Rua das Flores');
expect(screen.getByPlaceholderText('Bairro')).toHaveValue('Centro');
expect(screen.getByPlaceholderText('Cidade')).toHaveValue('Natal');
expect(screen.getByPlaceholderText('SP')).toHaveValue('RN');
});
expect(fetchMock).toHaveBeenCalledWith('https://viacep.com.br/ws/59000000/json/');
});

it('leaves fields empty when ViaCEP returns erro (fallback: user types manually)', async () => {
fetchMock.mockResolvedValue({
json: async () => ({ erro: true }),
});
render(<OnboardingClient />);

const cepInput = screen.getByPlaceholderText('xxxxx-xxx');
fireEvent.change(cepInput, { target: { value: '99999-999' } });
fireEvent.blur(cepInput);

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
expect(screen.getByPlaceholderText('Rua / Avenida')).toHaveValue('');
expect(screen.getByPlaceholderText('Cidade')).toHaveValue('');
});
});
Loading
Loading