Skip to content

Commit be3f888

Browse files
committed
Normalize on render_component for view specs
1 parent 122937e commit be3f888

File tree

8 files changed

+212
-223
lines changed

8 files changed

+212
-223
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
require "rails_helper"
22

33
RSpec.describe ClipboardCopy, type: :view do
4-
def render(text, &block)
5-
Capybara.string(described_class.new(text:).call)
6-
end
7-
84
it "renders the clipboard copy button" do
9-
expect(render("Hello")).to have_css("button", text: "Copied!")
5+
render_component(text: "Hello")
6+
7+
expect(rendered).to have_css("button", text: "Copied!")
108
end
119

1210
it "gracefully handles text containing ERB when processed by ERB" do
13-
text = "<%= \"color\" %>"
14-
rendered = described_class.new(text:).call
11+
render_component(text: "<%= \"color\" %>")
12+
1513
processed = ERB.new(rendered).result(binding)
1614

17-
expect(Capybara.string(processed)).to have_css("[data-value*=color]")
15+
expect(processed).to have_css("[data-value*=color]")
1816
end
1917
end
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
require "rails_helper"
22

33
RSpec.describe CodeBlock::AppFileRun, type: :view do
4-
def render_page(*, **)
5-
Capybara.string(render(*, **))
6-
end
7-
84
before do
95
allow(view).to receive(:headers).and_return({"Content-Type" => "text/html"})
106
end
117

128
it "renders contents of file" do
13-
page = render_page(CodeBlock::AppFileRun.new("spec/fixtures/code_block/app_file/example.rb"))
14-
expect(page).to have_content(<<~RUBY.strip)
9+
render_component("spec/fixtures/code_block/app_file/example.rb")
10+
11+
expect(rendered).to have_content(<<~RUBY.strip)
1512
class FixturesCodeBlockAppFileExample
1613
def add(m, n)
1714
m + n
1815
end
1916
end
2017
RUBY
21-
expect(page).to have_content("spec/fixtures/code_block/app_file/example.rb")
18+
19+
expect(rendered).to have_content("spec/fixtures/code_block/app_file/example.rb")
2220
end
2321
end

spec/views/components/color_schemes/css_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
RSpec.describe ColorSchemes::Css do
44
describe "#call" do
55
it "renders valid css" do
6-
component = described_class.new(color_scheme: FactoryBot.build(:color_scheme))
7-
86
validator = W3CValidators::CSSValidator.new
9-
validator_results = validator.validate_text(component.call)
7+
8+
render_component(color_scheme: FactoryBot.build(:color_scheme))
9+
10+
validator_results = validator.validate_text(rendered)
1011

1112
expect(validator_results.is_valid?).to be(true), "Expected feed to be valid, but got: #{validator_results.errors.map(&:to_s).join(", ")}"
1213
end
Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
11
require "rails_helper"
22

