1+ /* @vitest -environment jsdom */
2+ import "@testing-library/jest-dom/vitest" ;
3+ import { cleanup , render , screen , waitFor } from "@testing-library/react" ;
4+ import { afterEach , describe , expect , it , vi } from "vitest" ;
5+ import { App } from "./App" ;
6+ import { messages } from "./lib/i18n" ;
7+
8+ vi . mock ( "./components/GraphTab" , ( ) => ( { GraphTab : ( ) => null } ) ) ;
9+ vi . mock ( "./components/StatsTab" , ( ) => ( { StatsTab : ( ) => null } ) ) ;
10+ vi . mock ( "./components/ControlTab" , ( ) => ( { ControlTab : ( ) => null } ) ) ;
11+ vi . mock ( "./lib/i18n" , async ( importOriginal ) => {
12+ const actual = await importOriginal < typeof import ( "./lib/i18n" ) > ( ) ;
13+ return { ...actual , useUiMessages : ( ) => messages . en } ;
14+ } ) ;
15+
16+ describe ( "App" , ( ) => {
17+ afterEach ( ( ) => {
18+ cleanup ( ) ;
19+ vi . unstubAllGlobals ( ) ;
20+ window . history . replaceState ( null , "" , "/" ) ;
21+ } ) ;
22+
23+ it ( "shows the serving binary version" , async ( ) => {
24+ vi . stubGlobal ( "fetch" , vi . fn ( async ( ) =>
25+ new Response ( JSON . stringify ( { lang : "en" , version : "0.10.8" } ) , {
26+ status : 200 ,
27+ headers : { "Content-Type" : "application/json" } ,
28+ } ) ,
29+ ) ) ;
30+
31+ render ( < App /> ) ;
32+
33+ expect ( await screen . findByText ( "v0.10.8" ) ) . toBeVisible ( ) ;
34+ } ) ;
35+
36+ it ( "hides the version when the config has no string version" , async ( ) => {
37+ const fetchMock = vi . fn ( async ( ) =>
38+ new Response ( JSON . stringify ( { lang : "en" , version : 108 } ) , { status : 200 } ) ,
39+ ) ;
40+ vi . stubGlobal ( "fetch" , fetchMock ) ;
41+
42+ render ( < App /> ) ;
43+
44+ await waitFor ( ( ) => expect ( fetchMock ) . toHaveBeenCalledWith ( "/api/ui-config" ) ) ;
45+ expect ( screen . queryByTitle ( "Server version" ) ) . not . toBeInTheDocument ( ) ;
46+ } ) ;
47+
48+ it ( "hides the version when the config request fails" , async ( ) => {
49+ const fetchMock = vi . fn ( async ( ) => {
50+ throw new Error ( "offline" ) ;
51+ } ) ;
52+ vi . stubGlobal ( "fetch" , fetchMock ) ;
53+
54+ render ( < App /> ) ;
55+
56+ await waitFor ( ( ) => expect ( fetchMock ) . toHaveBeenCalledWith ( "/api/ui-config" ) ) ;
57+ expect ( screen . queryByTitle ( "Server version" ) ) . not . toBeInTheDocument ( ) ;
58+ } ) ;
59+ } ) ;
0 commit comments