Skip to content

Commit

Permalink
port: Fix passing context on the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampo Kivistö committed Oct 12, 2017
1 parent 20c74e4 commit 4e31950
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
111 changes: 111 additions & 0 deletions packages/inferno-server/__tests__/props-context.spec.jsx
@@ -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>");
});
});
12 changes: 7 additions & 5 deletions packages/inferno-server/src/renderToString.ts
Expand Up @@ -37,12 +37,14 @@ function renderVNodeToString(
const instance = new type(props, context);
instance._blockSetState = false;
let childContext;
if (!isNullOrUndef(instance.getChildContext)) {
if (isFunction(instance.getChildContext)) {
childContext = instance.getChildContext();
}

if (!isNullOrUndef(childContext)) {
context = combineFrom(context, childContext);
if (isNullOrUndef(childContext)) {
childContext = context;
} else {
childContext = combineFrom(context, childContext);
}
if (instance.props === EMPTY_OBJ) {
instance.props = props;
Expand All @@ -68,12 +70,12 @@ function renderVNodeToString(
}
instance._blockRender = false;
}
const nextVNode = instance.render(props, instance.state, vNode.context);
const nextVNode = instance.render(props, instance.state, instance.context);
// In case render returns invalid stuff
if (isInvalid(nextVNode)) {
return "<!--!-->";
}
return renderVNodeToString(nextVNode, vNode, context, true);
return renderVNodeToString(nextVNode, vNode, childContext, true);
} else {
const nextVNode = type(props, context);

Expand Down

0 comments on commit 4e31950

Please sign in to comment.