replace matchers in view specs with nokogiri-based ones

This commit is contained in:
Georg Gadinger 2023-02-16 21:50:12 +01:00
parent caccf8b1b3
commit 1a0ca59113
5 changed files with 55 additions and 26 deletions

View File

@ -8,6 +8,35 @@ module NokogiriMatchers
Nokogiri::HTML.parse(rendered).css(css).size.positive?
end
end
RSpec::Matchers.matcher :have_attribute do |expected_attribute|
description do
case expected_attribute
when Hash
raise ArgumentError.new("have_attribute only wants one key=>value pair") unless expected_attribute.size == 1
key = expected_attribute.keys.first
value = expected_attribute.values.first
%(have an attribute named #{key.inspect} with a value of #{value.inspect})
else
%(have an attribute named #{expected_attribute.inspect})
end
end
match do |element|
case expected_attribute
when Hash
raise ArgumentError.new("have_attribute only wants one key=>value pair") unless expected_attribute.size == 1
key = expected_attribute.keys.first
value = expected_attribute.values.first
element.attr(key.to_s).value == value
else
!element.attr(expected_attribute.to_s).nil?
end
end
end
end
RSpec.configure do |c|

View File

@ -23,9 +23,7 @@ describe "inbox/_actions.html.haml", type: :view do
it "has a button for deleting all inbox entries" do
html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all")
expect(button.attr("disabled")).to be_nil
expect(button.attr("data-ib-count").value).to eq "4020"
expect(button).not_to have_attribute(:disabled)
end
context "with disabled = true" do
@ -34,8 +32,7 @@ describe "inbox/_actions.html.haml", type: :view do
it "has a button for deleting all inbox entries" do
html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all")
expect(button.attr("disabled").value).to be_truthy
expect(button).to have_attribute(:disabled)
end
end
@ -46,7 +43,7 @@ describe "inbox/_actions.html.haml", type: :view do
html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all-author")
expect(button.attr("data-ib-count").value).to eq "4020"
expect(button).to have_attribute("data-ib-count" => "4020")
end
end
end

View File

@ -6,10 +6,10 @@ describe "inbox/_push_settings.haml", type: :view do
subject(:rendered) { render }
it "has a button to enable push notifications" do
expect(rendered).to match(/<button.+data-action='push-enable'>/)
expect(rendered).to have_css(%(button[data-action="push-enable"]))
end
it "has a button to dismiss the view" do
expect(rendered).to match(/<button.+data-action='push-dismiss'>/)
expect(rendered).to have_css(%(button[data-action="push-dismiss"]))
end
end

View File

@ -17,7 +17,10 @@ describe "inbox/show.html.haml", type: :view do
end
it "displays an 'inbox is empty' message" do
expect(rendered).to match %(<p class='empty'>Nothing to see here.</p>)
html = Nokogiri::HTML.parse(rendered)
selector = "p.empty"
expect(rendered).to have_css(selector)
expect(html.css(selector).text.strip).to eq "Nothing to see here."
end
end
@ -30,16 +33,16 @@ describe "inbox/show.html.haml", type: :view do
end
it "renders inbox entries" do
expect(rendered).to match(/id='inbox_#{inbox_entry1.id}'/)
expect(rendered).to match(/id='inbox_#{inbox_entry2.id}'/)
expect(rendered).to have_css("#inbox_#{inbox_entry1.id}")
expect(rendered).to have_css("#inbox_#{inbox_entry2.id}")
end
it "does not contain the empty inbox message" do
expect(rendered).not_to match(%r{<p class='empty'>Nothing to see here.</p>})
expect(rendered).not_to have_css("p.empty")
end
it "does not render the paginator" do
expect(rendered).not_to match(/id='paginator'/)
expect(rendered).not_to have_css("#paginator")
end
context "when more data is available" do
@ -49,12 +52,12 @@ describe "inbox/show.html.haml", type: :view do
end
it "renders the paginator" do
expect(rendered).to match(/id='paginator'/)
expect(rendered).to have_css("#paginator")
end
it "has the correct params on the button" do
expect(rendered).to match(/input type="hidden" name="last_id" value="1337"/)
expect(rendered).not_to match(/input type="hidden" name="author"/)
expect(rendered).to have_css(%(input[type="hidden"][name="last_id"][value="1337"]))
expect(rendered).not_to have_css(%(input[type="hidden"][name="author"]))
end
context "when passed an author" do
@ -63,8 +66,8 @@ describe "inbox/show.html.haml", type: :view do
end
it "has the correct params on the button" do
expect(rendered).to match(/input type="hidden" name="last_id" value="1337"/)
expect(rendered).to match(/input type="hidden" name="author"/)
expect(rendered).to have_css(%(input[type="hidden"][name="last_id"][value="1337"]))
expect(rendered).to have_css(%(input[type="hidden"][name="author"]))
end
end
end

View File

@ -20,16 +20,16 @@ describe "inbox/show.turbo_stream.haml", type: :view do
end
it "appends to entries" do
expect(rendered).to match(/<turbo-stream action="append" target="entries">/)
expect(rendered).to have_css(%(turbo-stream[action="append"][target="entries"]))
end
it "renders inbox entries" do
expect(rendered).to match(/id='inbox_#{inbox_entry1.id}'/)
expect(rendered).to match(/id='inbox_#{inbox_entry2.id}'/)
expect(rendered).to have_css("#inbox_#{inbox_entry1.id}")
expect(rendered).to have_css("#inbox_#{inbox_entry2.id}")
end
it "updates the paginator" do
expect(rendered).to match(/<turbo-stream action="update" target="paginator">/)
expect(rendered).to have_css(%(turbo-stream[action="update"][target="paginator"]))
end
context "when more data is available" do
@ -39,8 +39,8 @@ describe "inbox/show.turbo_stream.haml", type: :view do
end
it "has the correct params on the button" do
expect(rendered).to match(/input type="hidden" name="last_id" value="1337"/)
expect(rendered).not_to match(/input type="hidden" name="author"/)
expect(rendered).to have_css(%(input[type="hidden"][name="last_id"][value="1337"]))
expect(rendered).not_to have_css(%(input[type="hidden"][name="author"]))
end
context "when passed an author" do
@ -49,8 +49,8 @@ describe "inbox/show.turbo_stream.haml", type: :view do
end
it "has the correct params on the button" do
expect(rendered).to match(/input type="hidden" name="last_id" value="1337"/)
expect(rendered).to match(/input type="hidden" name="author"/)
expect(rendered).to have_css(%(input[type="hidden"][name="last_id"][value="1337"]))
expect(rendered).to have_css(%(input[type="hidden"][name="author"]))
end
end
end