|
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +vi.mock("swiper/react", () => ({ |
| 5 | + Swiper: ({ children }: any) => <div data-testid="swiper">{children}</div>, |
| 6 | + SwiperSlide: ({ children }: any) => <div data-testid="swiper-slide">{children}</div>, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("swiper/modules", () => ({ |
| 10 | + Pagination: {}, |
| 11 | + Autoplay: {}, |
| 12 | +})); |
| 13 | + |
| 14 | +import TeamSection from "@/components/landing/TeamSection"; |
| 15 | + |
| 16 | +describe("TeamSection", () => { |
| 17 | + afterEach(() => { |
| 18 | + vi.restoreAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("renderiza lista da API, filtra bots e garante presença do Pedro", async () => { |
| 22 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 23 | + ok: true, |
| 24 | + json: async () => [ |
| 25 | + { |
| 26 | + id: 2, |
| 27 | + login: "Maria", |
| 28 | + avatar_url: "https://github.com/maria.png", |
| 29 | + html_url: "https://github.com/maria", |
| 30 | + contributions: 1, |
| 31 | + type: "User", |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 3, |
| 35 | + login: "bot-user", |
| 36 | + avatar_url: "x", |
| 37 | + html_url: "x", |
| 38 | + contributions: 10, |
| 39 | + type: "Bot", |
| 40 | + }, |
| 41 | + ], |
| 42 | + } as any); |
| 43 | + |
| 44 | + render(<TeamSection />); |
| 45 | + |
| 46 | + await waitFor(() => { |
| 47 | + expect(screen.getByText("Maria")).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + expect(screen.queryByText("bot-user")).not.toBeInTheDocument(); |
| 51 | + expect(screen.getByText("PedroLucas1337")).toBeInTheDocument(); |
| 52 | + expect(screen.getByText("QA")).toBeInTheDocument(); |
| 53 | + expect(screen.getByText("1 commit")).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("mantém fallback local quando API retorna resposta inválida", async () => { |
| 57 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 58 | + ok: true, |
| 59 | + json: async () => ({ invalid: true }), |
| 60 | + } as any); |
| 61 | + |
| 62 | + render(<TeamSection />); |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(screen.getByText("Benevanio")).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it("mantém fallback local e emite warning quando falha na API", async () => { |
| 70 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 71 | + vi.spyOn(global, "fetch").mockRejectedValue(new Error("offline")); |
| 72 | + |
| 73 | + render(<TeamSection />); |
| 74 | + |
| 75 | + await waitFor(() => { |
| 76 | + expect(screen.getByText("Benevanio")).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(warnSpy).toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("atualiza avatar de PedroLucas1337 quando vem da API com avatar incorreto", async () => { |
| 83 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 84 | + ok: true, |
| 85 | + json: async () => [ |
| 86 | + { |
| 87 | + id: 104951475, |
| 88 | + login: "PedroLucas1337", |
| 89 | + avatar_url: "https://wrong-avatar.com/pedro.png", |
| 90 | + html_url: "https://github.com/PedroLucas1337", |
| 91 | + contributions: 5, |
| 92 | + type: "User", |
| 93 | + }, |
| 94 | + ], |
| 95 | + } as any); |
| 96 | + |
| 97 | + render(<TeamSection />); |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + const pedroImg = screen.getByAltText("PedroLucas1337"); |
| 101 | + expect(pedroImg).toHaveAttribute( |
| 102 | + "src", |
| 103 | + "https://github.com/PedroLucas1337.png" |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it("trata erro HTTP com status nao ok", async () => { |
| 109 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 110 | + ok: false, |
| 111 | + status: 403, |
| 112 | + } as any); |
| 113 | + |
| 114 | + render(<TeamSection />); |
| 115 | + |
| 116 | + await waitFor(() => { |
| 117 | + expect(screen.getByText("Benevanio")).toBeInTheDocument(); |
| 118 | + expect(screen.getByText("PedroLucas1337")).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("exibe plural de commits corretamente", async () => { |
| 123 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 124 | + ok: true, |
| 125 | + json: async () => [ |
| 126 | + { |
| 127 | + id: 5, |
| 128 | + login: "TestUser", |
| 129 | + avatar_url: "https://github.com/test.png", |
| 130 | + html_url: "https://github.com/test", |
| 131 | + contributions: 1, |
| 132 | + type: "User", |
| 133 | + }, |
| 134 | + ], |
| 135 | + } as any); |
| 136 | + |
| 137 | + render(<TeamSection />); |
| 138 | + |
| 139 | + await waitFor(() => { |
| 140 | + expect(screen.getByText("1 commit")).toBeInTheDocument(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("exibe plural de commits quando > 1", async () => { |
| 145 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 146 | + ok: true, |
| 147 | + json: async () => [ |
| 148 | + { |
| 149 | + id: 5, |
| 150 | + login: "TestUser", |
| 151 | + avatar_url: "https://github.com/test.png", |
| 152 | + html_url: "https://github.com/test", |
| 153 | + contributions: 10, |
| 154 | + type: "User", |
| 155 | + }, |
| 156 | + ], |
| 157 | + } as any); |
| 158 | + |
| 159 | + render(<TeamSection />); |
| 160 | + |
| 161 | + await waitFor(() => { |
| 162 | + expect(screen.getByText("10 commits")).toBeInTheDocument(); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("nao atualiza contributors quando lista vazia", async () => { |
| 167 | + vi.spyOn(global, "fetch").mockResolvedValue({ |
| 168 | + ok: true, |
| 169 | + json: async () => [], |
| 170 | + } as any); |
| 171 | + |
| 172 | + render(<TeamSection />); |
| 173 | + |
| 174 | + await waitFor(() => { |
| 175 | + expect(screen.getByText("Benevanio")).toBeInTheDocument(); |
| 176 | + expect(screen.getByText("PedroLucas1337")).toBeInTheDocument(); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments