Skip to content

Commit

Permalink
feat: Set displayName of Context and Provider (#79) (#80)
Browse files Browse the repository at this point in the history
Closes #79
  • Loading branch information
Tobias Walle authored and diegohaz committed Jun 29, 2019
1 parent 127b8ca commit fc6595f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createUseContext<P, V>(
) {
const Context = React.createContext(defaultValue as V);

const Provider = (props: { children?: React.ReactNode } & P) => {
const Provider: React.FunctionComponent<P> = props => {
const value = useValue(props);
// createMemoInputs won't change between renders
const memoizedValue = createMemoInputs
Expand All @@ -34,6 +34,11 @@ function createUseContext<P, V>(
);
};

if (useValue.name) {
Context.displayName = `${useValue.name}.Context`;
Provider.displayName = `${useValue.name}.Provider`;
}

const useContext = () => React.useContext(Context);
useContext.Context = Context;
useContext.Provider = Provider;
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,15 @@ test("provider props", () => {
fireEvent.click(getByText("Increment"));
expect(getByText("11")).toBeDefined();
});

test("displayName with named hook", () => {
const Container = createContainer(useCounter);
expect(Container.Provider.displayName).toBe("useCounter.Provider");
expect(Container.Context.displayName).toBe("useCounter.Context");
});

test("displayName with anonymous hook", () => {
const Container = createContainer(() => {});
expect(Container.Provider.displayName).toBeUndefined();
expect(Container.Context.displayName).toBeUndefined();
});

0 comments on commit fc6595f

Please sign in to comment.