Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 84093b1

Browse files
committed
implement render to html matcher
1 parent c76c3e1 commit 84093b1

File tree

8 files changed

+87
-38
lines changed

8 files changed

+87
-38
lines changed

spec/react/component/base_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def render
2525
@instance_data.join(" ")
2626
end
2727
end
28-
expect(render_to_html(Foo)).to eq("<span>working</span>")
29-
expect(render_to_html(Bar)).to eq("<span>working well</span>")
28+
expect(Foo).to render("<span>working</span>")
29+
expect(Bar).to render("<span>working well</span>")
3030
end
3131

3232
end

spec/react/component_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def render
120120
end
121121
end
122122

123-
expect(render_to_html(Foo)).to eq('<div>bar</div>')
123+
expect(Foo).to render('<div>bar</div>')
124124
end
125125

126126
it 'allows kernal method names like "format" to be used as state variable names' do
@@ -131,7 +131,7 @@ def render
131131
end
132132
end
133133

134-
expect(render_to_html(Foo)).to eq('<div>yes</div>')
134+
expect(Foo).to render('<div>yes</div>')
135135
end
136136

137137
it 'returns an observer with the bang method and no arguments' do
@@ -141,7 +141,7 @@ def render
141141
end
142142
end
143143

144-
expect(render_to_html(Foo)).to eq('<div>React::Observable</div>')
144+
expect(Foo).to render('<div>React::Observable</div>')
145145
end
146146

147147
it 'returns the current value of a state when written' do
@@ -152,15 +152,15 @@ def render
152152
end
153153
end
154154

155-
expect(render_to_html(Foo)).to eq('<div>bar</div>')
155+
expect(Foo).to render('<div>bar</div>')
156156
end
157157

158158
it 'can access an explicitly defined state`' do
159159
Foo.class_eval do
160160
define_state foo: :bar
161161
end
162162

163-
expect(render_to_html(Foo)).to eq('<div>bar</div>')
163+
expect(Foo).to render('<div>bar</div>')
164164
end
165165

166166
end
@@ -304,7 +304,7 @@ def render
304304
end
305305
end
306306

307-
expect(render_to_html(Foo)).to eq('<div>bar</div>')
307+
expect(Foo).to render('<div>bar</div>')
308308
end
309309

310310
it 'transforms state getter to Ruby object' do
@@ -320,7 +320,7 @@ def render
320320
end
321321
end
322322

323-
expect(render_to_html(Foo)).to eq('<div>Hello</div>')
323+
expect(Foo).to render('<div>Hello</div>')
324324
end
325325
end
326326

@@ -468,8 +468,8 @@ def render
468468
end
469469
end
470470

471-
expect(render_to_html(Foo, foo: 'lorem')).to eq('<div>lorem-bar</div>')
472-
expect(render_to_html(Foo)).to eq('<div>foo-bar</div>')
471+
expect(Foo).to render('<div>lorem-bar</div>').with_params(foo: 'lorem')
472+
expect(Foo).to render('<div>foo-bar</div>')
473473
end
474474
end
475475
end
@@ -596,7 +596,7 @@ def render
596596
end
597597
end
598598

599-
expect(render_to_html(Bar)).to eq('<div><div><span>astring</span></div></div>')
599+
expect(Bar).to render('<div><div><span>astring</span></div></div>')
600600
end
601601

602602
it 'builds single node in top-level render without providing a block' do
@@ -609,7 +609,7 @@ def render
609609
end
610610
end
611611

612-
expect(render_to_html(Foo)).to eq('<div></div>')
612+
expect(Foo).to render('<div></div>')
613613
end
614614

615615
it 'redefines `p` to make method missing work' do
@@ -627,7 +627,7 @@ def render
627627
end
628628

629629
markup = '<p class="foo"><p></p><div>lorem ipsum</div><p id="10"></p></p>'
630-
expect(render_to_html(Foo)).to eq(markup)
630+
expect(Foo).to render(markup)
631631
end
632632

633633
it 'only overrides `p` in render context' do

spec/react/dsl_spec.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
if opal?
44
describe 'the React DSL' do
5-
65
it "will turn the last string in a block into a element" do
76
stub_const 'Foo', Class.new
87
Foo.class_eval do
@@ -12,7 +11,7 @@ def render
1211
end
1312
end
1413

15-
expect(render_to_html(Foo)).to eq('<div>hello</div>')
14+
expect(Foo).to render('<div>hello</div>')
1615
end
1716

1817
it "has a .span short hand String method" do
@@ -24,7 +23,7 @@ def render
2423
end
2524
end
2625

