Skip to content

Commit

Permalink
Add system spec replacement for slimmer tests
Browse files Browse the repository at this point in the history
- These tests were previously controller tests
  that worked by looking for Slimmer header
  presence. We can't do that in request tests,
  so we recast as system tests and actually provide
  the layout elements that slimmer tinkers with
  to control how search items appear on the final
  page. This isn't an ideal way to do this, because
  it requires a bit more knowledge of how slimmer
  works than you'd hope. If the layouts that are
  actually used no longer have the #global-header
  #search and form#search# elements, this test
  won't catch that they're gone. But that's a
  wider issue with slimmer/static.
  • Loading branch information
KludgeKML committed Jul 3, 2024
1 parent 8ea87a2 commit 73a64a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
26 changes: 26 additions & 0 deletions spec/support/slimmer_templates/wrapper.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html>
<head>
<title>Test Template</title>
</head>
<body>
<script>document.body.className += ' js-enabled' + ('noModule' in HTMLScriptElement.prototype ? ' govuk-frontend-supported' : '');</script>
<article>Hello!</article>
<header id="global-header">
<div class="header-wrapper"></div>
<div id="search"></div>
</header>
<div id="global-breadcrumb" class="header-context">
<nav role="navigation">
<ol class="group">
<li><a href="/">Home</a></li>
</ol>
</nav>
</div>

<form id="search"></form>

<div id="wrapper"></div>

<footer id="footer"></footer>
</body>
</html>
38 changes: 17 additions & 21 deletions spec/system/service_manual_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
# WARNING: This test won't catch if the actual layouts change in static. It's
# only testing whether we're correctly telling slimmer to process the search
# elements, and slimmer is trying to process the layouts in the way we expect.
RSpec.describe("Service Manual", type: :system) do
let!(:content_item) { content_store_has_schema_example("service_manual_guide", "service_manual_guide") }
let!(:path) { "/#{path_for(content_item)}" }

it "has a search form scoped to the manual" do
visit path

expect(page).to have_css("input")
before do
html = File.read(Rails.root.join("spec/support/slimmer_templates/wrapper.html"))
allow_any_instance_of(Slimmer::Skin).to receive(:load_template).and_return(html)
end

# it "tells slimmer to scope search results to the manual for a service manual guide" do
# content_item = content_store_has_schema_example("service_manual_guide", "service_manual_guide")

# expect(Slimmer::Processors::SearchParameterInserter).to receive(:new).with(hash_including: {
# Slimmer::Headers::SEARCH_PARAMETERS_HEADER => { filter_manual: "/service-manual" }.to_json,
# })
# get "/#{path_for(content_item)}"
it "has a search form scoped to the manual" do
content_item = content_store_has_schema_example("service_manual_guide", "service_manual_guide")
visit "/#{path_for(content_item)}"

# # byebug
# # expect(response.body).to have_css("input")
# # assert_select("input[type='hidden'][name='filter_manual'][values='/service-manual']")
# # expect(response.headers[Slimmer::Headers::SEARCH_PARAMETERS_HEADER]).to eq({ filter_manual: "/service-manual" }.to_json)
# end
expect(page).to have_css("#global-header #search")
expect(page).to have_css("form#search input[type=hidden][name=filter_manual][value='/service-manual']", visible: false)
end

# it("tells slimmer not to include a search box in the header for the service manual homepage") do
# content_item = content_store_has_schema_example("service_manual_homepage", "service_manual_homepage")
# get "/#{path_for(content_item)}"
it "does not show a search box for the manual homepage" do
content_item = content_store_has_schema_example("service_manual_homepage", "service_manual_homepage")
visit "/#{path_for(content_item)}"

# expect(response.headers[Slimmer::Headers::REMOVE_SEARCH_HEADER]).to be true
# end
expect(page).not_to have_css("#global-header #search")
end
end

0 comments on commit 73a64a4

Please sign in to comment.