diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
commit | 7e9c479f7de77702622631cff2628a9c8dcbc627 (patch) | |
tree | c8f718a08e110ad7e1894510980d2155a6549197 /qa/spec/support/matchers/have_text.rb | |
parent | e852b0ae16db4052c1c567d9efa4facc81146e88 (diff) |
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'qa/spec/support/matchers/have_text.rb')
-rw-r--r-- | qa/spec/support/matchers/have_text.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/qa/spec/support/matchers/have_text.rb b/qa/spec/support/matchers/have_text.rb new file mode 100644 index 00000000000..4e6fbf1f6d6 --- /dev/null +++ b/qa/spec/support/matchers/have_text.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Matchers + class HaveText + def initialize(expected_text, **kwargs) + @expected_text = expected_text + @kwargs = kwargs + end + + def matches?(actual) + @actual = wrap(actual) + @actual.has_text?(@expected_text, **@kwargs) + end + + def does_not_match?(actual) + @actual = wrap(actual) + @actual.has_no_text?(@expected_text, **@kwargs) + end + + def failure_message + "expected to find text \"#{@expected_text}\" in \"#{normalized_actual_text}\"" + end + + def failure_message_when_negated + "expected not to find text \"#{@expected_text}\" in \"#{normalized_actual_text}\"" + end + + def normalized_actual_text + @actual.text.gsub(/\s+/, " ") + end + + # From https://github.com/teamcapybara/capybara/blob/fe5940c6afbfe32152df936ce03ad1371ae05354/lib/capybara/rspec/matchers/base.rb#L66 + def wrap(actual) + actual = actual.to_capybara_node if actual.respond_to?(:to_capybara_node) + @context_el = if actual.respond_to?(:has_selector?) + actual + else + Capybara.string(actual.to_s) + end + end + end + + def have_text(text, **kwargs) # rubocop:disable Naming/PredicateName + HaveText.new(text, **kwargs) + end + + alias_method :have_content, :have_text +end |