Skip to content

Commit f37a910

Browse files
committed
Merge branch 'feature/additional-context' of https://github.com/erikmueller/react-stdio into erikmueller-feature/additional-context
2 parents 4afdfac + 7db47dd commit f37a910

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

__mocks__/contextComponent.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { createElement: h } = require('react')
2+
let context = {}
3+
4+
const component = function() {
5+
context.test = true
6+
return h('div', null, 'I am a context component')
7+
}
8+
9+
component.context = context
10+
11+
module.exports = component

__mocks__/testComponent.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { createElement: h } = require('react')
2+
3+
module.exports = function() {
4+
return h('div', null, 'I am a test component')
5+
}

__tests__/server.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const { spawn } = require('child_process')
2+
const path = require('path')
3+
4+
describe('server', function() {
5+
let proc
6+
7+
beforeEach(function() {
8+
proc = spawn(path.join(__dirname, '..', 'bin', 'react-stdio'), {
9+
stdio: 'pipe'
10+
})
11+
})
12+
13+
afterEach(function() {
14+
proc.kill()
15+
})
16+
17+
it('throws an error when component is missing', function(done) {
18+
proc.stdin.write(JSON.stringify({}))
19+
20+
proc.stdout.once('data', function(out) {
21+
expect(JSON.parse(out).error).toEqual('Missing { component } in request')
22+
23+
done()
24+
})
25+
})
26+
27+
it('throws an error when component cannot be found', function(done) {
28+
proc.stdin.write(JSON.stringify({ component: 'component.js' }))
29+
30+
proc.stdout.once('data', function(out) {
31+
expect(JSON.parse(out).error).toEqual(
32+
'Cannot load component: component.js'
33+
)
34+
35+
done()
36+
})
37+
})
38+
39+
it('renders the component', function(done) {
40+
proc.stdin.write(
41+
JSON.stringify({
42+
component: path.join(__dirname, '..', '__mocks__', 'testComponent.js')
43+
})
44+
)
45+
46+
proc.stdout.once('data', function(out) {
47+
expect(JSON.parse(out).html).toMatch('I am a test component')
48+
49+
done()
50+
})
51+
})
52+
53+
it('renders a component and exposes additional context', function(done) {
54+
proc.stdin.write(
55+
JSON.stringify({
56+
component: path.join(
57+
__dirname,
58+
'..',
59+
'__mocks__',
60+
'contextComponent.js'
61+
)
62+
})
63+
)
64+
65+
proc.stdout.once('data', function(out) {
66+
const result = JSON.parse(out)
67+
68+
expect(result.html).toMatch('I am a context component')
69+
expect(result.context).toEqual({ test: true })
70+
71+
done()
72+
})
73+
})
74+
})

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"server.js"
1616
],
1717
"scripts": {
18-
"release": "node ./scripts/release.js"
18+
"release": "node ./scripts/release.js",
19+
"test": "jest"
1920
},
2021
"dependencies": {
2122
"JSONStream": "^1.0.7",
@@ -25,6 +26,7 @@
2526
"react-dom": "^15.0.1"
2627
},
2728
"devDependencies": {
29+
"jest": "^21.2.1",
2830
"readline-sync": "^1.4.1"
2931
}
3032
}

server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,22 @@ function handleRequest(workingDir, request, callback) {
7272

7373
invariant(component != null, "Cannot load component: %s", componentPath);
7474

75-
render(React.createElement(component, props), callback);
75+
render(React.createElement(component, props), function(err, html) {
76+
callback(err, html, component.context);
77+
});
7678
}
7779

7880
function createRequestHandler(workingDir) {
7981
return function(request, callback) {
8082
try {
81-
handleRequest(workingDir, request, function(error, html) {
83+
handleRequest(workingDir, request, function(error, html, context) {
8284
if (error) {
8385
callback(error);
8486
} else if (typeof html !== "string") {
8587
// Crash the server process.
8688
callback(new Error("Render method must return a string"));
8789
} else {
88-
callback(null, JSON.stringify({ html: html }));
90+
callback(null, JSON.stringify({ html: html, context: context }));
8991
}
9092
});
9193
} catch (error) {

0 commit comments

Comments
 (0)