@@ -4,6 +4,7 @@ import React from "react";
44import { afterEach , beforeEach , describe , expect , mock , test } from "bun:test" ;
55import { cleanup , render } from "@testing-library/react" ;
66import { useTheme } from "../../contexts/ThemeContext" ;
7+ import type { APIClient , UseAPIResult } from "../../contexts/API" ;
78import { installDom } from "../../../../tests/ui/dom" ;
89
910let cleanupDom : ( ( ) => void ) | null = null ;
@@ -27,37 +28,56 @@ void mock.module("lottie-react", () => ({
2728 default : ( ) => < div data-testid = "LottieMock" /> ,
2829} ) ) ;
2930
30- void mock . module ( "@/browser/contexts/API" , ( ) => ( {
31- APIProvider : ( props : { children : React . ReactNode } ) => props . children ,
32- useAPI : ( ) => {
33- if ( apiStatus === "auth_required" ) {
34- return {
35- api : null ,
36- status : "auth_required" as const ,
37- error : apiError ,
38- authenticate : ( ) => undefined ,
39- retry : ( ) => undefined ,
40- } ;
41- }
42-
43- if ( apiStatus === "error" ) {
44- return {
45- api : null ,
46- status : "error" as const ,
47- error : apiError ?? "Connection error" ,
48- authenticate : ( ) => undefined ,
49- retry : ( ) => undefined ,
50- } ;
51- }
31+ function getMockAPIContextValue ( ) : UseAPIResult {
32+ if ( apiStatus === "auth_required" ) {
33+ return {
34+ api : null ,
35+ status : "auth_required" as const ,
36+ error : apiError ,
37+ authenticate : ( ) => undefined ,
38+ retry : ( ) => undefined ,
39+ } ;
40+ }
5241
42+ if ( apiStatus === "error" ) {
5343 return {
5444 api : null ,
55- status : "connecting " as const ,
56- error : null ,
45+ status : "error " as const ,
46+ error : apiError ?? "Connection error" ,
5747 authenticate : ( ) => undefined ,
5848 retry : ( ) => undefined ,
5949 } ;
50+ }
51+
52+ return {
53+ api : null ,
54+ status : "connecting" as const ,
55+ error : null ,
56+ authenticate : ( ) => undefined ,
57+ retry : ( ) => undefined ,
58+ } ;
59+ }
60+
61+ const MockAPIContext = React . createContext < UseAPIResult | null > ( null ) ;
62+ let injectedAPIValue : UseAPIResult | null = null ;
63+
64+ void mock . module ( "@/browser/contexts/API" , ( ) => ( {
65+ APIContext : MockAPIContext ,
66+ APIProvider : ( props : { children : React . ReactNode ; client ?: APIClient } ) => {
67+ const value = props . client
68+ ? {
69+ api : props . client ,
70+ status : "connected" as const ,
71+ error : null ,
72+ authenticate : ( ) => undefined ,
73+ retry : ( ) => undefined ,
74+ }
75+ : getMockAPIContextValue ( ) ;
76+ injectedAPIValue = value ;
77+ return < MockAPIContext . Provider value = { value } > { props . children } </ MockAPIContext . Provider > ;
6078 } ,
79+ useAPI : ( ) => injectedAPIValue ?? getMockAPIContextValue ( ) ,
80+ useOptionalAPI : ( ) => injectedAPIValue ?? getMockAPIContextValue ( ) ,
6181} ) ) ;
6282
6383void mock . module ( "@/browser/components/LoadingScreen/LoadingScreen" , ( ) => ( {
0 commit comments