@@ -2,14 +2,21 @@ import React from 'react';
22import { render , screen , waitFor } from '@testing-library/react' ;
33import useFlagsStatus from './useFlagsStatus' ;
44import FlagProvider from './FlagProvider' ;
5- import { EVENTS , UnleashClient } from 'unleash-proxy-client' ;
5+ import { UnleashClient } from 'unleash-proxy-client' ;
66
77const TestComponent = ( ) => {
88 const { flagsReady } = useFlagsStatus ( ) ;
99
1010 return < div > { flagsReady ? 'flagsReady' : 'loading' } </ div > ;
1111} ;
1212
13+ const ErrorTestComponent = ( ) => {
14+ const { flagsError } = useFlagsStatus ( ) ;
15+
16+ return < div > { flagsError ? 'flagsError' : 'no issue' } </ div > ;
17+ } ;
18+
19+
1320const mockClient = {
1421 on : vi . fn ( ) ,
1522 off : vi . fn ( ) ,
@@ -78,3 +85,36 @@ test('should start when already initialized client is passed', async () => {
7885 expect ( screen . queryByText ( 'flagsReady' ) ) . toBeInTheDocument ( ) ;
7986 } ) ;
8087} ) ;
88+
89+ test ( 'should handle client errors' , async ( ) => {
90+ const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
91+
92+ const client = new UnleashClient ( {
93+ url : 'http://localhost:4242/api' ,
94+ fetch : async ( ) => {
95+ throw new Error ( 'test error' ) ;
96+ } ,
97+ clientKey : '123' ,
98+ appName : 'test' ,
99+ } ) ;
100+
101+ await client . start ( ) ;
102+
103+ const ui = (
104+ < FlagProvider unleashClient = { client } >
105+ < ErrorTestComponent />
106+ </ FlagProvider >
107+ ) ;
108+
109+ render ( ui ) ;
110+
111+ await waitFor ( ( ) => {
112+ expect ( screen . queryByText ( 'flagsError' ) ) . toBeInTheDocument ( ) ;
113+ } ) ;
114+
115+ expect ( consoleError ) . toHaveBeenCalledWith (
116+ 'Unleash: unable to fetch feature toggles' ,
117+ expect . any ( Error )
118+ ) ;
119+ consoleError . mockRestore ( ) ;
120+ } ) ;
0 commit comments