|
1 | 1 | import React from 'react';
|
2 | 2 | import { render, screen, waitFor } from '@testing-library/react';
|
3 |
| -import useFlagsStatus from "./useFlagsStatus"; |
| 3 | +import useFlagsStatus from './useFlagsStatus'; |
4 | 4 | import FlagProvider from './FlagProvider';
|
5 |
| -import { EVENTS, type UnleashClient } from 'unleash-proxy-client'; |
| 5 | +import { EVENTS, UnleashClient } from 'unleash-proxy-client'; |
6 | 6 |
|
7 | 7 | const TestComponent = () => {
|
8 |
| - const { flagsReady } = useFlagsStatus(); |
| 8 | + const { flagsReady } = useFlagsStatus(); |
9 | 9 |
|
10 |
| - return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>; |
11 |
| -} |
| 10 | + return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>; |
| 11 | +}; |
12 | 12 |
|
13 | 13 | 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(), |
21 | 22 | } as unknown as UnleashClient;
|
22 | 23 |
|
23 | 24 | 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(); |
28 | 28 | }
|
| 29 | + }; |
29 | 30 |
|
30 |
| - mockClient.on = onEventHandler as typeof mockClient.on; |
| 31 | + mockClient.on = onEventHandler as typeof mockClient.on; |
31 | 32 |
|
32 |
| - const ui = ( |
33 |
| - <FlagProvider unleashClient={mockClient}> |
34 |
| - <TestComponent /> |
35 |
| - </FlagProvider> |
36 |
| - ) |
| 33 | + const ui = ( |
| 34 | + <FlagProvider unleashClient={mockClient}> |
| 35 | + <TestComponent /> |
| 36 | + </FlagProvider> |
| 37 | + ); |
37 | 38 |
|
38 |
| - render(ui); |
| 39 | + render(ui); |
39 | 40 |
|
40 |
| - await waitFor(() => { |
41 |
| - expect(screen.queryByText('flagsReady')).toBeInTheDocument(); |
42 |
| - }); |
| 41 | + await waitFor(() => { |
| 42 | + expect(screen.queryByText('flagsReady')).toBeInTheDocument(); |
| 43 | + }); |
43 | 44 | });
|
44 | 45 |
|
45 | 46 | // https://github.com/Unleash/proxy-client-react/issues/168
|
46 | 47 | 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); |
58 | 68 |
|
59 |
| - const ui = ( |
60 |
| - <FlagProvider unleashClient={mockClient}> |
61 |
| - <TestComponent /> |
62 |
| - </FlagProvider> |
63 |
| - ) |
| 69 | + const ui = ( |
| 70 | + <FlagProvider unleashClient={client}> |
| 71 | + <TestComponent /> |
| 72 | + </FlagProvider> |
| 73 | + ); |
64 | 74 |
|
65 |
| - render(ui); |
| 75 | + render(ui); |
66 | 76 |
|
67 |
| - await waitFor(() => { |
68 |
| - expect(screen.queryByText('flagsReady')).toBeInTheDocument(); |
69 |
| - }); |
| 77 | + await waitFor(() => { |
| 78 | + expect(screen.queryByText('flagsReady')).toBeInTheDocument(); |
| 79 | + }); |
70 | 80 | });
|
0 commit comments