Skip to content

Commit 89f56d9

Browse files
committed
Extract html fixture renderers
1 parent 9013119 commit 89f56d9

File tree

4 files changed

+87
-39
lines changed

4 files changed

+87
-39
lines changed

lib/html_fixtures/renderer.rb

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,32 @@
11
module HtmlFixtures
22
class Renderer
33
def render_all
4+
[
5+
HtmlFixtures::Renderers::DarkmodeSwitch.new,
6+
HtmlFixtures::Renderers::SearchesCombobox.new
7+
].each do |renderer|
8+
File.write(renderer.fixture_path, renderer.render(view_context))
9+
end
10+
end
11+
12+
def view_context
13+
@view_context ||= build_view_context
14+
end
15+
16+
def build_view_context
417
request = ActionDispatch::Request.new({})
518
request.routes = ApplicationController._routes
619

720
instance = ApplicationController.new
821
instance.set_request! request
922
instance.set_response! ApplicationController.make_response!(request)
10-
view_context = instance.view_context
11-
12-
File.write(
13-
Rails.root.join("app", "javascript", "test", "fixtures", "views", "darkmode", "switch.html"),
14-
DarkMode::Switch.call
15-
)
16-
17-
search_result = Data.define(:title, :url, :body) do
18-
def to_key = [url.parameterize]
19-
20-
def model_name = ActiveModel::Name.new(self, nil, "SearchResult")
21-
end
22-
23-
Class.new(ApplicationComponent) do
24-
attr_reader :search_result
25-
26-
def initialize(search_result)
27-
@search_result = search_result
28-
end
29-
30-
def view_template
31-
a(href: search_result.url) do
32-
div { raw safe(search_result.title) }
33-
div { raw safe(search_result.body) }
34-
end
35-
end
36-
37-
Searches::Result.register(search_result, self)
38-
end
39-
40-
search_results = [
41-
search_result.new(title: "Here’the thing", url: "/articles/heres-the-thing", body: "Ruby is a programming language"),
42-
search_result.new(title: "Introducing Joy of Rails", url: "/articles/joy-of-rails", body: "Rails is a web application framework")
43-
]
44-
45-
File.write(
46-
Rails.root.join("app", "javascript", "test", "fixtures", "views", "searches", "combobox.html"),
47-
Searches::Combobox.new(results: search_results, query: "Rails").call(view_context:)
48-
)
23+
instance.view_context
4924
end
5025
end
5126
end
5227

28+
# We need to override the randomness of the InlineSvg gem to ensure that the SVG
29+
# IDs are consistent across test runs and git commits.
5330
module InlineSvgIdGeneratorRandomnessOverrideForFixtures
5431
def call
5532
"non-random-for-fixtures"

lib/html_fixtures/renderers/base.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module HtmlFixtures
2+
module Renderers
3+
class Base
4+
def fixture_path
5+
raise NotImplementedError
6+
end
7+
8+
def render(view_context)
9+
raise NotImplementedError
10+
end
11+
end
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module HtmlFixtures
2+
module Renderers
3+
class DarkmodeSwitch < Base
4+
def render(view_context)
5+
DarkMode::Switch.new.call(view_context:)
6+
end
7+
8+
def fixture_path
9+
Rails.root.join("app", "javascript", "test", "fixtures", "views", "darkmode", "switch.html")
10+
end
11+
end
12+
end
13+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module HtmlFixtures
2+
module Renderers
3+
class SearchesCombobox < Base
4+
def render(view_context)
5+
Searches::Combobox.new(results: search_results, query: "Rails").call(view_context:)
6+
end
7+
8+
def fixture_path
9+
Rails.root.join("app", "javascript", "test", "fixtures", "views", "searches", "combobox.html")
10+
end
11+
12+
private
13+
14+
def search_results
15+
search_result = Data.define(:title, :url, :body) do
16+
def to_key = [url.parameterize]
17+
18+
def model_name = ActiveModel::Name.new(self, nil, "SearchResult")
19+
end
20+
21+
Class.new(ApplicationComponent) do
22+
attr_reader :search_result
23+
24+
def initialize(search_result)
25+
@search_result = search_result
26+
end
27+
28+
def view_template
29+
a(href: search_result.url) do
30+
div { raw safe(search_result.title) }
31+
div { raw safe(search_result.body) }
32+
end
33+
end
34+
35+
Searches::Result.register(search_result, self)
36+
end
37+
38+
[
39+
search_result.new(title: "Here’the thing", url: "/articles/heres-the-thing", body: "Ruby is a programming language"),
40+
search_result.new(title: "Introducing Joy of Rails", url: "/articles/joy-of-rails", body: "Rails is a web application framework")
41+
]
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)