Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'nokogiri'
require 'rspec'
describe "nth-child" do
let(:doc) do
Nokogiri::HTML(<<-HTML)
<html>
<body>
<p id="first"></p>
<p id="second"></p>
</body>
</html>
HTML
end
let(:first) { doc.at_css("#first") }
let(:second) { doc.at_css("#second") }
it "returns both nodes with '1n'" do
nodes = doc.css("p:nth-child(1n)")
expect(nodes).to include(first)
expect(nodes).to include(second)
end
it "returns only the second node with '2n'" do
nodes = doc.css("p:nth-child(2n)")
expect(nodes).to include(second)
end
it "returns only the first node with '2n + 1'" do
nodes = doc.css("p:nth-child(2n + 1)")
expect(nodes).to include(first)
end
it "returns only the first node with '1'" do
nodes = doc.css("p:nth-child(1)")
expect(nodes).to include(first)
end
it "returns only the second node with '2'" do
nodes = doc.css("p:nth-child(2)")
expect(nodes).to include(second)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment