Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port: Fix passing context on the server side
- Loading branch information
Sampo Kivistö
committed
Oct 12, 2017
1 parent
20c74e4
commit 4e31950
Showing
2 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
packages/inferno-server/__tests__/props-context.spec.jsx
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,111 @@ | ||
import { renderToStaticMarkup } from "inferno-server"; | ||
import Component from "inferno-component"; | ||
|
||
describe("SSR render() arguments", () => { | ||
|
||
class TestProvider extends Component { | ||
getChildContext() { | ||
return { testContext: 'context-works' } | ||
} | ||
render({ children }) { | ||
return children | ||
} | ||
} | ||
|
||
it("should have props as 1st argument", () => { | ||
class TestChild extends Component { | ||
render(props) { | ||
return <p>{props.testProps}</p> | ||
} | ||
} | ||
|
||
const output = renderToStaticMarkup(<TestChild testProps="props-works"/>); | ||
expect(output).toBe("<p>props-works</p>") | ||
}); | ||
|
||
it("should have state as 2nd argument", () => { | ||
class TestChild extends Component { | ||
constructor() { | ||
super() | ||
this.state = { testState: 'state-works' } | ||
} | ||
render(props, state) { | ||
return <p>{state.testState}</p> | ||
} | ||
} | ||
const output = renderToStaticMarkup(<TestChild/>); | ||
expect(output).toBe("<p>state-works</p>"); | ||
}); | ||
|
||
it("statefull has context as 3rd argument", () => { | ||
|
||
class TestChild extends Component { | ||
render(props, state, context) { | ||
return <p>{context.testContext}</p> | ||
} | ||
} | ||
|
||
const output = renderToStaticMarkup(( | ||
<TestProvider> | ||
<TestChild/> | ||
</TestProvider> | ||
)); | ||
expect(output).toBe("<p>context-works</p>"); | ||
}); | ||
|
||
it("stateless has context as 2nd argument", () => { | ||
|
||
function TestChild(props, context) { | ||
return <p>{context.testContext}</p> | ||
} | ||
|
||
const output = renderToStaticMarkup(( | ||
<TestProvider> | ||
<TestChild/> | ||
</TestProvider> | ||
)); | ||
expect(output).toBe("<p>context-works</p>"); | ||
}); | ||
|
||
it("nested stateless has context as 2nd argument", () => { | ||
|
||
function ChildWrapper(props, context) { | ||
return props.children | ||
} | ||
function TestChild(props, context) { | ||
return <p>{context.testContext}</p> | ||
} | ||
const output = renderToStaticMarkup(( | ||
<TestProvider> | ||
<ChildWrapper> | ||
<ChildWrapper> | ||
<TestChild/> | ||
</ChildWrapper> | ||
</ChildWrapper> | ||
</TestProvider> | ||
)); | ||
expect(output).toBe("<p>context-works</p>"); | ||
}); | ||
|
||
it("nested providers should have merged context", () => { | ||
class TestContext extends Component { | ||
getChildContext() { | ||
return { testContextWrap: 'context-wrap-works' } | ||
} | ||
render({ children }) { | ||
return children | ||
} | ||
} | ||
function TestChild(props, context) { | ||
return <p>{context.testContext}|{context.testContextWrap}</p> | ||
} | ||
const output = renderToStaticMarkup(( | ||
<TestProvider> | ||
<TestContext> | ||
<TestChild/> | ||
</TestContext> | ||
</TestProvider> | ||
)); | ||
expect(output).toBe("<p>context-works<!---->|<!---->context-wrap-works</p>"); | ||
}); | ||
}); |
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