Files
openfoodnetwork/spec/support/matchers/table_matchers.rb
Maikel Linke 1407ae7778 Make TableMatcher match exactly, not part of row
Reverts dbf3a7aaaf9d458f99e14983ca9db2d4cbe4b564.

The reverted commit tried to avoid a 30 second delay by using
have_selector. While that was successful in reducing test time, it made
the matcher find rows that where not exactly the wanted row, but contained
text of the wanted row. This wasn't a problem until we experimented with
Chrome as test browser returns text on select boxes.

This commit makes the matcher precise again. We still have to deal with
the 30 second delay.
2019-01-16 16:21:30 +00:00

66 lines
1.5 KiB
Ruby

RSpec::Matchers.define :have_table_row do |row|
match do |node|
@row = row
rows_under(node).include? row # Robust check of columns
end
match_when_negated do |node|
@row = row
!rows_under(node).include? row # Robust check of columns
end
failure_message do |text|
"expected to find table row #{@row}"
end
failure_message_when_negated do |text|
"expected not to find table row #{@row}"
end
def rows_under(node)
node.all('tr').map { |tr| tr.all('th, td').map(&:text) }
end
end
# find("#my-table").should match_table [[...]]
RSpec::Matchers.define :match_table do |expected_table|
match do |node|
rows = node.
all("tr").
map { |r| r.all("th,td").map { |c| c.text.strip } }
if rows.count != expected_table.count
@failure_message = "found table with #{rows.count} rows, expected #{expected_table.count}"
else
rows.each_with_index do |row, i|
expected_row = expected_table[i]
if row.count != expected_row.count
@failure_message = "row #{i} has #{row.count} columns, expected #{expected_row.count}"
break
elsif row != expected_row
row.each_with_index do |cell, j|
if cell != expected_row[j]
@failure_message = "cell [#{i}, #{j}] has content '#{cell}', expected '#{expected_row[j]}'"
break
end
end
break if @failure_message
end
end
end
@failure_message.nil?
end
failure_message do |text|
@failure_message
end
end