27-
expect(render_to_html(Foo)).to eq('<div><span>hello</span><span>goodby</span></div>')
26+
expect(Foo).to render('<div><span>hello</span><span>goodby</span></div>')
2827
end
2928

3029
it "has a .br short hand String method" do
@@ -36,7 +35,7 @@ def render
3635
end
3736
end
3837

39-
expect(render_to_html(Foo)).to eq('<div><span>hello<br></span></div>')
38+
expect(Foo).to render('<div><span>hello<br></span></div>')
4039
end
4140

4241
it "has a .td short hand String method" do
@@ -48,7 +47,7 @@ def render
4847
end
4948
end
5049

51-
expect(render_to_html(Foo)).to eq('<table><tr><td>hello</td></tr></table>')
50+
expect(Foo).to render('<table><tr><td>hello</td></tr></table>')
5251
end
5352

5453
it "has a .para short hand String method" do
@@ -60,7 +59,7 @@ def render
6059
end
6160
end
6261

63-
expect(render_to_html(Foo)).to eq('<div><p>hello</p></div>')
62+
expect(Foo).to render('<div><p>hello</p></div>')
6463
end
6564

6665
it "will treat the component class name as a first class component name" do
@@ -78,7 +77,7 @@ def render
7877
end
7978
end
8079

81-
expect(render_to_html(Foo)).to eq('<span>a man walks into a bar</span>')
80+
expect(Foo).to render('<span>a man walks into a bar</span>')
8281
end
8382

8483
it "will treat the component class name as a first class component name" do
@@ -96,7 +95,7 @@ def render
9695
end
9796
end
9897

99-
expect(render_to_html(Foo)).to eq('<span>a man walks into a bar</span>')
98+
expect(Foo).to render('<span>a man walks into a bar</span>')
10099
end
101100

102101
it "can add class names by the haml .class notation" do
@@ -114,7 +113,7 @@ def render
114113
end
115114
end
116115

117-
expect(render_to_html(Foo)).to eq('<span class="the-class">a man walks into a bar</span>')
116+
expect(Foo).to render('<span class="the-class">a man walks into a bar</span>')
118117
end
119118

120119
it "can use the 'class' keyword for classes" do
@@ -126,7 +125,7 @@ def render
126125
end
127126
end
128127

129-
expect(render_to_html(Foo)).to eq('<span class="the-class">hello</span>')
128+
expect(Foo).to render('<span class="the-class">hello</span>')
130129
end
131130

132131
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
@@ -138,7 +137,7 @@ def render
138137
end
139138
end
140139

141-
expect(render_to_html(Foo)).to eq('<span>React::Element</span>')
140+
expect(Foo).to render('<span>React::Element</span>')
142141
end
143142

144143
it "can use the dangerously_set_inner_HTML param" do
@@ -150,7 +149,7 @@ def render
150149
end
151150
end
152151

153-
expect(render_to_html(Foo)).to eq('<div>Hello&nbsp;&nbsp;Goodby</div>')
152+
expect(Foo).to render('<div>Hello&nbsp;&nbsp;Goodby</div>')
154153
end
155154

156155
it "will remove all elements passed as params from the rendering buffer" do
@@ -173,7 +172,7 @@ def render
173172
end
174173
end
175174

176-
expect(render_to_html(Test)).to eq('<div><b>hello</b><b>hello</b></div>')
175+
expect(Test).to render('<div><b>hello</b><b>hello</b></div>')
177176
end
178177
end
179178
end

spec/react/param_declaration_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def render
1313
end
1414
end
1515

16-
expect(render_to_html(Foo, bar: 'biz')).to eq('<div>biz</div>')
16+
expect(Foo).to render('<div>biz</div>').with_params(bar: 'biz')
1717
end
1818

1919
it "can create and access a required param" do
@@ -26,7 +26,7 @@ def render
2626
end
2727
end
2828

29-
expect(render_to_html(Foo, foo: :bar)).to eq('<div>bar</div>')
29+
expect(Foo).to render('<div>bar</div>').with_params(foo: :bar)
3030
end
3131

3232
it "can create and access an optional params" do
@@ -43,7 +43,7 @@ def render
4343
end
4444
end
4545

46-
expect(render_to_html(Foo, foo1: :bar1, foo3: :bar3)).to eq('<div>bar1-no_bar2-bar3-no_bar4</div>')
46+
expect(Foo).to render('<div>bar1-no_bar2-bar3-no_bar4</div>').with_params(foo1: :bar1, foo3: :bar3)
4747
end
4848

