Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
11 changes: 11 additions & 0 deletions __mocks__/contextComponent.js
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions __mocks__/testComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { createElement: h } = require('react')

module.exports = function() {
return h('div', null, 'I am a test component')
}
74 changes: 74 additions & 0 deletions __tests__/server.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
Loading