-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
177 lines (165 loc) · 5.54 KB
/
Copy pathindex.js
File metadata and controls
177 lines (165 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var path = require('path');
var assert = require('chai').assert;
var reactRender = require('..');
var cache = require('../lib/cache');
var Component = require('../lib/Component');
var Hello = path.join(__dirname, 'test_components', 'Hello.js');
var ErrorThrowingComponent = path.join(__dirname, 'test_components', 'ErrorThrowingComponent.js');
describe('reactRender', function() {
beforeEach(function() {
cache.clear();
});
it('is a function', function() {
assert.isFunction(reactRender);
});
it('can render a component to static markup', function(done) {
reactRender({
path: Hello,
toStaticMarkup: true,
props: {
name: 'World'
}
}, function(err, output) {
assert.isNull(err);
assert.equal(output, '<div>Hello World</div>');
done();
});
});
it('can render a component to a string', function(done) {
reactRender({
path: Hello,
props: {
name: 'World'
}
}, function(err, output) {
assert.isNull(err);
assert.include(output, '<div');
assert.include(output, '><span');
assert.include(output, '>Hello </span><span');
assert.include(output, '>World</span>');
assert.include(output, '</div>');
done();
});
});
it('can render a component without props', function(done) {
reactRender({
path: Hello,
toStaticMarkup: true
}, function(err, output) {
assert.isNull(err);
assert.equal(output, '<div>Hello </div>');
done();
});
});
it('can parse props which are provided in a JSON serialized form', function(done) {
reactRender({
path: Hello,
toStaticMarkup: true,
serializedProps: '{"name": "World"}'
}, function(err, output) {
assert.isNull(err);
assert.equal(output, '<div>Hello World</div>');
done();
});
});
it('can require a component specified by a path and render it', function(done) {
reactRender({
path: path.join(__dirname, 'test_components', 'Hello'),
toStaticMarkup: true,
props: {
name: 'World'
}
}, function(err, output) {
assert.isNull(err);
assert.equal(output, '<div>Hello World</div>');
done();
});
});
it('should return an error if neither `component` and `path` have been defined', function(done) {
reactRender({
toStaticMarkup: true,
props: {
name: 'World'
}
}, function(err, output) {
assert.instanceOf(err, Error);
assert.include(err.stack, 'Component missing `path` property');
assert.isUndefined(output);
done();
});
});
it('should create `Component` instances when called', function(done) {
assert.isArray(cache._cache);
assert.equal(cache._cache.length, 0);
reactRender({
path: Hello
}, function() {
assert.equal(cache._cache.length, 1);
assert.instanceOf(cache._cache[0], Component);
assert.equal(cache._cache[0].component, require(Hello));
done();
});
});
it('should reuse Component instances when called', function() {
assert.isArray(cache._cache);
assert.equal(cache._cache.length, 0);
reactRender({component: Hello}, function() {});
assert.equal(cache._cache.length, 1);
assert.instanceOf(cache._cache[0], Component);
assert.equal(cache._cache[0].component, Hello);
reactRender({path: 'foo'}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
reactRender({path: 'foo'}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
reactRender({path: 'foo', props: {bar: 1}}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
reactRender({path: 'bar', serializedProps: {bar: 1}}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[2], Component);
assert.equal(cache._cache[2].opts.path, 'bar');
reactRender({path: 'foo', serializedProps: '{"bar": 1}'}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
reactRender({path: 'bar', serializedProps: '{"bar": 1}'}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[2], Component);
assert.equal(cache._cache[2].opts.path, 'bar');
});
it('passes up errors thrown during a component\'s rendering', function(done) {
reactRender({
path: ErrorThrowingComponent
}, function(err, output) {
assert.instanceOf(err, Error);
assert.include(err.stack, 'Error from inside ErrorThrowingComponent');
assert.include(err.stack, path.join(__dirname, 'test_components', 'ErrorThrowingComponent.js'));
assert.isUndefined(output);
done();
});
});
it('can accept an option denoting a path to react', function(done) {
reactRender({
path: Hello,
pathToReact: path.join(__dirname, '..', 'node_modules', 'react'),
toStaticMarkup: true
}, function(err, markup) {
assert.isNull(err);
assert.equal(markup, '<div>Hello </div>');
reactRender({
path: Hello,
pathToReact: path.join(__dirname, '..', '..')
}, function(err, markup) {
assert.isNotNull(err);
assert.instanceOf(err, Error);
assert.isUndefined(markup);
done();
});
});
});
});