2012-08-09 20:38:07 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::Result do
|
2012-08-09 20:38:07 -04:00
|
|
|
let :string do
|
|
|
|
Capybara.string <<-STRING
|
|
|
|
<ul>
|
|
|
|
<li>Alpha</li>
|
|
|
|
<li>Beta</li>
|
|
|
|
<li>Gamma</li>
|
|
|
|
<li>Delta</li>
|
|
|
|
</ul>
|
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
|
|
|
let :result do
|
|
|
|
string.all '//li'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a length" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.length).to eq(4)
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a first element" do
|
|
|
|
result.first.text == 'Alpha'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a last element" do
|
|
|
|
result.last.text == 'Delta'
|
|
|
|
end
|
|
|
|
|
2013-10-29 16:43:11 -04:00
|
|
|
it 'can supports values_at method' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.values_at(0, 2).map(&:text)).to eq(%w(Alpha Gamma))
|
2013-10-29 16:43:11 -04:00
|
|
|
end
|
|
|
|
|
2012-08-09 20:38:07 -04:00
|
|
|
it "can return an element by its index" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.at(1).text).to eq('Beta')
|
|
|
|
expect(result[2].text).to eq('Gamma')
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be mapped" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.map(&:text)).to eq(%w(Alpha Beta Gamma Delta))
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be selected" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.select do |element|
|
2012-08-09 20:38:07 -04:00
|
|
|
element.text.include? 't'
|
2013-11-14 12:43:36 -05:00
|
|
|
end.length).to eq(2)
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be reduced" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.reduce('') do |memo, element|
|
2012-08-09 20:38:07 -04:00
|
|
|
memo += element.text[0]
|
2013-11-14 12:43:36 -05:00
|
|
|
end).to eq('ABGD')
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|
2013-09-16 07:19:06 -04:00
|
|
|
|
|
|
|
it 'can be sampled' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result).to include(result.sample)
|
2013-09-16 07:19:06 -04:00
|
|
|
end
|
2013-10-29 16:43:11 -04:00
|
|
|
|
|
|
|
it 'can be indexed' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.index do |el|
|
2013-10-29 16:43:11 -04:00
|
|
|
el.text == 'Gamma'
|
2013-11-14 12:43:36 -05:00
|
|
|
end).to eq(2)
|
2013-10-29 16:43:11 -04:00
|
|
|
end
|
2012-08-09 20:38:07 -04:00
|
|
|
end
|