33
RSpec.describe Markdown::Article, type: :view do
4-
describe ".call" do
5-
before do
6-
allow(view).to receive(:headers).and_return({"Content-Type" => "text/html"})
7-
end
8-
9-
def render(content, &block)
10-
described_class.new(content).call(view_context: view, &block)
11-
end
12-
13-
it "processes text" do
14-
expect(render("Hello")).to match "Hello"
15-
end
16-
17-
it "ignores unfenced erb at start of line" do
18-
expect(render("<%= 1 + 1 %>")).to eq("<%= 1 + 1 %>")
19-
end
20-
21-
it "renders article code block" do
22-
md = <<~MD
23-
```
24-
1 + 1
25-
```
26-
MD
27-
28-
page = Capybara.string(render(md))
29-
code = page.find("pre code")
30-
31-
expect(code).to have_content("1 + 1")
32-
expect(page).to have_content("Copied")
33-
end
34-
35-
it "renders arbitrary html" do
36-
md = <<~MD
37-
<div>Hello</div>
38-
MD
39-
html = <<~HTML
40-
<div>Hello</div>
41-
HTML
42-
expect(render(md)).to eq(html)
43-
end
44-
45-
it "renders basic code block when content type is atom" do
46-
allow(view).to receive(:headers).and_return({"Content-Type" => "application/atom+xml"})
47-
48-
md = <<~MD
49-
```
50-
1 + 1
51-
```
52-
MD
53-
54-
page = Capybara.string(render(md))
55-
code = page.find("pre code")
56-
57-
expect(code).to have_content("1 + 1")
58-
expect(page).not_to have_content("Copied!")
59-
end
4+
before do
5+
allow(view).to receive(:headers).and_return({"Content-Type" => "text/html"})
6+
end
7+
8+
it "processes text" do
9+
expect(render_component("Hello")).to match "Hello"
10+
end
11+
12+
it "ignores unfenced erb at start of line" do
13+
expect(render_component("<%= 1 + 1 %>")).to eq("<%= 1 + 1 %>")
14+
end
15+
16+
it "renders article code block" do
17+
md = <<~MD
18+
```
19+
1 + 1
20+
```
21+
MD
22+
23+
render_component(md)
24+
25+
page = Capybara.string(rendered)
26+
code = page.find("pre code")
27+
28+
expect(code).to have_content("1 + 1")
29+
expect(page).to have_content("Copied")
30+
end
31+
32+
it "renders arbitrary html" do
33+
md = <<~MD
34+
<div>Hello</div>
35+
MD
36+
html = <<~HTML
37+
<div>Hello</div>
38+
HTML
39+
40+
expect(render_component(md)).to eq(html)
41+
end
42+
43+
it "renders basic code block when content type is atom" do
44+
allow(view).to receive(:headers).and_return({"Content-Type" => "application/atom+xml"})
45+
46+
md = <<~MD
47+
```
48+
1 + 1
49+
```
50+
MD
51+
52+
render_component(md)
53+
54+
page = Capybara.string(rendered)
55+
code = page.find("pre code")
56+
57+
expect(code).to have_content("1 + 1")
58+
expect(page).not_to have_content("Copied!")
6059
end
6160
end
Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
11
require "rails_helper"
22

33
RSpec.describe Markdown::Atom, type: :view do
4-
describe ".call" do
5-
let(:template) { instance_double(ActionView::Template, type: "") }
4+
let(:template) { instance_double(ActionView::Template, type: "") }
65

7-
def render(content, &block)
8-
described_class.new(content).call(&block)
9-
end
10-
11-
it "processes text" do
12-
expect(render("Hello")).to match "Hello"
13-
end
6+
it "processes text" do
7+
expect(render_component("Hello")).to match "Hello"
8+
end
149

15-
it "ignores unfenced erb at start of line" do
16-
expect(render("<%= 1 + 1 %>")).to eq("<%= 1 + 1 %>")
17-
end
10+
it "ignores unfenced erb at start of line" do
11+
expect(render_component("<%= 1 + 1 %>")).to eq("<%= 1 + 1 %>")
12+
end
1813

19-
it "renders basic code block" do
20-
md = <<~MD
21-
```
22-
1 + 1
23-
```
24-
MD
25-
html = <<~HTML
26-
<pre><code>1 + 1</code></pre>
27-
HTML
28-
expect(render(md).delete("\n")).to eq(html.delete("\n"))
29-
end
14+
it "renders basic code block" do
15+
md = <<~MD
16+
```
17+
1 + 1
18+
```
19+
MD
20+
html = <<~HTML
21+
<pre><code>1 + 1</code></pre>
22+
HTML
23+
expect(render_component(md).delete("\n")).to eq(html.delete("\n"))
24+
end
3025

31-
it "renders arbitrary html" do
32-
md = <<~MD
33-
<div>Hello</div>
34-
MD
35-
html = <<~HTML
36-
<div>Hello</div>
37-
HTML
38-
expect(render(md)).to eq(html)
39-
end
26+
it "renders arbitrary html" do
27+
md = <<~MD
28+
<div>Hello</div>
29+
MD
30+
html = <<~HTML
31+
<div>Hello</div>
32+
HTML
33+
expect(render_component(md)).to eq(html)
4034
end
4135
end

spec/views/components/markdown/base_spec.rb

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe Markdown::Base, type: :view do
66
it "supports multiple headings" do
7-
output = md <<~MD
7+
render_component <<~MD
88
# 1
99
## 2
1010
### 3
@@ -13,106 +13,102 @@
1313
###### 6
1414
MD
1515

16-
expect(output.strip).to be ==
16+
expect(rendered.strip).to be ==
1717
"<h1>1</h1> <h2>2</h2> <h3>3</h3> <h4>4</h4> <h5>5</h5> <h6>6</h6>"
1818
end
1919

