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? Nokogiri::HTML.parse(rendered).css(css).size.positive?
end end
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 end
RSpec.configure do |c| 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 it "has a button for deleting all inbox entries" do
html = Nokogiri::HTML.parse(rendered) html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all") button = html.css("button#ib-delete-all")
expect(button).not_to have_attribute(:disabled)
expect(button.attr("disabled")).to be_nil
expect(button.attr("data-ib-count").value).to eq "4020"
end end
context "with disabled = true" do 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 it "has a button for deleting all inbox entries" do
html = Nokogiri::HTML.parse(rendered) html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all") button = html.css("button#ib-delete-all")
expect(button).to have_attribute(:disabled)
expect(button.attr("disabled").value).to be_truthy
end end
end end
@ -46,7 +43,7 @@ describe "inbox/_actions.html.haml", type: :view do
html = Nokogiri::HTML.parse(rendered) html = Nokogiri::HTML.parse(rendered)
button = html.css("button#ib-delete-all-author") 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 end
end end

View file

@ -6,10 +6,10 @@ describe "inbox/_push_settings.haml", type: :view do
subject(:rendered) { render } subject(:rendered) { render }
it "has a button to enable push notifications" do 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 end
it "has a button to dismiss the view" do 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
end end

View file

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

View file

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