Skip to content

Commit 295fbfc

Browse files
committed
Extract class for rendering js html fixtures
1 parent d8f523b commit 295fbfc

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

app/javascript/test/fixtures/views/searches/combobox.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
dialog:open@window->search-combobox#openInTarget
66
"><form data-controller="autosubmit-form" data-autosubmit-delay-value="300" data-autosubmit-minimum-length-value="3" data-turbo-frame="search" action="/search" accept-charset="UTF-8" method="post"><div class="flex items-center flex-row pl-2 col-gap-xs"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 1200 1200" class="w-[32px] fill-current text-theme">
77
<path d="m1135.2 986.4-254.4-226.8c124.8-181.2 105.6-430.8-55.199-591.6-181.2-182.4-476.4-182.4-657.6 0-182.4 181.2-182.4 476.4 0 658.8 160.8 160.8 410.4 178.8 591.6 55.199l226.8 253.2c38.398 43.199 105.6 45.602 146.4 3.6016l6-6c40.801-40.801 39.598-108-3.6016-146.4zm-422.4-273.6c-120 120-313.2 120-432 0-120-120-120-313.2 0-432 120-120 313.2-120 432 0 120 120 120 313.2 0 432z"></path>
8-
</svg><label for="query" class="sr-only">Search</label> <input value="Rails" autofocus="autofocus" role="combobox" aria-expanded="false" aria-autocomplete="none" aria-controls="search-listbox" aria-owns="search-listbox" aria-haspopup="listbox" aria-activedescendant="" data-action="
8+
</svg><label for="query" class="sr-only">Query</label> <input value="Rails" autofocus="autofocus" role="combobox" aria-expanded="false" aria-autocomplete="none" aria-controls="search-listbox" aria-owns="search-listbox" aria-haspopup="listbox" aria-activedescendant="" id="search-combobox" data-action="
99
focus-&gt;combobox#tryOpen
1010
input-&gt;autosubmit-form#submit
11-
" placeholder="Search Joy of Rails" class="w-full step-1" type="search" name="query" id="query" /></div></form><ul id="search-listbox" role="listbox" class="grid" data-controller="search-listbox"><li aria-label="Mastering Custom Configuration in Rails" role="option" id="search-option_page_01jc3etwr9rb4gabrneh0dwjjh" class="rounded"><a href="/articles/mastering-custom-configuration-in-rails" data-turbo-frame="_top" class="p-2 block"><div class="font-semibold">Mastering Custom Configuration in <mark>Rails</mark></div><div class="text-sm">As your Ruby on <mark>Rails</mark> application grows, you‘ll need to add your own bits of configuration. Where do you put API keys for third-party apps? What if you need different…</div></a></li><li aria-label="Introducing Joy of Rails" role="option" id="search-option_page_01jc3etwj4p7csjx5a92qxpzad" class="rounded"><a href="/articles/introducing-joy-of-rails" data-turbo-frame="_top" class="p-2 block"><div class="font-semibold">Introducing Joy of <mark>Rails</mark></div><div class="text-sm">…With Joy of <mark>Rails</mark>, I want to do something a little different—more than I can with a static website. Built with <mark>Rails</mark> Joy of <mark>Rails</mark> is a <mark>Rails</mark> monolith currently running…</div></a></li><li aria-label="Add your Rails app to the Home Screen - the Ultimate Guide" role="option" id="search-option_page_01jc3etwn1nrym36ch59gf03gy" class="rounded"><a href="/articles/add-your-rails-app-to-the-home-screen" data-turbo-frame="_top" class="p-2 block"><div class="font-semibold">Add your <mark>Rails</mark> app to the Home Screen - the Ultimate Guide</div><div class="text-sm">…In this article, I‘ll show you how to set up your <mark>Rails</mark> app to be installable as a PWA. Install Joy of <mark>Rails</mark> Since Joy of <mark>Rails</mark> is itself a <mark>Rails</mark></div></a></li></ul></div>
11+
" placeholder="Search Joy of Rails" class="w-full step-1" type="search" name="query" /></div><input value="search" autocomplete="off" type="hidden" name="name" id="name" /></form><ul id="search-listbox" role="listbox" class="grid" data-controller="search-listbox"><li aria-label="Here’the thing" role="option" id="search-option_search_result_articles-heres-the-thing" class="rounded"><a href="/articles/heres-the-thing"><div>Here’the thing</div><div>Ruby is a programming language</div></a></li><li aria-label="Introducing Joy of Rails" role="option" id="search-option_search_result_articles-joy-of-rails" class="rounded"><a href="/articles/joy-of-rails"><div>Introducing Joy of Rails</div><div>Rails is a web application framework</div></a></li></ul></div>

lib/html_fixtures/renderer.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module HtmlFixtures
2+
class Renderer
3+
def render_all
4+
request = ActionDispatch::Request.new({})
5+
request.routes = ApplicationController._routes
6+
7+
instance = ApplicationController.new
8+
instance.set_request! request
9+
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+
)
49+
end
50+
end
51+
end
52+
53+
module InlineSvgIdGeneratorRandomnessOverrideForFixtures
54+
def call
55+
"non-random-for-fixtures"
56+
end
57+
end
58+
59+
if Rails.env.local?
60+
class InlineSvg::IdGenerator::Randomness
61+
class << self
62+
prepend InlineSvgIdGeneratorRandomnessOverrideForFixtures
63+
end
64+
end
65+
end

lib/tasks/fixtures/html.rake

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
11
namespace :fixtures do
22
task html: :environment do
3-
if Rails.env.local?
4-
class InlineSvg::IdGenerator::Randomness
5-
class << self
6-
prepend InlineSvgIdGeneratorRandomnessOverrideForFixtures
7-
end
8-
end
9-
end
3+
require_relative "../../html_fixtures/renderer"
104

11-
Rake::Task["db:seed"].invoke
12-
13-
request = ActionDispatch::Request.new({})
14-
request.routes = ApplicationController._routes
15-
16-
instance = ApplicationController.new
17-
instance.set_request! request
18-
instance.set_response! ApplicationController.make_response!(request)
19-
view_context = instance.view_context
20-
21-
File.write(
22-
Rails.root.join("app", "javascript", "test", "fixtures", "views", "darkmode", "switch.html"),
23-
DarkMode::Switch.call
24-
)
25-
26-
File.write(
27-
Rails.root.join("app", "javascript", "test", "fixtures", "views", "searches", "combobox.html"),
28-
Searches::Combobox.new(pages: Page.search("Rails*").with_snippets.ranked.limit(3), query: "Rails").call(view_context:)
29-
)
30-
end
31-
end
32-
33-
module InlineSvgIdGeneratorRandomnessOverrideForFixtures
34-
def call
35-
"non-random-for-fixtures"
5+
HtmlFixtures::Renderer.new.render_all
366
end
377
end

0 commit comments

Comments
 (0)