custom_matchers.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # taken from https://makandracards.com/makandra/1080-rspec-matcher-to-check-if-an-activerecord-exists-in-the-database
  3. RSpec::Matchers.define :exist_in_database do
  4. match do |actual|
  5. actual.class.exists?(actual.id)
  6. end
  7. end
  8. RSpec::Matchers.define :have_computed_style do
  9. match do
  10. actual_value == expected_value
  11. end
  12. failure_message do
  13. "Expected element to have CSS property #{expected_key} with value #{expected_value}. But it was #{actual_value}."
  14. end
  15. def expected_key
  16. expected[0]
  17. end
  18. def expected_value
  19. expected[1]
  20. end
  21. def actual_value
  22. actual.evaluate_script "getComputedStyle(this).#{expected_key}"
  23. end
  24. end
  25. RSpec::Matchers.define :have_multiple_texts do
  26. match do
  27. wait.until do
  28. expected.all? do |elem|
  29. actual.has_text? elem, wait: 0
  30. end
  31. end
  32. end
  33. match_when_negated do
  34. wait.until do
  35. expected.all? do |elem|
  36. actual.has_no_text? elem, wait: 0
  37. end
  38. end
  39. end
  40. failure_message do
  41. missing = array_to_display expected.reject { |elem| actual.has_text? elem, wait: 0 }
  42. "Expected element to have #{expected_texts_display} but #{missing} missing"
  43. end
  44. failure_message_when_negated do
  45. present = array_to_display expected.select { |elem| actual.has_text? elem, wait: 0 }
  46. "Expected element to have #{expected_texts_display} but #{present} missing"
  47. end
  48. def expected_texts_display
  49. array_to_display expected
  50. end
  51. def array_to_display(input)
  52. input
  53. .map { |elem| "\"#{elem}\"" }
  54. .join(', ')
  55. end
  56. end
  57. RSpec::Matchers.define_negated_matcher :have_no_multiple_texts, :have_multiple_texts