|
| 1 | +import { UseQueryResult } from "@tanstack/react-query"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { RpcError } from "grpc-web"; |
| 4 | +import { GetVolunteersRes, Volunteer } from "proto/public_pb"; |
| 5 | +import wrapper from "test/hookWrapper"; |
| 6 | +import i18n from "test/i18n"; |
| 7 | + |
| 8 | +import Facts from "./Facts"; |
| 9 | + |
| 10 | +const { t } = i18n; |
| 11 | + |
| 12 | +const makeVolunteers = ( |
| 13 | + current: Volunteer.AsObject[] = [], |
| 14 | + past: Volunteer.AsObject[] = [], |
| 15 | + isLoading = false, |
| 16 | +) => |
| 17 | + ({ |
| 18 | + data: { currentVolunteersList: current, pastVolunteersList: past }, |
| 19 | + isLoading, |
| 20 | + }) as UseQueryResult<GetVolunteersRes.AsObject, RpcError>; |
| 21 | + |
| 22 | +const mockSignupInfo = { |
| 23 | + userCount: "80000", |
| 24 | + lastSignup: "2024-01-01T00:00:00Z", |
| 25 | + lastLocation: "Berlin", |
| 26 | +}; |
| 27 | + |
| 28 | +describe("Facts", () => { |
| 29 | + beforeEach(() => { |
| 30 | + global.fetch = jest.fn().mockResolvedValue({ |
| 31 | + ok: true, |
| 32 | + json: () => Promise.resolve(mockSignupInfo), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + jest.restoreAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("shows skeletons while loading and hides stat text", () => { |
| 41 | + const { container } = render( |
| 42 | + <Facts volunteers={makeVolunteers([], [], true)} />, |
| 43 | + { wrapper }, |
| 44 | + ); |
| 45 | + expect(container.querySelectorAll(".MuiSkeleton-root")).toHaveLength(4); |
| 46 | + expect(screen.queryByText(/members/)).not.toBeInTheDocument(); |
| 47 | + expect(screen.queryByText(/countries/)).not.toBeInTheDocument(); |
| 48 | + expect(screen.queryByText(/volunteers/)).not.toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("shows user count from API", async () => { |
| 52 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 53 | + expect( |
| 54 | + await screen.findByText(t("landing:num_users2", { count: 80000 })), |
| 55 | + ).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("falls back to 77000 when fetch fails", async () => { |
| 59 | + global.fetch = jest.fn().mockRejectedValue(new Error("Network error")); |
| 60 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 61 | + expect( |
| 62 | + await screen.findByText(t("landing:num_users2", { count: 77000 })), |
| 63 | + ).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("falls back to 77000 when the response is not ok", async () => { |
| 67 | + global.fetch = jest.fn().mockResolvedValue({ |
| 68 | + ok: false, |
| 69 | + json: () => Promise.resolve({}), |
| 70 | + }); |
| 71 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 72 | + expect( |
| 73 | + await screen.findByText(t("landing:num_users2", { count: 77000 })), |
| 74 | + ).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("falls back to 77000 when userCount is not a valid number", async () => { |
| 78 | + global.fetch = jest.fn().mockResolvedValue({ |
| 79 | + ok: true, |
| 80 | + json: () => Promise.resolve({ ...mockSignupInfo, userCount: "bad" }), |
| 81 | + }); |
| 82 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 83 | + expect( |
| 84 | + await screen.findByText(t("landing:num_users2", { count: 77000 })), |
| 85 | + ).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("shows countries count", async () => { |
| 89 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 90 | + expect( |
| 91 | + await screen.findByText(t("landing:num_countries2", { count: 180 })), |
| 92 | + ).toBeInTheDocument(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("shows last signup when lastSignup is present", async () => { |
| 96 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 97 | + expect(await screen.findByText(/Last signup/)).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("hides last signup stat when lastSignup is absent", async () => { |
| 101 | + global.fetch = jest.fn().mockResolvedValue({ |
| 102 | + ok: true, |
| 103 | + json: () => Promise.resolve({ ...mockSignupInfo, lastSignup: "" }), |
| 104 | + }); |
| 105 | + render(<Facts volunteers={makeVolunteers()} />, { wrapper }); |
| 106 | + await screen.findByText(/members/); |
| 107 | + expect(screen.queryByText(/Last signup/)).not.toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("shows combined current and past volunteer count", async () => { |
| 111 | + const current = Array(4).fill({}) as Volunteer.AsObject[]; |
| 112 | + const past = Array(2).fill({}) as Volunteer.AsObject[]; |
| 113 | + render(<Facts volunteers={makeVolunteers(current, past)} />, { wrapper }); |
| 114 | + expect( |
| 115 | + await screen.findByText(t("press:num_volunteers2", { count: 6 })), |
| 116 | + ).toBeInTheDocument(); |
| 117 | + }); |
| 118 | +}); |
0 commit comments