-
Notifications
You must be signed in to change notification settings - Fork 6
/
spec.js
190 lines (152 loc) · 6.44 KB
/
spec.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
var assert = require('assert');
var React = require('react');
var ReactDOM = require('react-dom/server');
var Interpolate = React.createFactory(require('./'));
var render = ReactDOM.renderToString;
var createReactClass = require('create-react-class');
// hack: raise React console warnings as failed assertions
console.error = function(message) {
assert(false, message);
};
assert.matches = function(regexp, value, message) {
if (!regexp.test(value)) {
assert.fail(value, regexp, message, '=~');
}
};
assert.doesNotMatch = function(regexp, value, message) {
if (regexp.test(value)) {
assert.fail(value, regexp, message, '!~');
}
};
describe('The Interpolate component', function() {
it('does not mutate props', function() {
var props = { className: 'foo', with: { name: 'bar', value: 'baz' }, children: '%(name)s: %(value)s' };
var markup = render(Interpolate(props));
assert.deepEqual(props, { className: 'foo', with: { name: 'bar', value: 'baz' }, children: '%(name)s: %(value)s' });
props.unsafe = true;
markup = render(Interpolate(props));
assert.deepEqual(props, { className: 'foo', with: { name: 'bar', value: 'baz' }, children: '%(name)s: %(value)s', unsafe: true });
});
it('transfers those props to the container component that are not interpolation arguments', function() {
var props = { className: 'foo', with: { name: 'bar', value: 'baz' } };
var format = '%(name)s: %(value)s';
var markup = render(Interpolate(props, format));
assert.matches(/^<span [^>]*?class="foo"/, markup);
assert.doesNotMatch(/\sname="/, markup);
assert.doesNotMatch(/\svalue="/, markup);
props.unsafe = true;
markup = render(Interpolate(props, format));
assert.matches(/^<span [^>]*?class="foo"/, markup);
assert.doesNotMatch(/\sname="/, markup);
assert.doesNotMatch(/\svalue="/, markup);
});
it('renders a `span` HTML element as container by default', function() {
var markup = render(Interpolate(null, 'bar'));
assert.matches(/^<span\s/, markup);
});
it('allows a custom container component to be set as prop', function() {
var markup = render(Interpolate({ component: 'section' }, 'bar'));
assert.matches(/^<section\s/, markup);
});
it('allows a `format` prop instead of children', function() {
assert.doesNotThrow(function() {
render(Interpolate({ format: 'foo' }));
});
});
it('handles using the same named interpolation argument more than once correctly', function() {
var props = { with: { arg: 'test' } };
var format = 'foo %(arg)s bar %(arg)s baz';
var markup = render(Interpolate(props, format));
assert.matches(/foo.*?test.*?bar.*?test.*?baz/, markup);
props.unsafe = true;
markup = render(Interpolate(props, format));
assert.matches(/foo test bar test baz/, markup);
assert.doesNotMatch(/undefined/, markup);
});
it('has a working example in the README', function() {
var React = require('react');
var Interpolate = React.createFactory(require('./'));
var MyApp = createReactClass({
render: function() {
var props = {
with: {
firstName: React.createElement('strong', null, 'Paul'),
age: 13,
unit: 'years'
},
component: 'p', // default is a <span>
className: 'foo'
};
var format = '%(firstName)s is %(age)s %(unit)s old.';
return React.createElement('div', null, Interpolate(props, format));
}
});
var markup = render(React.createFactory(MyApp)());
assert.matches(
/<div[^>]+><p.*?class="foo"[^>]*><strong>Paul<\/strong>.*? is .*?13.*? .*?years.*? old\..*?<\/p><\/div>/,
markup
);
});
it('rejects everything as format that is not a string', function() {
// How can something like this be properly testet?
[undefined, null, {}, [], function() {}, new Date, true, 123].forEach(function(object) {
assert.throws(function() { render(Interpolate(null, object)); }, /invariant/i);
assert.throws(function() { render(Interpolate({ format: object })); }, /invariant/i);
});
});
describe('with format set as child', function() {
it('interpolates properly', function() {
var props = {
with: { foo: 'bar', number: 42, comp: React.createElement('i', null, 'baz') }
};
var format = 'lala %(foo)s lulu %(comp)s lili %(number)s lele';
var markup = render(Interpolate(props, format));
assert.matches(/lala .*?bar.*? lulu .*?baz.*? lili .*?42.*? lele/, markup);
assert.doesNotMatch(/%\(|\)s|foo|comp|number/, markup);
});
it('interpolates properly when child is an empty string', function() {
var props = { component: 'div' };
var markup = render(Interpolate(props, ''));
assert.matches(/<div[^>]*><\/div>/, markup);
});
});
describe('with format set as prop', function() {
it('interpolates properly', function() {
var props = {
with: { foo: 'bar', number: 42, comp: React.createElement('i', null, 'baz') },
format: 'lala %(foo)s lulu %(comp)s lili %(number)s lele'
};
var markup = render(Interpolate(props));
assert.matches(/lala .*?bar.*? lulu .*?baz.*? lili .*?42.*? lele/, markup);
assert.doesNotMatch(/%\(|\)s|foo|comp|number/, markup);
});
it('interpolates properly when prop is an empty string', function() {
var props = { component: 'div', format: '' };
var markup = render(Interpolate(props));
assert.matches(/<div[^>]*><\/div>/, markup);
});
});
it('escapes HTML markup in the format string by default', function() {
var format = 'foo <script>alert("Danger!");</script> bar';
var markup = render(Interpolate(null, format));
assert.doesNotMatch(/<\/?script>/, markup);
});
describe('when providing an `unsafe` prop set to `true`', function() {
it('renders HTML markup present in the format string', function() {
var format = 'foo <script>alert("%(alert)s");</script> bar';
var markup = render(Interpolate({ unsafe: true, alert: 'Danger!' }, format));
assert.matches(/<script>alert\("Danger!"\);<\/script>/, markup);
});
it('throws an error when interpolating React components', function() {
var format = 'foo <p>%(para)s</p> bar';
assert.throws(function() {
render(
Interpolate(
{ unsafe: true, para: React.createElement('span', null, 'baz') },
format
)
);
}, /cannot interpolate/i);
});
});
});