Skip to content

Commit aa6d36a

Browse files
committed
use client ready state
1 parent 046014a commit aa6d36a

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

src/FlagProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ const FlagProvider: FC<PropsWithChildren<IFlagProvider>> = ({
3939
const [flagsReady, setFlagsReady] = React.useState(
4040
Boolean(
4141
unleashClient
42-
? (customConfig?.bootstrap && customConfig?.bootstrapOverride !== false) || unleashClient.isReady()
42+
? (customConfig?.bootstrap && customConfig?.bootstrapOverride !== false) || unleashClient.isReady?.()
4343
: config.bootstrap && config.bootstrapOverride !== false
4444
)
4545
);
46-
const [flagsError, setFlagsError] = useState(null);
46+
const [flagsError, setFlagsError] = useState(client?.getError?.() || null);
4747

4848
useEffect(() => {
4949
if (!config && !unleashClient) {

src/useFlagStatus.test.tsx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,80 @@
11
import React from 'react';
22
import { render, screen, waitFor } from '@testing-library/react';
3-
import useFlagsStatus from "./useFlagsStatus";
3+
import useFlagsStatus from './useFlagsStatus';
44
import FlagProvider from './FlagProvider';
5-
import { EVENTS, type UnleashClient } from 'unleash-proxy-client';
5+
import { EVENTS, UnleashClient } from 'unleash-proxy-client';
66

77
const TestComponent = () => {
8-
const { flagsReady } = useFlagsStatus();
8+
const { flagsReady } = useFlagsStatus();
99

10-
return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>;
11-
}
10+
return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>;
11+
};
1212

1313
const mockClient = {
14-
on: vi.fn(),
15-
off: vi.fn(),
16-
start: vi.fn(),
17-
stop: vi.fn(),
18-
updateContext: vi.fn(),
19-
isEnabled: vi.fn(),
20-
getVariant: vi.fn(),
14+
on: vi.fn(),
15+
off: vi.fn(),
16+
start: vi.fn(),
17+
stop: vi.fn(),
18+
updateContext: vi.fn(),
19+
isEnabled: vi.fn(),
20+
getVariant: vi.fn(),
21+
isReady: vi.fn(),
2122
} as unknown as UnleashClient;
2223

2324
test('should initialize', async () => {
24-
const onEventHandler = (event: string, callback: () => void) => {
25-
if (event === 'ready') {
26-
callback();
27-
}
25+
const onEventHandler = (event: string, callback: () => void) => {
26+
if (event === 'ready') {
27+
callback();
2828
}
29+
};
2930

30-
mockClient.on = onEventHandler as typeof mockClient.on;
31+
mockClient.on = onEventHandler as typeof mockClient.on;
3132

32-
const ui = (
33-
<FlagProvider unleashClient={mockClient}>
34-
<TestComponent />
35-
</FlagProvider>
36-
)
33+
const ui = (
34+
<FlagProvider unleashClient={mockClient}>
35+
<TestComponent />
36+
</FlagProvider>
37+
);
3738

38-
render(ui);
39+
render(ui);
3940

40-
await waitFor(() => {
41-
expect(screen.queryByText('flagsReady')).toBeInTheDocument();
42-
});
41+
await waitFor(() => {
42+
expect(screen.queryByText('flagsReady')).toBeInTheDocument();
43+
});
4344
});
4445

4546
// https://github.com/Unleash/proxy-client-react/issues/168
4647
test('should start when already initialized client is passed', async () => {
47-
let initialized = false;
48-
const onEventHandler = (event: string, callback: () => void) => {
49-
if (event === EVENTS.READY && !initialized) {
50-
initialized = true;
51-
callback();
52-
}
53-
}
54-
55-
mockClient.on = onEventHandler as typeof mockClient.on;
56-
57-
await new Promise((resolve) => mockClient.on(EVENTS.READY, () => resolve));
48+
const client = new UnleashClient({
49+
url: 'http://localhost:4242/api',
50+
fetch: async () =>
51+
new Promise((resolve) => {
52+
setTimeout(() =>
53+
resolve({
54+
status: 200,
55+
ok: true,
56+
json: async () => ({
57+
toggles: [],
58+
}),
59+
headers: new Headers(),
60+
})
61+
);
62+
}),
63+
clientKey: '123',
64+
appName: 'test',
65+
});
66+
await client.start();
67+
expect(client.isReady()).toBe(true);
5868

59-
const ui = (
60-
<FlagProvider unleashClient={mockClient}>
61-
<TestComponent />
62-
</FlagProvider>
63-
)
69+
const ui = (
70+
<FlagProvider unleashClient={client}>
71+
<TestComponent />
72+
</FlagProvider>
73+
);
6474

65-
render(ui);
75+
render(ui);
6676

67-
await waitFor(() => {
68-
expect(screen.queryByText('flagsReady')).toBeInTheDocument();
69-
});
77+
await waitFor(() => {
78+
expect(screen.queryByText('flagsReady')).toBeInTheDocument();
79+
});
7080
});

0 commit comments

Comments
 (0)