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

fix: console.error #194

Merged
merged 6 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ Upgrading should be as easy as running yarn again with the new version, but we m

`startClient` option has been simplified. Now it will also work if you don't pass custom client with it. It defaults to `true`.

## Upgrade path from v4 -> v5

[FlagContext public interface changed](https://github.com/Unleash/proxy-client-react/commit/b783ef4016dbb881ac3d878cffaf5241b047cc35#diff-825c82ad66c3934257e0ee3e0511d9223db22e7ddf5de9cbdf6485206e3e02cfL20-R20). If you used FlagContext directly you may have to adjust your code slightly to accomodate the new type changes.


#### Note on v4.0.0:
The major release is driven by Node14 end of life and represents no other changes. From this version onwards we do not guarantee that this library will work server side with Node 14.

Expand Down
23 changes: 15 additions & 8 deletions src/useFlagContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { renderHook } from '@testing-library/react-hooks/native';
import { renderHook } from '@testing-library/react';
import { vi, test, expect } from 'vitest';
import FlagProvider from "./FlagProvider";
import { useFlagContext } from "./useFlagContext";

test("throws an error if used outside of a FlagProvider", () => {
const { result } = renderHook(() => useFlagContext());
test("logs an error if used outside of a FlagProvider", () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

expect(result.error).toEqual(
Error("This hook must be used within a FlagProvider")
);
renderHook(() => useFlagContext());
expect(consoleSpy).toHaveBeenCalledWith("useFlagContext() must be used within a FlagProvider");

consoleSpy.mockRestore();
});

test("does not throw an error if used inside of a FlagProvider", () => {
test("does not log an error if used inside of a FlagProvider", () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

const { result } = renderHook(() => useFlagContext(), { wrapper: FlagProvider });

expect(result.error).toBeUndefined();
expect(consoleSpy).not.toHaveBeenCalled();
expect(result.current).not.toBeNull();

consoleSpy.mockRestore();
});
65 changes: 58 additions & 7 deletions src/useFlagContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import { useContext } from 'react';
import FlagContext from './FlagContext';
import { useContext } from "react";
import FlagContext, { type IFlagContextValue } from "./FlagContext";
import type { UnleashClient } from "unleash-proxy-client";

const methods = {
on: (event: string, callback: Function, ctx?: any): UnleashClient => {
console.error("on() must be used within a FlagProvider");
return mockUnleashClient;
},
off: (event: string, callback?: Function): UnleashClient => {
console.error("off() must be used within a FlagProvider");
return mockUnleashClient;
},
updateContext: async () => {
console.error("updateContext() must be used within a FlagProvider");
return undefined;
},
isEnabled: () => {
console.error("isEnabled() must be used within a FlagProvider");
return false;
},
getVariant: () => {
console.error("getVariant() must be used within a FlagProvider");
return { name: "disabled", enabled: false };
}
};

const mockUnleashClient = {
...methods,
toggles: [],
impressionDataAll: {},
context: {},
storage: {},
start: () => {},
stop: () => {},
isReady: () => false,
getError: () => null,
getAllToggles: () => []
} as unknown as UnleashClient;

const defaultContextValue: IFlagContextValue = {
...methods,
client: mockUnleashClient,
flagsReady: false,
setFlagsReady: () => {
console.error("setFlagsReady() must be used within a FlagProvider");
},
flagsError: null,
setFlagsError: () => {
console.error("setFlagsError() must be used within a FlagProvider");
}
};

export function useFlagContext() {
const context = useContext(FlagContext);
if (!context) {
throw new Error('This hook must be used within a FlagProvider');
}
return context;
const context = useContext(FlagContext);
if (!context) {
console.error("useFlagContext() must be used within a FlagProvider");
return defaultContextValue;
}
return context;
}