-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
154 lines (119 loc) · 4.39 KB
/
test.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
describe('react-jsx', function () {
'use strict';
//
// Turn off the annoying jsx warnings by forcing production.
//
process.env.NODE_ENV = 'production';
var assume = require('assume')
, React = require('react')
, ReactDOM = require('react-dom/server')
, path = require('path')
, jsx = require('./')
, fs = require('fs')
, fixtures
, from;
//
// Read all the templates from the fixtures directly to make sure that we can
// render all the things correctly.
//
from = path.join(__dirname, 'fixtures');
fixtures = fs.readdirSync(from).reduce(function each(fixtures, file) {
if (path.extname(file) !== '.jsx') return fixtures;
fixtures[file.slice(0, -4)] = fs.readFileSync(path.join(from, file), 'UTF-8');
return fixtures;
}, {});
var Hello = React.createClass({
render: function render() {
return jsx.server(fixtures.hello)(this);
}
});
describe('.client', function () {
before(function () {
global.React = require('react');
global.ReactDOM = require('react-dom/server');
});
it('is exported as a function', function () {
assume(jsx.client).is.a('function');
});
it('can compile the `react.jsx` template', function () {
var client = jsx.client(fixtures.react);
assume(client).is.a('function');
});
it('can render the `react.jsx` to a DOM node', function () {
var client = jsx.client(fixtures.react);
assume(client).is.a('function');
assume(React.isValidElement(client())).is.true();
});
it('can introduce local variables', function () {
var client = jsx.client(fixtures.advanced);
assume(client).is.a('function');
assume(React.isValidElement(client({ defaultValue: 1 }))).is.true();
});
it('can also output HTML', function () {
var client = jsx.client(fixtures.react, { raw: true });
assume(client({}, { html: true })).equals('<div>content</div>');
});
});
describe('.server', function () {
before(function () {
delete global.React;
delete global.ReactDOM;
});
it('is exported as a function', function () {
assume(jsx.server).is.a('function');
});
it('returns React.createElement by default', function () {
var server = jsx.server(fixtures.react);
assume(React.isValidElement(server())).is.true();
});
it('can compile the `react.jsx` template', function () {
var server = jsx.server(fixtures.react);
assume(server).is.a('function');
});
it('can render the `react.jsx` to a raw template string', function () {
var server = jsx.server(fixtures.react, { raw: true });
assume(server).is.a('function');
assume(server({}, { html: true })).equals('<div>content</div>');
});
it('can render the `react.jsx` to a react template string', function () {
var server = jsx.server(fixtures.react);
assume(server).is.a('function');
assume(server({}, { html: true })).includes('data-reactid');
assume(server({}, { html: true })).includes('data-react-checksum');
assume(server({}, { html: true })).includes('content');
assume(server({}, { html: true })).includes('div');
});
it('can introduce local variables', function () {
var server = jsx.server(fixtures.advanced);
assume(server).is.a('function');
assume(server({ defaultValue: 1 }, { html: true })).includes('button');
});
it('can render components', function () {
var server = jsx.server(fixtures.component, { raw: true })
, result = server({
Hello: Hello,
namethings: function name(named) {
return named;
}
}, { html: true });
assume(result).equals('<div>Hello john</div>');
});
it('assumes data should be used as this if it has props', function () {
var server = jsx.server('<Hello name="lol" />', { raw: true })
, that = jsx.server(fixtures.this);
var Hello = React.createClass({
render: function () {
return that(this);
}
});
assume(server).is.a('function');
assume(server({ Hello: Hello }, { html: true })).equals('<div>lol</div>');
});
it('works with a custom `this`', function () {
var that = jsx.server(fixtures.this, { raw: true });
assume(that.call({
props: { name: 'john' }
}, {}, { html: true })).equals('<div>john</div>');
});
});
});