Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in client #105

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jest.mock('./context', () => ({ Provider: 'Provider' }));
import * as React from 'react';
import { create } from 'react-test-renderer';
import { shallow } from 'enzyme';
import type { LDClient } from 'launchdarkly-js-client-sdk';
import { LDFlagChangeset, LDFlagSet, LDOptions, LDUser } from 'launchdarkly-js-client-sdk';
import initLDClient from './initLDClient';
import { LDReactOptions, EnhancedComponent, defaultReactOptions, ProviderConfig } from './types';
Expand All @@ -18,6 +19,7 @@ const mockLDClient = {
on: jest.fn((e: string, cb: () => void) => {
cb();
}),
allFlags: jest.fn().mockReturnValue({}),
};

describe('LDProvider', () => {
Expand Down Expand Up @@ -58,6 +60,61 @@ describe('LDProvider', () => {
expect(mockInitLDClient).toHaveBeenCalledWith(clientSideID, user, defaultReactOptions, options, undefined);
});

test('ld client is used if passed in', async () => {
const user: LDUser = { key: 'yus', name: 'yus ng' };
const options: LDOptions = { bootstrap: {} };
const ldClient = (await initLDClient(clientSideID, user, defaultReactOptions, options, undefined)).ldClient;
mockInitLDClient.mockClear();
const props: ProviderConfig = { clientSideID, ldClient };
const LaunchDarklyApp = (
<LDProvider {...props}>
<App />
</LDProvider>
);
const instance = create(LaunchDarklyApp).root.findByType(LDProvider).instance as EnhancedComponent;

await instance.componentDidMount();
expect(mockInitLDClient).not.toHaveBeenCalled();
});

test('ld client is used if passed in as promise', async () => {
const user1: LDUser = { key: 'yus', name: 'yus ng' };
const user2: LDUser = { key: 'launch', name: 'darkly' };
const options: LDOptions = { bootstrap: {} };
const ldClient: Promise<LDClient> = new Promise(async resolve =>
resolve((await initLDClient(clientSideID, user1, defaultReactOptions, options, undefined)).ldClient),
);
const props: ProviderConfig = { clientSideID, ldClient, user: user2 };
const LaunchDarklyApp = (
<LDProvider {...props}>
<App />
</LDProvider>
);
const instance = create(LaunchDarklyApp).root.findByType(LDProvider).instance as EnhancedComponent;

await instance.componentDidMount();
expect(mockInitLDClient).toBeCalledTimes(1);
expect(mockInitLDClient).toHaveBeenCalledWith(clientSideID, user1, defaultReactOptions, options, undefined);
});

test('ld client is created if passed in promise resolves as undefined', async () => {
const user: LDUser = { key: 'yus', name: 'yus ng' };
const options: LDOptions = { bootstrap: {} };
const ldClient: Promise<undefined> = new Promise(async resolve =>
resolve(undefined),
);
const props: ProviderConfig = { clientSideID, ldClient, user, options };
const LaunchDarklyApp = (
<LDProvider {...props}>
<App />
</LDProvider>
);
const instance = create(LaunchDarklyApp).root.findByType(LDProvider).instance as EnhancedComponent;

await instance.componentDidMount();
expect(mockInitLDClient).toHaveBeenCalledWith(clientSideID, user, defaultReactOptions, options, undefined);
});

test('ldClient bootstraps with empty flags', () => {
const user: LDUser = { key: 'yus', name: 'yus ng' };
const options: LDOptions = {
Expand Down
12 changes: 10 additions & 2 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LDClient, LDFlagSet, LDFlagChangeset } from 'launchdarkly-js-client-sdk
import { EnhancedComponent, ProviderConfig, defaultReactOptions } from './types';
import { Provider, LDContext as HocState } from './context';
import initLDClient from './initLDClient';
import { camelCaseKeys, getFlattenedFlagsFromChangeset } from './utils';
import { camelCaseKeys, fetchFlags, getFlattenedFlagsFromChangeset } from './utils';

/**
* The `LDProvider` is a component which accepts a config object which is used to
Expand Down Expand Up @@ -62,8 +62,16 @@ class LDProvider extends React.Component<ProviderConfig, HocState> implements En

initLDClient = async () => {
const { clientSideID, flags, options, user } = this.props;
let ldClient = await this.props.ldClient;
const reactOptions = this.getReactOptions();
const { flags: fetchedFlags, ldClient } = await initLDClient(clientSideID, user, reactOptions, options, flags);
let fetchedFlags;
if (ldClient) {
fetchedFlags = await fetchFlags(ldClient, reactOptions, flags);
} else {
const initialisedOutput = await initLDClient(clientSideID, user, reactOptions, options, flags);
fetchedFlags = initialisedOutput.flags;
ldClient = initialisedOutput.ldClient;
}
this.setState({ flags: fetchedFlags, ldClient });
this.subscribeToChanges(ldClient);
};
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export interface ProviderConfig {
* Otherwise, all flags will be requested and listened to.
*/
flags?: LDFlagSet;

/**
* Optionally, the LDClient can be initialised outside of the provider
* and passed in, instead of being initialised by the provider.
*/
ldClient?: LDClient | Promise<LDClient | undefined>;
}

/**
Expand Down