|
| 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