Browse Source

added unit testing to view component

pull/673/head
Ted 3 years ago committed by JG
parent
commit
12801bfd98
  1. 2
      app/components/search_result_caption_component.html.erb
  2. 6
      app/components/search_result_caption_component.rb
  3. 23
      spec/components/search_result_caption_component_spec.rb

2
app/components/search_result_caption_component.html.erb

@ -1,6 +1,6 @@
<span class="govuk-!-margin-right-4">
<% if searched.present? %>
<strong><%= count %></strong> <%= item_label %> found matching ‘<%= searched %>’ of <strong><%= total_count %></strong> total <%= item %>. <%= govuk_link_to("Clear search", request.path) %>
<strong><%= count %></strong> <%= item_label %> found matching '<%= searched %>' of <strong><%= total_count %></strong> total <%= item %>. <%= govuk_link_to("Clear search", path) %>
<% else %>
<strong><%= count %></strong> total <%= item %>.
<% end %>

6
app/components/search_result_caption_component.rb

@ -1,13 +1,13 @@
class SearchResultCaptionComponent < ViewComponent::Base
attr_reader :searched, :count, :item_label, :total_count, :item, :request
attr_reader :searched, :count, :item_label, :total_count, :item, :path
def initialize(searched:, count:, item_label:, total_count:, item:, request:)
def initialize(searched:, count:, item_label:, total_count:, item:, path:)
@searched = searched
@count = count
@item_label = item_label
@total_count = total_count
@item = item
@request = request
@path = path
super
end
end

23
spec/components/search_result_caption_component_spec.rb

@ -0,0 +1,23 @@
require "rails_helper"
RSpec.describe SearchResultCaptionComponent, type: :component do
let(:page) { Capybara::Node::Simple.new(rendered_component) }
let(:searched) { "search item" }
let(:count) { 2 }
let(:item_label) { "user" }
let(:total_count) { 3 }
let(:item) { "schemes" }
let(:path) { "path" }
it "all of the nav tabs specified in the items hash are passed to it" do
result = render_inline(described_class.new(searched:, count:, item_label:, total_count:, item:, path:))
expect(result.to_html).to eq(" <span class=\"govuk-!-margin-right-4\">\n <strong>#{count}</strong> #{item_label} found matching '#{searched}' of <strong>#{total_count}</strong> total #{item}. <a class=\"govuk-link\" href=\"path\">Clear search</a>\n</span>\n")
end
context "when no search results are found" do
it "all of the nav tabs specified in the items hash are passed to it" do
result = render_inline(described_class.new(searched:, count:, item_label:, total_count:, item:, path:))
expect(result.to_html).to eq(" <span class=\"govuk-!-margin-right-4\">\n <strong>#{count}</strong> #{item_label} found matching '#{searched}' of <strong>#{total_count}</strong> total #{item}. <a class=\"govuk-link\" href=\"path\">Clear search</a>\n</span>\n")
end
end
end
Loading…
Cancel
Save