4949
it 'can specify validation rules with the type option' do
@@ -67,7 +67,7 @@ def render
6767
end
6868
end
6969

70-
expect(render_to_html(Foo, foo1: 12, foo2: "string")).to eq('<div>12-string</div>')
70+
expect(Foo).to render('<div>12-string</div>').with_params(foo1: 12, foo2: "string")
7171
end
7272

7373
it 'logs error in warning if validation failed' do
@@ -162,8 +162,9 @@ def render
162162
"#{params.bar.kind}, #{params.baz[0].kind}"
163163
end
164164
end
165-
expect(render_to_html(
166-
Foo, foo: "", bar: {bazwoggle: 1}, baz: [{bazwoggle: 2}])).to eq('<span>1, 2</span>')
165+
166+
params = { foo: "", bar: { bazwoggle: 1 }, baz: [{ bazwoggle: 2 }] }
167+
expect(Foo).to render('<span>1, 2</span>').with_params(params)
167168
expect(`window.dummy_log`).to eq(["Warning: Failed propType: In component `Foo`\nProvided prop `foo` could not be converted to BazWoggle"])
168169
end
169170

@@ -174,7 +175,7 @@ def render
174175
params.foo
175176
end
176177
end
177-
expect(render_to_html(Foo, foo: lambda { 'works!' })).to eq('<span>works!</span>')
178+
expect(Foo).to render('<span>works!</span>').with_params(foo: lambda { 'works!' })
178179
end
179180

180181
it "will create a 'bang' (i.e. update) method if the type is React::Observable" do
@@ -189,7 +190,7 @@ def render
189190
end
190191
current_state = ""
191192
observer = React::Observable.new(current_state) { |new_state| current_state = new_state }
192-
expect(render_to_html(Foo, foo: observer)).to eq('<span>ha!</span>')
193+
expect(Foo).to render('<span>ha!</span>').with_params(foo: observer)
193194
expect(current_state).to eq("ha!")
194195
end
195196
end

spec/react/react_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def render
102102
end
103103
end
104104

105-
expect(render_to_html(Foo)).to eq("<div>20</div>")
105+
expect(Foo).to render("<div>20</div>")
106106
end
107107

108108
it "should match the instance cycle to ReactComponent life cycle" do

spec/react/tutorial/tutorial_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def render
1010

1111
describe 'An Example from the react.rb doc' do
1212
it 'produces the correct result' do
13-
expect(render_to_html(HelloMessage)).to eq('<div>Hello World!</div>')
13+
expect(HelloMessage).to render('<div>Hello World!</div>')
1414
end
1515
end
1616

@@ -24,7 +24,7 @@ def render
2424

2525
describe 'Adding state to a component (second tutorial example)' do
2626
it "produces the correct result" do
27-
expect(render_to_html(HelloMessage2)).to eq('<div>Hello @catmando</div>')
27+
expect(HelloMessage2).to render('<div>Hello @catmando</div>')
2828
end
2929

3030
it 'renders to the document' do

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def self.should_immediately_generate(opts={}, &block)
6565

6666
RSpec.configure do |config|
6767
config.include React::SpecHelpers
68+
config.include React::Testing::Matchers
6869
config.filter_run_excluding :ruby
6970
end
7071
end

spec/support/react/spec_helpers.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,52 @@ def expect_component_to_eventually(component_class, opts = {}, &block)
5959
check_block.call
6060
end
6161
end
62+
63+
module Testing
64+
module Matchers
65+
class RenderHTMLMatcher
66+
def initialize(expected)
67+
@expected = expected
68+
@params = {}
69+
end
70+
71+
def with_params(params)
72+
@params = params
73+
self
74+
end
75+
76+
def matches?(component)
77+
@component = component
78+
@actual = render_to_html
79+
@expected == @actual
80+
end
81+
82+
def failure_message
83+
failure_string
84+
end
85+
86+
def negative_failure_message
87+
failure_string(:negative)
88+
end
89+
90+
private
91+
92+
def render_to_html
93+
element = React.create_element(@component, @params)
94+
React.render_to_static_markup(element)
95+
end
96+
97+
def failure_string(negative = false)
98+
str = "expected '#{@component.name}' with params '#{@params}' to "
99+
str = str + "not " if negative
100+
str = str + "render '#{@expected}', but '#{@actual}' was rendered."
101+
str
102+
end
103+
end
104+
105+
def render(*args)
106+
RenderHTMLMatcher.new(*args)
107+
end
108+
end
109+
end
62110
end

0 commit comments

Comments
 (0)