@@ -2,14 +2,21 @@ import React from 'react';
2
2
import { render , screen , waitFor } from '@testing-library/react' ;
3
3
import useFlagsStatus from './useFlagsStatus' ;
4
4
import FlagProvider from './FlagProvider' ;
5
- import { EVENTS , UnleashClient } from 'unleash-proxy-client' ;
5
+ import { UnleashClient } from 'unleash-proxy-client' ;
6
6
7
7
const TestComponent = ( ) => {
8
8
const { flagsReady } = useFlagsStatus ( ) ;
9
9
10
10
return < div > { flagsReady ? 'flagsReady' : 'loading' } </ div > ;
11
11
} ;
12
12
13
+ const ErrorTestComponent = ( ) => {
14
+ const { flagsError } = useFlagsStatus ( ) ;
15
+
16
+ return < div > { flagsError ? 'flagsError' : 'no issue' } </ div > ;
17
+ } ;
18
+
19
+
13
20
const mockClient = {
14
21
on : vi . fn ( ) ,
15
22
off : vi . fn ( ) ,
@@ -78,3 +85,36 @@ test('should start when already initialized client is passed', async () => {
78
85
expect ( screen . queryByText ( 'flagsReady' ) ) . toBeInTheDocument ( ) ;
79
86
} ) ;
80
87
} ) ;
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