This repository was archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdsl_spec.rb
More file actions
299 lines (258 loc) · 7.95 KB
/
dsl_spec.rb
File metadata and controls
299 lines (258 loc) · 7.95 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require 'spec_helper'
if opal?
describe 'the React DSL', type: :component do
context "render macro" do
it "can define the render method with the render macro with a html tag container" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
render(:div, class: :foo) do
"hello"
end
end
expect(Foo).to render_static_html('<div class="foo">hello</div>')
end
it "can define the render method with the render macro without a container" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
render do
"hello"
end
end
expect(Foo).to render_static_html('<span>hello</span>')
end
it "can define the render method with the render macro with a application defined container" do
stub_const 'Bar', Class.new(React::Component::Base)
Bar.class_eval do
param :p1
render { "hello #{params.p1}" }
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
render Bar, p1: "fred"
end
expect(Foo).to render_static_html('<span>hello fred</span>')
end
end
it "will turn the last string in a block into a element" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello" }
end
end
expect(Foo).to render_static_html('<div>hello</div>')
end
it "will pass converted props through event handlers" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
INPUT(data: {foo: 12}).on(:change) {}
end
end
expect(React::Server.render_to_static_markup(React.create_element(Foo))).to match(/<input data-foo="12"(\/)?>/)
end
it "will turn the last string in a block into a element" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
DIV { "hello" }
end
end
expect(Foo).to render_static_html('<div>hello</div>')
end
it "has a .span short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".span; "goodby".span }
end
end
expect(Foo).to render_static_html('<div><span>hello</span><span>goodby</span></div>')
end
it "has a .br short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".br }
end
end
expect(React::Server.render_to_static_markup(React.create_element(Foo)).gsub("<br/>", "<br>")).to eq('<div><span>hello<br></span></div>')
end
it "has a .td short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
table { tr { "hello".td } }
end
end
expect(Foo).to render_static_html('<table><tr><td>hello</td></tr></table>')
end
it "has a .para short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".para }
end
end
expect(Foo).to render_static_html('<div><p>hello</p></div>')
end
it 'can do a method call on a class name that is not a direct sibling' do
stub_const 'Mod', Module.new
stub_const 'Mod::NestedMod', Module.new
stub_const 'Mod::Comp', Class.new(React::Component::Base)
Mod::Comp.class_eval do
render { 'Mod::Comp' }
end
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
Mod::NestedMod::NestedComp.class_eval do
render do
Comp()
end
end
expect(Mod::NestedMod::NestedComp)
.to render_static_html('<span>Mod::Comp</span>')
end
it 'raises a meaningful error if a Constant Name is not actually a component' do
stub_const 'Mod', Module.new
stub_const 'Mod::NestedMod', Module.new
stub_const 'Mod::Comp', Class.new
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
Mod::NestedMod::NestedComp.class_eval do
backtrace :none
render do
Comp()
end
end
expect { React::Server.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)) }
.to raise_error('Comp does not appear to be a react component.')
end
it 'raises a method missing error' do
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
backtrace :none
render do
_undefined_method
end
end
expect { React::Server.render_to_static_markup(React.create_element(Foo)) }
.to raise_error(NoMethodError)
end
it "will treat the component class name as a first class component name" do
stub_const 'Mod::Bar', Class.new
Mod::Bar.class_eval do
include React::Component
def render
"a man walks into a bar"
end
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
def render
Mod::Bar()
end
end
expect(Foo).to render_static_html('<span>a man walks into a bar</span>')
end
it "can add class names by the haml .class notation" do
stub_const 'Mod::Bar', Class.new
Mod::Bar.class_eval do
include React::Component
collect_other_params_as :attributes
def render
"a man walks into a bar".span(params.attributes)
end
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
def render
Mod::Bar().the_class.other_class
end
end
expect(Foo).to render_static_html('<span class="other-class the-class">a man walks into a bar</span>')
end
it "can use the 'class' keyword for classes" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
span(class: "the-class") { "hello" }
end
end
expect(Foo).to render_static_html('<span class="the-class">hello</span>')
end
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
stub_const 'Foo', Class.new #(React::Component::Base)
Foo.class_eval do
include React::Component
def render
span(data: {size: 12}) { "hello".span.as_node.class.name }.as_node.render
end
end
expect(Foo)
.to render_static_html('<span data-size="12">React::Element</span>')
end
it "can use the dangerously_set_inner_HTML param" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(dangerously_set_inner_HTML: { __html: "Hello Goodby" })
end
end
expect(Foo).to render_static_html('<div>Hello Goodby</div>')
end
it 'should convert a hash param to hyphenated html attributes if in React::HASH_ATTRIBUTES' do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(data: { foo: :bar }, aria: { foo_bar: :foo })
end
end
expect(Foo)
.to render_static_html('<div data-foo="bar" aria-foo-bar="foo"></div>')
end
it 'should not convert a hash param to hyphenated html attributes if not in React::HASH_ATTRIBUTES' do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(title: { bar: :foo })
end
end
expect(Foo)
.to render_static_html(
'<div title="{"bar"=>"foo"}"></div>'
)
end
it "will remove all elements passed as params from the rendering buffer" do
stub_const 'X2', Class.new
X2.class_eval do
include React::Component
def render
div do
params[:ele].render
params[:ele].render
end
end
end
stub_const 'Test', Class.new
Test.class_eval do
include React::Component
def render
X2(ele: b { "hello" })
end
end
expect(Test).to render_static_html('<div><b>hello</b><b>hello</b></div>')
end
end
end