Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/asyncWithLDProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,35 @@ describe('asyncWithLDProvider', () => {

expect(receivedNode).toHaveTextContent('{"testFlag":false}');
});

test('Provider keeps the latest flags even when it is remounted', async () => {
mockInitLDClient.mockImplementation(() => ({
ldClient: mockLDClient,
flags: rawFlags,
}));

mockLDClient.on.mockImplementationOnce((e: string, cb: (c: LDFlagChangeset) => void) => {
Comment thread
bufferings marked this conversation as resolved.
Outdated
cb({ 'test-flag': { current: false, previous: true }, 'another-test-flag': { current: false, previous: true } });
});
const options: LDOptions = {};

const LDProvider = await asyncWithLDProvider({ clientSideID, context, options });
Comment thread
bufferings marked this conversation as resolved.
Outdated

const { getByText: getByText1, unmount } = render(
<LDProvider>
<Consumer>{(value) => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
</LDProvider>,
);
const receivedNode1 = getByText1(/^Received:/);
expect(receivedNode1).toHaveTextContent('{"testFlag":false,"anotherTestFlag":false}');
unmount();

const { getByText: getByText2 } = render(
<LDProvider>
<Consumer>{(value) => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
</LDProvider>,
);
const receivedNode2 = getByText2(/^Received:/);
expect(receivedNode2).toHaveTextContent('{"testFlag":false,"anotherTestFlag":false}');
Comment thread
bufferings marked this conversation as resolved.
Outdated
});
});
6 changes: 4 additions & 2 deletions src/asyncWithLDProvider.tsx
Comment thread
bufferings marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export default async function asyncWithLDProvider(config: AsyncProviderConfig) {
);

const initialFlags = options?.bootstrap && options.bootstrap !== 'localStorage' ? options.bootstrap : fetchedFlags;
let currentFlags = initialFlags;
Comment thread
kinyoklion marked this conversation as resolved.
Outdated

const LDProvider = ({ children }: { children: ReactNode }) => {
const [ldData, setLDData] = useState(() => ({
unproxiedFlags: initialFlags,
...getFlagsProxy(ldClient, initialFlags, reactOptions, targetFlags),
unproxiedFlags: currentFlags,
...getFlagsProxy(ldClient, currentFlags, reactOptions, targetFlags),
Comment thread
kinyoklion marked this conversation as resolved.
}));

useEffect(() => {
Expand All @@ -54,6 +55,7 @@ export default async function asyncWithLDProvider(config: AsyncProviderConfig) {
if (Object.keys(updates).length > 0) {
setLDData(({ unproxiedFlags }) => {
const updatedUnproxiedFlags = { ...unproxiedFlags, ...updates };
currentFlags = updatedUnproxiedFlags;
Comment thread
kinyoklion marked this conversation as resolved.
Outdated

return {
unproxiedFlags: updatedUnproxiedFlags,
Comment thread
kinyoklion marked this conversation as resolved.
Outdated
Expand Down