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
/
Copy pathdsl_spec.rb
323 lines (299 loc) · 8.54 KB
/
dsl_spec.rb
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
require 'spec_helper'
describe 'the React DSL', js: true do
context "render macro" do
it "can define the render method with the render macro with a html tag container" do
mount 'Foo' do
class Foo
include React::Component
render(:div, class: :foo) do
"hello"
end
end
end
expect(page.body[-60..-19]).to include('<div class="foo">hello</div>')
end
it "can define the render method with the render macro without a container" do
mount 'Foo' do
class Foo
include React::Component
render do
"hello"
end
end
end
expect(page.body[-60..-19]).to include('<span>hello</span>')
end
it "can define the render method with the render macro with a application defined container" do
on_client do
class Bar < React::Component::Base
param :p1
render { "hello #{params.p1}" }
end
class Foo < React::Component::Base
render Bar, p1: "fred"
end
end
mount 'Foo'
expect(page.body[-60..-19]).to include('<span>hello fred</span>')
end
end
it "will turn the last string in a block into a element" do
mount 'Foo' do
class Foo
include React::Component
def render
div { "hello" }
end
end
end
expect(page.body[-60..-19]).to include('<div>hello</div>')
end
it "in prerender will pass converted props through event handlers" do
client_option render_on: :both
mount 'Foo' do
class Foo
include React::Component
def render
INPUT(data: {foo: 12}).on(:change) {}
end
end
end
expect(page.body).to match(/<input data-foo="12" data-reactroot="" \/>/)
end
it "will turn the last string in a block into a element" do
mount 'Foo' do
class Foo
include React::Component
def render
DIV { "hello" }
end
end
end
expect(page.body[-60..-19]).to include('<div>hello</div>')
end
it "has a .span short hand String method" do
mount 'Foo' do
class Foo
include React::Component
def render
div { "hello".span; "goodby".span }
end
end
end
expect(page.body[-70..-19]).to include('<div><span>hello</span><span>goodby</span></div>')
end
it "in prerendering has a .br short hand String method" do
client_option render_on: :both
client_option raise_on_js_errors: :off
mount 'Foo' do
class Foo
include React::Component
def render
div { "hello".br }
end
end
end
expect(page.body[-285..-233]).to match(/(<div data-reactroot=""|<div)><span>hello<(br|br\/|br \/)><\/span><\/div>/)
end
it "has a .td short hand String method" do
mount 'Foo' do
class Foo
include React::Component
def render
table {
tbody {
tr { "hello".td }
}
}
end
end
end
expect(page.body[-90..-19]).to include('<table><tbody><tr><td>hello</td></tr></tbody></table>')
end
it "has a .para short hand String method" do
mount 'Foo' do
class Foo
include React::Component
def render
div { "hello".para }
end
end
end
expect(page.body[-60..-19]).to include('<div><p>hello</p></div>')
end
it 'can do a method call on a class name that is not a direct sibling' do
# this test is failing because Comp() is not serialized/compiled to a method call,
# but instead to a $scope.get("Comp"); which returns the Component Class Mod::Comp
# instead it sould serialize/compile to a $scope.$Comp();
# this is a bug in unparser, workaround is to pass a argument to comp
mount 'Mod::NestedMod::NestedComp' do
module Mod
class Comp
include React::Component
param :test
def render
"Mod::Comp"
end
end
module NestedMod
class NestedComp
include React::Component
def render
Comp(test: 'string')
end
end
end
end
end
expect(page.body[-60..-19]).to include('<span>Mod::Comp</span>')
end
it 'raises a meaningful error if a Constant Name is not actually a component' do
client_option raise_on_js_errors: :off
mount 'Mod::NestedMod::NestedComp' do
module Mod
module NestedMod
class NestedComp < React::Component::Base
backtrace :none
render do
Comp(test: 'string')
end
end
end
class Comp; end
end
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/Comp does not appear to be a react component./)
end
it 'raises a method missing error' do
client_option render_on: :both
client_option raise_on_js_errors: :off
expect_evaluate_ruby do
class Foo < React::Component::Base
backtrace :none
render do
_undefined_method
end
end
begin
React::Test::Utils.render_component_into_document(Foo)
'not raised'
rescue NoMethodError
'raised for sure!'
end
end.to eq('raised for sure!')
end
it "will treat the component class name as a first class component name" do
mount 'Foo' do
module Mod
class Bar
include React::Component
def render
"a man walks into a bar"
end
end
end
class Foo < React::Component::Base
def render
Mod::Bar()
end
end
end
expect(page.body[-60..-19]).to include('<span>a man walks into a bar</span>')
end
it "can add class names by the haml .class notation" do
mount 'Foo' do
module Mod
class Bar
include React::Component
collect_other_params_as :attributes
def render
"a man walks into a bar".span(params.attributes)
end
end
end
class Foo < React::Component::Base
def render
Mod::Bar().the_class.other_class
end
end
end
expect(page.body[-90..-19]).to include('<span class="other-class the-class">a man walks into a bar</span>')
end
it "can use the 'class' keyword for classes" do
mount 'Foo' do
class Foo
include React::Component
def render
span(class: "the-class") { "hello" }
end
end
end
expect(page.body[-60..-19]).to include('<span class="the-class">hello</span>')
end
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
mount 'Foo' do
class Foo
include React::Component
def render
span(data: {size: 12}) { "hello".span.as_node.class.name }.as_node.render
end
end
end
expect(page.body[-80..-19]).to include('<span data-size="12">React::Element</span>')
end
it "can use the dangerously_set_inner_HTML param" do
mount 'Foo' do
class Foo
include React::Component
def render
div(dangerously_set_inner_HTML: { __html: "Hello and Goodby" })
end
end
end
expect(page.body[-60..-19]).to include('<div>Hello and Goodby</div>')
end
it 'should convert a hash param to hyphenated html attributes if in React::HASH_ATTRIBUTES' do
mount 'Foo' do
class Foo
include React::Component
def render
div(data: { foo: :bar }, aria: { label: "title" })
end
end
end
expect(page.body[-70..-19]).to include('<div data-foo="bar" aria-label="title"></div>')
end
it 'should not convert a hash param to hyphenated html attributes if not in React::HASH_ATTRIBUTES' do
mount 'Foo' do
class Foo
include React::Component
def render
div(title: { bar: :foo })
end
end
end
expect(page.body[-80..-19]).to include('<div title="{"bar"=>"foo"}"></div>')
end
it "will remove all elements passed as params from the rendering buffer" do
mount 'Foo' do
class X2
include React::Component
param :ele
def render
div do
params[:ele].render
params[:ele].render
end
end
end
class Foo
include React::Component
def render
X2(ele: b { "hello" })
end
end
end
expect(page.body[-60..-19]).to include('<div><b>hello</b><b>hello</b></div>')
end
end