This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
12 changed files
with
5,649 additions
and
569 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": false, | ||
"printWidth": 120, | ||
"parser": "typescript", | ||
"trailingComma": "es5" | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`connectContext Should use the custom mergeContextWithProps function if provided 1`] = ` | ||
<div> | ||
<p> | ||
Sup my frand | ||
</p> | ||
<div> | ||
I have looked into context and I've seen the color is: | ||
green | ||
I also get my own props like | ||
sup | ||
! | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`connectContext should map context to props while preserving existing props 1`] = ` | ||
<div> | ||
<div> | ||
<p> | ||
Hello | ||
</p> | ||
<div> | ||
I have looked into context and I've seen the color is: | ||
green | ||
I also get my own props like | ||
test | ||
! | ||
</div> | ||
</div> | ||
</div> | ||
`; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as React from "react" | ||
import renderer from "react-test-renderer" | ||
import { connectContext, MergeContextWithProps } from "../" | ||
import "jest" | ||
|
||
interface ContentProps { | ||
color: string | ||
myProp: string | ||
} | ||
|
||
interface ContextValue { | ||
color: string | ||
} | ||
|
||
const Context = React.createContext<ContextValue>({ color: "red" }) | ||
const Content: React.SFC<ContentProps> = ({ children, color, myProp }) => ( | ||
<div> | ||
<p>{children}</p> | ||
<div> | ||
I have looked into context and I've seen the color is: {color} | ||
I also get my own props like {myProp}! | ||
</div> | ||
</div> | ||
) | ||
|
||
describe("connectContext", () => { | ||
it("should map context to props while preserving existing props", () => { | ||
const ConnectedContent = connectContext<ContextValue, ContentProps>( | ||
Context.Consumer | ||
)(Content) | ||
|
||
const App = () => ( | ||
<div> | ||
<ConnectedContent myProp="test">Hello</ConnectedContent> | ||
</div> | ||
) | ||
|
||
const tree = renderer | ||
.create( | ||
<Context.Provider value={{ color: "green" }}> | ||
<App /> | ||
</Context.Provider> | ||
) | ||
.toJSON() | ||
|
||
expect(tree).toMatchSnapshot() | ||
}) | ||
|
||
it("Should use the custom mergeContextWithProps function if provided", () => { | ||
const mergeContextWithProps: MergeContextWithProps< | ||
ContextValue, | ||
ContentProps | ||
> = (context, props) => { | ||
return ({ ...props, ...context }) | ||
} | ||
|
||
const ConnectedContent = connectContext( | ||
Context.Consumer, | ||
mergeContextWithProps | ||
)(Content) | ||
|
||
const AppWithPropsConflict = () => ( | ||
<ConnectedContent myProp="sup" color="red"> | ||
Sup my frand | ||
</ConnectedContent> | ||
) | ||
|
||
const tree = renderer | ||
.create( | ||
<Context.Provider value={{ color: "green" }}> | ||
<AppWithPropsConflict /> | ||
</Context.Provider> | ||
) | ||
.toJSON() | ||
|
||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as React from "react"; | ||
|
||
export type MergeContextWithProps<C, P> = (context: C, props: P) => C & P; | ||
|
||
function defaultMergeFn<C, P>(context: C, props: P): C & P { | ||
return Object.assign({}, context, props); | ||
} | ||
|
||
type Merge<L, R> = | ||
/** Mandatory properties */ | ||
Pick<L, Exclude<keyof L, keyof R>> | ||
/** Optional properties */ | ||
& Partial<R> | ||
|
||
export function connectContext<C, P>( | ||
ContextConsumer: React.Consumer<C>, | ||
mergeContextWithProps: MergeContextWithProps<C, P> = defaultMergeFn | ||
) { | ||
return function(Component: React.SFC<P>) { | ||
return function(props: Merge<P, C>) { | ||
// TODO remove `as any` when is available in the react typing definition | ||
if (!(ContextConsumer as any).currentValue as any instanceof Object) { | ||
throw new Error( | ||
`react-connect-context: the current value of the given context identifier is _not_ an object, | ||
and therefore cannot be passed as props to ${ | ||
Component.name | ||
}. Please check the value in the context | ||
and try again. | ||
More info: https://github.com/Contiamo/react-connect-context#gotchas` | ||
); | ||
} | ||
return ( | ||
<ContextConsumer> | ||
{context => <Component {...mergeContextWithProps(context, props as P)} />} | ||
</ContextConsumer> | ||
); | ||
}; | ||
}; | ||
} |
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
Oops, something went wrong.