Skip to content

Commit 7db47dd

Browse files
committed
expose additional context when rendering
1 parent bd1fe47 commit 7db47dd

File tree

7 files changed

+4476
-4
lines changed

7 files changed

+4476
-4
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

__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+
})

0 commit comments

Comments
 (0)