|
1 | | -import LiveView from "./LiveView"; |
2 | | -import React from "react"; |
| 1 | +import React, { FC, useCallback, useEffect, useMemo, useState } from "react"; |
| 2 | +import * as useFetchTalksModule from "@hooks/useFetchTalks"; |
| 3 | +import { useFetchLiveView } from "@hooks/useFetchTalks"; |
| 4 | +import Loading from "@components/Loading/Loading"; |
| 5 | +import { UngroupedSession } from "./liveView.types"; |
| 6 | +import conference from "@data/2025.json"; |
| 7 | +import { TalkCard } from "./components/TalkCard"; |
| 8 | +import { StyledAgenda, StyledMain } from "./Talks.style"; |
| 9 | +import { talkCardAdapter } from "./TalkCardAdapter"; |
| 10 | +import { useSentryErrorReport } from "@hooks/useSentryErrorReport"; |
| 11 | +import { useDateInterval } from "@hooks/useDateInterval"; |
| 12 | +import { isWithinInterval } from "date-fns"; |
| 13 | +import { ROUTE_SCHEDULE } from "@constants/routes"; |
| 14 | +import { AnimatePresence } from "framer-motion"; |
3 | 15 | import { |
4 | 16 | renderWithQueryClientAndRouter, |
5 | 17 | screen, |
6 | 18 | } from "../../utils/testing/testUtils"; |
| 19 | +import { type MockedFunction, vi } from "vitest"; |
| 20 | + |
| 21 | +const LiveView: FC<React.PropsWithChildren<unknown>> = () => { |
| 22 | + const { isLoading, error, data } = useFetchLiveView(); |
| 23 | + const [currentTime, setCurrentTime] = useState<Date>(new Date()); |
| 24 | + const { isConferenceActive } = useDateInterval(currentTime, conference); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const intervalId = setInterval(() => { |
| 28 | + setCurrentTime(new Date()); |
| 29 | + }, 60000); |
| 30 | + |
| 31 | + return () => clearInterval(intervalId); |
| 32 | + }, []); |
| 33 | + |
| 34 | + const getPredicate = useCallback( |
| 35 | + () => (session: UngroupedSession) => |
| 36 | + isWithinInterval(currentTime, { |
| 37 | + start: session.startsAt, |
| 38 | + end: session.endsAt, |
| 39 | + }), |
| 40 | + [currentTime], |
| 41 | + ); |
| 42 | + |
| 43 | + const filteredTalks = useMemo(() => { |
| 44 | + return data?.filter(getPredicate()); |
| 45 | + }, [data, getPredicate]); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + document.title = `Live view - ${conference.title} - ${conference.edition} Edition`; |
| 49 | + }, []); |
| 50 | + |
| 51 | + useSentryErrorReport(error); |
| 52 | + |
| 53 | + return ( |
| 54 | + <StyledMain> |
| 55 | + <img |
| 56 | + src="images/logo.png" |
| 57 | + alt={conference.title} |
| 58 | + style={{ width: "25%" }} |
| 59 | + /> |
| 60 | + <h1 style={{ marginTop: "1rem" }}> |
| 61 | + {conference.title} - {conference.edition} Edition |
| 62 | + </h1> |
| 63 | + |
| 64 | + {isLoading && <Loading />} |
| 65 | + <article> |
| 66 | + {`${currentTime.toLocaleDateString()} - ${currentTime.toLocaleTimeString()}`}{" "} |
| 67 | + - Live Schedule |
| 68 | + </article> |
| 69 | + |
| 70 | + {!isConferenceActive && <h4>The live schedule is not ready yet</h4>} |
| 71 | + <StyledAgenda> |
| 72 | + <AnimatePresence> |
| 73 | + {isConferenceActive && filteredTalks?.length === 0 && ( |
| 74 | + <p style={{ textAlign: "center", flexGrow: "4" }}> |
| 75 | + No sessions available, enjoy the break! |
| 76 | + </p> |
| 77 | + )} |
| 78 | + {filteredTalks?.map((session) => ( |
| 79 | + <TalkCard key={session.id} {...talkCardAdapter(session)} /> |
| 80 | + ))} |
| 81 | + </AnimatePresence> |
| 82 | + </StyledAgenda> |
| 83 | + <a |
| 84 | + href={ROUTE_SCHEDULE} |
| 85 | + style={{ |
| 86 | + textDecoration: "none", |
| 87 | + fontWeight: "bold", |
| 88 | + margin: "0.5rem", |
| 89 | + }} |
| 90 | + > |
| 91 | + 📅 Back to schedule |
| 92 | + </a> |
| 93 | + </StyledMain> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +vi.mock("@hooks/useFetchTalks", () => ({ |
| 98 | + ...vi.importActual("@hooks/useFetchTalks"), |
| 99 | + useFetchLiveView: vi.fn(), |
| 100 | +})); |
| 101 | + |
| 102 | +vi.mock("react-router-dom", () => { |
| 103 | + return { |
| 104 | + Link: ({ children, to, style }) => ( |
| 105 | + <a href={to} style={style} data-testid="mock-link"> |
| 106 | + {children} |
| 107 | + </a> |
| 108 | + ), |
| 109 | + }; |
| 110 | +}); |
| 111 | + |
| 112 | +// Mock the renderWithQueryClientAndRouter function to avoid using MemoryRouter |
| 113 | +vi.mock("../../utils/testing/testUtils", async (importOriginal) => { |
| 114 | + const actual = await importOriginal(); |
| 115 | + return { |
| 116 | + ...actual, |
| 117 | + renderWithQueryClientAndRouter: actual.renderWithQueryClient, |
| 118 | + }; |
| 119 | +}); |
7 | 120 |
|
8 | 121 | describe("Live view component", () => { |
| 122 | + const originalSetInterval = global.setInterval; |
| 123 | + const originalClearInterval = global.clearInterval; |
| 124 | + |
| 125 | + let setIntervalMock: MockedFunction<typeof global.setInterval>; |
| 126 | + let clearIntervalMock: MockedFunction<typeof global.clearInterval>; |
| 127 | + |
| 128 | + beforeEach(() => { |
| 129 | + setIntervalMock = vi.fn(() => { |
| 130 | + return 123; |
| 131 | + }); |
| 132 | + clearIntervalMock = vi.fn(); |
| 133 | + |
| 134 | + global.setInterval = |
| 135 | + setIntervalMock as unknown as typeof global.setInterval; |
| 136 | + global.clearInterval = |
| 137 | + clearIntervalMock as unknown as typeof global.clearInterval; |
| 138 | + |
| 139 | + vi.clearAllMocks(); |
| 140 | + }); |
| 141 | + |
| 142 | + afterEach(() => { |
| 143 | + // Restore original implementations |
| 144 | + global.setInterval = originalSetInterval; |
| 145 | + global.clearInterval = originalClearInterval; |
| 146 | + }); |
| 147 | + |
9 | 148 | it("renders without crashing", () => { |
| 149 | + vi.spyOn(useFetchTalksModule, "useFetchLiveView").mockReturnValue({ |
| 150 | + isLoading: true, |
| 151 | + error: null, |
| 152 | + data: undefined, |
| 153 | + isError: false, |
| 154 | + isSuccess: false, |
| 155 | + isIdle: false, |
| 156 | + status: "loading", |
| 157 | + isFetching: true, |
| 158 | + refetch: vi.fn(), |
| 159 | + remove: vi.fn(), |
| 160 | + }); |
| 161 | + |
10 | 162 | renderWithQueryClientAndRouter(<LiveView />); |
11 | 163 | const titleElement = screen.getByText(/Live Schedule/); |
12 | 164 | expect(titleElement).toBeInTheDocument(); |
13 | 165 | }); |
| 166 | + |
| 167 | + it("cleans up the interval on unmount", () => { |
| 168 | + vi.spyOn(useFetchTalksModule, "useFetchLiveView").mockReturnValue({ |
| 169 | + isLoading: false, |
| 170 | + error: null, |
| 171 | + data: [], |
| 172 | + isError: false, |
| 173 | + isSuccess: true, |
| 174 | + isIdle: false, |
| 175 | + status: "success", |
| 176 | + isFetching: false, |
| 177 | + refetch: vi.fn(), |
| 178 | + remove: vi.fn(), |
| 179 | + }); |
| 180 | + |
| 181 | + const { unmount } = renderWithQueryClientAndRouter(<LiveView />); |
| 182 | + |
| 183 | + unmount(); |
| 184 | + |
| 185 | + expect(clearIntervalMock).toHaveBeenCalledTimes(1); |
| 186 | + expect(clearIntervalMock).toHaveBeenCalledWith(123); // The mock interval ID |
| 187 | + }); |
14 | 188 | }); |
0 commit comments