-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,80 @@ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import useFlagsStatus from "./useFlagsStatus"; | ||
import useFlagsStatus from './useFlagsStatus'; | ||
import FlagProvider from './FlagProvider'; | ||
import { EVENTS, type UnleashClient } from 'unleash-proxy-client'; | ||
import { EVENTS, UnleashClient } from 'unleash-proxy-client'; | ||
|
||
const TestComponent = () => { | ||
const { flagsReady } = useFlagsStatus(); | ||
const { flagsReady } = useFlagsStatus(); | ||
|
||
return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>; | ||
} | ||
return <div>{flagsReady ? 'flagsReady' : 'loading'}</div>; | ||
}; | ||
|
||
const mockClient = { | ||
on: vi.fn(), | ||
off: vi.fn(), | ||
start: vi.fn(), | ||
stop: vi.fn(), | ||
updateContext: vi.fn(), | ||
isEnabled: vi.fn(), | ||
getVariant: vi.fn(), | ||
on: vi.fn(), | ||
off: vi.fn(), | ||
start: vi.fn(), | ||
stop: vi.fn(), | ||
updateContext: vi.fn(), | ||
isEnabled: vi.fn(), | ||
getVariant: vi.fn(), | ||
isReady: vi.fn(), | ||
} as unknown as UnleashClient; | ||
|
||
test('should initialize', async () => { | ||
const onEventHandler = (event: string, callback: () => void) => { | ||
if (event === 'ready') { | ||
callback(); | ||
} | ||
const onEventHandler = (event: string, callback: () => void) => { | ||
if (event === 'ready') { | ||
callback(); | ||
} | ||
}; | ||
|
||
mockClient.on = onEventHandler as typeof mockClient.on; | ||
mockClient.on = onEventHandler as typeof mockClient.on; | ||
|
||
const ui = ( | ||
<FlagProvider unleashClient={mockClient}> | ||
<TestComponent /> | ||
</FlagProvider> | ||
) | ||
const ui = ( | ||
<FlagProvider unleashClient={mockClient}> | ||
<TestComponent /> | ||
</FlagProvider> | ||
); | ||
|
||
render(ui); | ||
render(ui); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('flagsReady')).toBeInTheDocument(); | ||
}); | ||
await waitFor(() => { | ||
expect(screen.queryByText('flagsReady')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
// https://github.com/Unleash/proxy-client-react/issues/168 | ||
test('should start when already initialized client is passed', async () => { | ||
let initialized = false; | ||
const onEventHandler = (event: string, callback: () => void) => { | ||
if (event === EVENTS.READY && !initialized) { | ||
initialized = true; | ||
callback(); | ||
} | ||
} | ||
|
||
mockClient.on = onEventHandler as typeof mockClient.on; | ||
|
||
await new Promise((resolve) => mockClient.on(EVENTS.READY, () => resolve)); | ||
const client = new UnleashClient({ | ||
url: 'http://localhost:4242/api', | ||
fetch: async () => | ||
new Promise((resolve) => { | ||
setTimeout(() => | ||
resolve({ | ||
status: 200, | ||
ok: true, | ||
json: async () => ({ | ||
toggles: [], | ||
}), | ||
headers: new Headers(), | ||
}) | ||
); | ||
}), | ||
clientKey: '123', | ||
appName: 'test', | ||
}); | ||
await client.start(); | ||
expect(client.isReady()).toBe(true); | ||
|
||
const ui = ( | ||
<FlagProvider unleashClient={mockClient}> | ||
<TestComponent /> | ||
</FlagProvider> | ||
) | ||
const ui = ( | ||
<FlagProvider unleashClient={client}> | ||
<TestComponent /> | ||
</FlagProvider> | ||
); | ||
|
||
render(ui); | ||
render(ui); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('flagsReady')).toBeInTheDocument(); | ||
}); | ||
await waitFor(() => { | ||
expect(screen.queryByText('flagsReady')).toBeInTheDocument(); | ||
}); | ||
}); |