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