Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose additional context when rendering #13

Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
@@ -0,0 +1,11 @@
const { createElement: h } = require('react')
let context = {}

const component = function() {
context.test = true
return h('div', null, 'I am a context component')
}

component.context = context

module.exports = component
@@ -0,0 +1,5 @@
const { createElement: h } = require('react')

module.exports = function() {
return h('div', null, 'I am a test component')
}
@@ -0,0 +1,74 @@
const { spawn } = require('child_process')
const path = require('path')

describe('server', function() {
let proc

beforeEach(function() {
proc = spawn(path.join(__dirname, '..', 'bin', 'react-stdio'), {
stdio: 'pipe'
})
})

afterEach(function() {
proc.kill()
})

it('throws an error when component is missing', function(done) {
proc.stdin.write(JSON.stringify({}))

proc.stdout.once('data', function(out) {
expect(JSON.parse(out).error).toEqual('Missing { component } in request')

done()
})
})

it('throws an error when component cannot be found', function(done) {
proc.stdin.write(JSON.stringify({ component: 'component.js' }))

proc.stdout.once('data', function(out) {
expect(JSON.parse(out).error).toEqual(
'Cannot load component: component.js'
)

done()
})
})

it('renders the component', function(done) {
proc.stdin.write(
JSON.stringify({
component: path.join(__dirname, '..', '__mocks__', 'testComponent.js')
})
)

proc.stdout.once('data', function(out) {
expect(JSON.parse(out).html).toMatch('I am a test component')

done()
})
})

it('renders a component and exposes additional context', function(done) {
proc.stdin.write(
JSON.stringify({
component: path.join(
__dirname,
'..',
'__mocks__',
'contextComponent.js'
)
})
)

proc.stdout.once('data', function(out) {
const result = JSON.parse(out)

expect(result.html).toMatch('I am a context component')
expect(result.context).toEqual({ test: true })

done()
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.