1
- import React from 'react' ;
2
- import { RouterContext } from 'react-router' ;
3
- import domServer from 'react-dom/server' ;
4
- import { MOVED_TEMPORARILY , MOVED_PERMANENTLY } from 'http-status-codes' ;
1
+ import { MOVED_PERMANENTLY , MOVED_TEMPORARILY } from 'http-status-codes' ;
5
2
import sinon from 'sinon' ;
6
3
import { assert } from 'chai' ;
7
4
import any from '@travi/any' ;
8
5
import Boom from 'boom' ;
9
6
import renderThroughReactRouter from '../../src/router-wrapper' ;
7
+ import * as defaultRenderFactory from '../../src/default-render-factory' ;
10
8
import * as routeMatcher from '../../src/route-matcher' ;
11
9
import * as dataFetcher from '../../src/data-fetcher' ;
12
10
@@ -25,8 +23,7 @@ suite('router-wrapper', () => {
25
23
sandbox . stub ( routeMatcher , 'default' ) ;
26
24
sandbox . stub ( dataFetcher , 'default' ) ;
27
25
sandbox . stub ( Boom , 'wrap' ) ;
28
- sandbox . stub ( React , 'createElement' ) ;
29
- sandbox . stub ( domServer , 'renderToString' ) ;
26
+ sandbox . stub ( defaultRenderFactory , 'default' ) ;
30
27
} ) ;
31
28
32
29
teardown ( ( ) => sandbox . restore ( ) ) ;
@@ -36,20 +33,36 @@ suite('router-wrapper', () => {
36
33
const reply = sinon . spy ( ) ;
37
34
const renderProps = any . simpleObject ( ) ;
38
35
const status = any . integer ( ) ;
39
- const context = any . simpleObject ( ) ;
40
- const rootComponent = any . simpleObject ( ) ;
41
36
const html = any . string ( ) ;
42
37
const response = any . string ( ) ;
38
+ const defaultRender = sinon . stub ( ) ;
43
39
routeMatcher . default . withArgs ( url , routes ) . resolves ( { renderProps, status} ) ;
44
40
dataFetcher . default . withArgs ( { renderProps, store, status} ) . resolves ( { renderProps, status} ) ;
45
- React . createElement . withArgs ( RouterContext , sinon . match ( renderProps ) ) . returns ( context ) ;
46
- React . createElement . withArgs ( Root , { request, store} ) . returns ( rootComponent ) ;
47
- domServer . renderToString . withArgs ( rootComponent ) . returns ( html ) ;
41
+ defaultRender . returns ( html ) ;
42
+ defaultRenderFactory . default . withArgs ( request , store , renderProps , Root ) . returns ( defaultRender ) ;
48
43
respond . withArgs ( reply , { renderedContent : { html} , store, status} ) . returns ( response ) ;
49
44
50
45
return assert . becomes ( renderThroughReactRouter ( request , reply , { routes, respond, Root, store} ) , response ) ;
51
46
} ) ;
52
47
48
+ test ( 'that response contains the custom-rendered content when a custom renderer is provided' , async ( ) => {
49
+ const respond = sinon . stub ( ) ;
50
+ const reply = sinon . spy ( ) ;
51
+ const renderProps = any . simpleObject ( ) ;
52
+ const status = any . integer ( ) ;
53
+ const response = any . string ( ) ;
54
+ const render = sinon . stub ( ) ;
55
+ const renderedContent = any . simpleObject ( ) ;
56
+ const defaultRender = ( ) => undefined ;
57
+ routeMatcher . default . withArgs ( url , routes ) . resolves ( { renderProps, status} ) ;
58
+ dataFetcher . default . withArgs ( { renderProps, store, status} ) . resolves ( { renderProps, status} ) ;
59
+ respond . withArgs ( reply , { renderedContent, store, status} ) . returns ( response ) ;
60
+ defaultRenderFactory . default . withArgs ( request , store , renderProps , Root ) . returns ( defaultRender ) ;
61
+ render . withArgs ( defaultRender ) . returns ( renderedContent ) ;
62
+
63
+ assert . equal ( await renderThroughReactRouter ( request , reply , { render, routes, respond, Root, store} ) , response ) ;
64
+ } ) ;
65
+
53
66
test ( 'that a temporary redirect results when a redirectLocation is defined with a 302 status' , ( ) => {
54
67
const respond = sinon . stub ( ) ;
55
68
const redirect = sinon . stub ( ) ;
0 commit comments