Skip to content

Commit e77861b

Browse files
Jeremias Santosclaude
authored andcommitted
test(pav-58): adicionar testes de GitHub OAuth nos componentes de login
Adiciona mocks e testes para handleGithubLogin em RigthSide e RegisterSide, cobrindo sucesso e falha, para atingir threshold de 80% de branch coverage. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9ad830f commit e77861b

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

frontend/tests/unit/components/login/RegisterSide.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
44

55
const mockRegister = vi.fn();
6+
const mockGetGoogleAuthUrl = vi.fn();
7+
const mockGetGithubAuthUrl = vi.fn();
68
const mockGetLinkedinAuthUrl = vi.fn();
79

810
vi.mock("@/services/authService", () => ({
911
register: (...args: any[]) => mockRegister(...args),
12+
getGoogleAuthUrl: (...args: any[]) => mockGetGoogleAuthUrl(...args),
13+
getGithubAuthUrl: (...args: any[]) => mockGetGithubAuthUrl(...args),
1014
getLinkedinAuthUrl: (...args: any[]) => mockGetLinkedinAuthUrl(...args),
1115
}));
1216

@@ -40,6 +44,8 @@ describe("RegisterSide", () => {
4044

4145
beforeEach(() => {
4246
mockRegister.mockReset();
47+
mockGetGoogleAuthUrl.mockReset();
48+
mockGetGithubAuthUrl.mockReset();
4349
mockGetLinkedinAuthUrl.mockReset();
4450
Object.defineProperty(window, "location", {
4551
configurable: true,
@@ -153,6 +159,37 @@ describe("RegisterSide", () => {
153159
});
154160
});
155161

162+
it("redireciona para GitHub OAuth ao clicar no botao GitHub", async () => {
163+
mockGetGithubAuthUrl.mockResolvedValueOnce(
164+
"https://github.com/login/oauth/authorize?state=abc"
165+
);
166+
167+
render(<RegisterSide />);
168+
169+
const buttons = screen.getAllByRole("button");
170+
const githubButton = buttons.find(btn => btn.querySelector('svg.fill-gray-900'));
171+
fireEvent.click(githubButton!);
172+
173+
await waitFor(() => {
174+
expect(mockGetGithubAuthUrl).toHaveBeenCalled();
175+
expect(window.location.href).toBe(
176+
"https://github.com/login/oauth/authorize?state=abc"
177+
);
178+
});
179+
});
180+
181+
it("exibe erro quando GitHub OAuth falha", async () => {
182+
mockGetGithubAuthUrl.mockRejectedValueOnce(new Error("Github indisponível"));
183+
184+
render(<RegisterSide />);
185+
186+
const buttons = screen.getAllByRole("button");
187+
const githubButton = buttons.find(btn => btn.querySelector('svg.fill-gray-900'));
188+
fireEvent.click(githubButton!);
189+
190+
expect(await screen.findByText(/Github indisponível/i)).toBeInTheDocument();
191+
});
192+
156193
it("redireciona para LinkedIn OAuth ao clicar no botao LinkedIn", async () => {
157194
mockGetLinkedinAuthUrl.mockResolvedValueOnce(
158195
"https://www.linkedin.com/oauth/v2/authorization?state=abc"

frontend/tests/unit/components/login/RigthSide.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55

66
const mockLogin = vi.fn();
7+
const mockGetGoogleAuthUrl = vi.fn();
8+
const mockGetGithubAuthUrl = vi.fn();
79
const mockGetLinkedinAuthUrl = vi.fn();
810

911
vi.mock("@/services/authService", () => ({
1012
login: (...args: any[]) => mockLogin(...args),
13+
getGoogleAuthUrl: (...args: any[]) => mockGetGoogleAuthUrl(...args),
14+
getGithubAuthUrl: (...args: any[]) => mockGetGithubAuthUrl(...args),
1115
getLinkedinAuthUrl: (...args: any[]) => mockGetLinkedinAuthUrl(...args),
1216
}));
1317

@@ -33,6 +37,8 @@ describe("RigthSide", () => {
3337

3438
beforeEach(() => {
3539
mockLogin.mockReset();
40+
mockGetGoogleAuthUrl.mockReset();
41+
mockGetGithubAuthUrl.mockReset();
3642
mockGetLinkedinAuthUrl.mockReset();
3743
Object.defineProperty(window, "location", {
3844
configurable: true,
@@ -161,6 +167,37 @@ describe("RigthSide", () => {
161167
expect(screen.getByText(/esqueceu a senha/i)).toBeInTheDocument();
162168
});
163169

170+
it("redireciona para GitHub OAuth ao clicar no botao GitHub", async () => {
171+
mockGetGithubAuthUrl.mockResolvedValueOnce(
172+
"https://github.com/login/oauth/authorize?state=abc"
173+
);
174+
175+
render(<RigthSide />);
176+
177+
const buttons = screen.getAllByRole("button");
178+
const githubButton = buttons.find(btn => btn.querySelector('svg.fill-gray-900'));
179+
fireEvent.click(githubButton!);
180+
181+
await waitFor(() => {
182+
expect(mockGetGithubAuthUrl).toHaveBeenCalled();
183+
expect(window.location.href).toBe(
184+
"https://github.com/login/oauth/authorize?state=abc"
185+
);
186+
});
187+
});
188+
189+
it("exibe erro quando GitHub OAuth falha", async () => {
190+
mockGetGithubAuthUrl.mockRejectedValueOnce(new Error("Github indisponível"));
191+
192+
render(<RigthSide />);
193+
194+
const buttons = screen.getAllByRole("button");
195+
const githubButton = buttons.find(btn => btn.querySelector('svg.fill-gray-900'));
196+
fireEvent.click(githubButton!);
197+
198+
expect(await screen.findByText(/Github indisponível/i)).toBeInTheDocument();
199+
});
200+
164201
it("redireciona para LinkedIn OAuth ao clicar no botao LinkedIn", async () => {
165202
mockGetLinkedinAuthUrl.mockResolvedValueOnce(
166203
"https://www.linkedin.com/oauth/v2/authorization?state=abc"

0 commit comments

Comments
 (0)