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

Commit df12da3

Browse files
committed
Custom RSpec is first clazz
1 parent 84093b1 commit df12da3

File tree

4 files changed

+133
-48
lines changed

4 files changed

+133
-48
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module React
2+
module Testing
3+
module Matchers
4+
class RenderHTMLMatcher
5+
def initialize(expected)
6+
@expected = expected
7+
@params = {}
8+
end
9+
10+
def with_params(params)
11+
@params = params
12+
self
13+
end
14+
15+
def matches?(component)
16+
@component = component
17+
@actual = render_to_html
18+
@expected == @actual
19+
end
20+
21+
def failure_message
22+
failure_string
23+
end
24+
25+
def negative_failure_message
26+
failure_string(:negative)
27+
end
28+
29+
private
30+
31+
def render_to_html
32+
element = React.create_element(@component, @params)
33+
React.render_to_static_markup(element)
34+
end
35+
36+
def failure_string(negative = false)
37+
str = "expected '#{@component.name}' with params '#{@params}' to "
38+
str = str + "not " if negative
39+
str = str + "render '#{@expected}', but '#{@actual}' was rendered."
40+
str
41+
end
42+
end
43+
44+
def render(*args)
45+
RenderHTMLMatcher.new(*args)
46+
end
47+
end
48+
end
49+
end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require 'spec_helper'
2+
3+
if opal?
4+
describe React::Testing::Matchers::RenderHTMLMatcher do
5+
let(:component) {
6+
Class.new do
7+
include React::Component
8+
params do
9+
optional :string
10+
end
11+
def render
12+
div do
13+
span { params.string } if params.string
14+
'lorem'
15+
end
16+
end
17+
end
18+
}
19+
let(:expected) { '<div>lorem</div>' }
20+
let(:matcher) { described_class.new(expected) }
21+
22+
describe '#matches?' do
23+
it 'is truthy when rendered component html equals expected html' do
24+
expect(matcher.matches?(component)).to be_truthy
25+
end
26+
27+
it 'is falsey when rendered component html does not equal expected html' do
28+
matcher = described_class.new('foo')
29+
expect(matcher.matches?(component)).to be_falsey
30+
end
31+
end
32+
33+
describe '#with_params' do
34+
let(:expected) { '<div><span>str</span>lorem</div>' }
35+
36+
it 'renders the component with the given params' do
37+
matcher.with_params(string: 'str')
38+
expect(matcher.matches?(component)).to be_truthy
39+
end
40+
end
41+
42+
describe '#failure_message' do
43+
let(:expected) { '<div><span>str</span>lorem</div>' }
44+
45+
it 'includes the name of the component' do
46+
stub_const 'Foo', component
47+
matcher.matches?(Foo)
48+
expect(matcher.failure_message).to match(/expected 'Foo'/)
49+
end
50+
51+
it 'includes the params hash' do
52+
matcher.with_params(string: 'bar')
53+
matcher.matches?(component)
54+
expect(matcher.failure_message).to match(/with params '{"string"=>"bar"}'/)
55+
end
56+
57+
it 'includes the expected html value' do
58+
matcher.matches?(component)
59+
expect(matcher.failure_message).to match(/to render '#{expected}'/)
60+
end
61+
62+
it 'includes the actual html value' do
63+
actual = '<div>lorem<\/div>'
64+
matcher.matches?(component)
65+
expect(matcher.failure_message).to match(/, but '#{actual}' was rendered/)
66+
end
67+
68+
it 'does not include "to not render"' do
69+
matcher.matches?(component)
70+
expect(matcher.failure_message).to_not match(/to not render/)
71+
end
72+
end
73+
74+
describe '#negative_failure_message' do
75+
let(:expected) { '<div><span>str</span>lorem</div>' }
76+
77+
it 'includes "to not render"' do
78+
matcher.matches?(component)
79+
expect(matcher.negative_failure_message).to match(/to not render/)
80+
end
81+
end
82+
end
83+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def ruby?
1313

1414
if RUBY_ENGINE == 'opal'
1515
require 'reactive-ruby'
16+
require 'react/testing/matchers/render_html_matcher'
1617
require File.expand_path('../support/react/spec_helpers', __FILE__)
1718

1819
module Opal

spec/support/react/spec_helpers.rb

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,52 +59,4 @@ 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
11062
end

0 commit comments

Comments
 (0)