2020
it "supports ordered lists" do
21-
output = md <<~MD
21+
render_component <<~MD
2222
1. One
2323
2. Two
2424
3. Three
2525
MD
2626

27-
expect(output).to be ==
27+
expect(rendered).to be ==
2828
"<ol><li>One</li> <li>Two</li> <li>Three</li> </ol>"
2929
end
3030

3131
it "supports unordered lists" do
32-
output = md <<~MD
32+
render_component <<~MD
3333
- One
3434
- Two
3535
- Three
3636
MD
3737

38-
expect(output).to be ==
38+
expect(rendered).to be ==
3939
"<ul><li>One</li> <li>Two</li> <li>Three</li> </ul>"
4040
end
4141

4242
it "supports inline code" do
43-
output = md "Some `code` here"
44-
expect(output.strip).to be == "<p>Some <code>code</code> here</p>"
43+
render_component "Some `code` here"
44+
expect(rendered.strip).to be == "<p>Some <code>code</code> here</p>"
4545
end
4646

4747
it "supports block code" do
48-
output = md <<~MD
48+
render_component <<~MD
4949
```ruby
5050
def foo
5151
bar
5252
end
5353
```
5454
MD
5555

56-
expect(output).to be ==
56+
expect(rendered).to be ==
5757
%(<pre><code class="language-ruby">def foo\n bar\nend\n</code></pre>)
5858
end
5959

6060
it "supports paragraphs" do
61-
output = md "A\n\nB"
62-
expect(output.strip).to be == "<p>A</p> <p>B</p>"
61+
render_component "A\n\nB"
62+
expect(rendered.strip).to be == "<p>A</p> <p>B</p>"
6363
end
6464

6565
it "supports links" do
66-
output = md "[Hello](world 'title')"
67-
expect(output.strip).to be == %(<p><a href="world" title="title">Hello</a></p>)
66+
render_component "[Hello](world 'title')"
67+
expect(rendered.strip).to be == %(<p><a href="world" title="title">Hello</a></p>)
6868
end
6969

7070
it "supports emphasis" do
71-
output = md "*Hello*"
72-
expect(output.strip).to be == "<p><em>Hello</em></p>"
71+
render_component "*Hello*"
72+
expect(rendered.strip).to be == "<p><em>Hello</em></p>"
7373
end
7474

7575
it "supports strong" do
76-
output = md "**Hello**"
77-
expect(output.strip).to be == "<p><strong>Hello</strong></p>"
76+
render_component "**Hello**"
77+
expect(rendered.strip).to be == "<p><strong>Hello</strong></p>"
7878
end
7979

8080
it "supports blockquotes" do
81-
output = md "> Hello"
82-
expect(output).to be == "<blockquote><p>Hello</p> </blockquote>"
81+
render_component "> Hello"
82+
expect(rendered).to be == "<blockquote><p>Hello</p> </blockquote>"
8383
end
8484

8585
it "supports horizontal rules" do
86-
output = md "---"
87-
expect(output).to be == "<hr>"
86+
render_component "---"
87+
expect(rendered).to be == "<hr>"
8888
end
8989

9090
it "supports images" do
91-
output = md "![alt](src 'title')"
92-
expect(output.strip).to be == %(<p><img src="src" alt="alt" title="title"></p>)
91+
render_component "![alt](src 'title')"
92+
expect(rendered.strip).to be == %(<p><img src="src" alt="alt" title="title"></p>)
9393
end
9494

9595
it "supports softbreaks in content as spaces" do
96-
output = md <<~MD
96+
render_component <<~MD
9797
One
9898
Two
9999
100100
Three
101101
MD
102102

103-
expect(output.strip).to be == "<p>One Two</p> <p>Three</p>"
103+
expect(rendered.strip).to be == "<p>One Two</p> <p>Three</p>"
104104
end
105105

106106
xit "supports blockquote [!NOTE]" do
107-
output = md <<~MD
107+
render_component <<~MD
108108
> [!NOTE]
109109
> Hello!
110110
MD
111111

112-
expect(output.strip).to match %r{<blockquote><svg .*><p>Hello!</p> </blockquote>}
113-
end
114-
115-
def md(content)
116-
Markdown::Base.new(content).call
112+
expect(rendered.strip).to match %r{<blockquote><svg .*><p>Hello!</p> </blockquote>}
117113
end
118114
end

0 commit comments

Comments
 (0)