Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Use custom matchers and test helpers for better test dsl

Kabiru Mwenja6bfc40f4dc22
diff --git a/spec/features/my/interface_spec.rb b/spec/features/my/interface_spec.rb
index a764a5c5c45..8562e8744b4 100644
--- a/spec/features/my/interface_spec.rb
+++ b/spec/features/my/interface_spec.rb
@@ -29,9 +29,12 @@
#++
require "spec_helper"
+require_relative "support/theme_matchers"
RSpec.describe "My account Interface settings",
:js, :selenium do
+ include ThemeTestHelpers
+
let(:user) { create(:user) }
before do
@@ -40,26 +43,20 @@ RSpec.describe "My account Interface settings",
end
it "allows the user to specify their preferred color mode" do
- click_on "Interface"
-
- select "Light", from: "Color mode"
- click_on "Update look and feel"
+ navigate_to_interface_settings
- expect(page).to have_css("body[data-color-mode='light'][data-light-theme='light']")
+ select_theme "Light"
+ expect(page).to have_theme("light")
- select "Dark", from: "Color mode"
- click_on "Update look and feel"
-
- expect(page).to have_css("body[data-color-mode='dark'][data-dark-theme='dark']")
-
- select "Automatic (match OS color mode)", from: "Color mode"
- click_on "Update look and feel"
+ select_theme "Dark"
+ expect(page).to have_theme("dark")
- expect(page).to have_css("body[data-auto-theme-switcher-theme-value='sync_with_os']")
+ select_theme "Automatic (match OS color mode)"
+ expect(page).to have_auto_theme_config({})
end
it "allows user to increase contrast for single theme modes" do
- click_on "Interface"
+ navigate_to_interface_settings
select "Light", from: "Color mode"
@@ -67,21 +64,16 @@ RSpec.describe "My account Interface settings",
expect(page).to have_no_field("Force high-contrast when in Light mode")
expect(page).to have_no_field("Force high-contrast when in Dark mode")
- check "Increase contrast"
- click_on "Update look and feel"
-
- expect(page).to have_css("body[data-color-mode='light'][data-light-theme='light_high_contrast']")
+ enable_contrast_for_single_theme
+ expect(page).to have_theme("light", high_contrast: true)
select "Dark", from: "Color mode"
-
- check "Increase contrast"
- click_on "Update look and feel"
-
- expect(page).to have_css("body[data-color-mode='dark'][data-dark-theme='dark_high_contrast']")
+ enable_contrast_for_single_theme
+ expect(page).to have_theme("dark", high_contrast: true)
end
it "shows appropriate contrast options based on theme selection" do
- click_on "Interface"
+ navigate_to_interface_settings
select "Light", from: "Color mode"
expect(page).to have_field("Increase contrast")
@@ -101,30 +93,26 @@ RSpec.describe "My account Interface settings",
describe "Automatic (match OS color mode)" do
def set_automatic_mode_with_reload
- click_on "Interface"
-
- select "Automatic (match OS color mode)", from: "Color mode"
- click_on "Update look and feel"
+ navigate_to_interface_settings
+ select_theme "Automatic (match OS color mode)"
end
it "allows user to configure auto-contrast for both light and dark modes" do
- click_on "Interface"
+ navigate_to_interface_settings
select "Automatic (match OS color mode)", from: "Color mode"
+ configure_auto_contrast(light: true, dark: true)
- check "Force high-contrast when in Light mode"
- check "Force high-contrast when in Dark mode"
- click_on "Update look and feel"
-
- expect(page).to have_css("body[data-auto-theme-switcher-theme-value='sync_with_os']")
- expect(page).to have_css("body[data-auto-theme-switcher-enable-auto-light-theme-contrast-value='true']")
- expect(page).to have_css("body[data-auto-theme-switcher-enable-auto-dark-theme-contrast-value='true']")
+ expect(page).to have_auto_theme_config(
+ enable_auto_light_contrast: true,
+ enable_auto_dark_contrast: true
+ )
end
context "with OS in dark mode", driver: :chrome_dark_mode do
it "syncs with OS colour mode" do
set_automatic_mode_with_reload
- expect(page).to have_css("body[data-color-mode='dark'][data-dark-theme='dark']")
+ expect(page).to have_theme("dark")
end
end
end
diff --git a/spec/features/my/support/theme_matchers.rb b/spec/features/my/support/theme_matchers.rb
new file mode 100644
index 00000000000..8fe219d67d2
--- /dev/null
+++ b/spec/features/my/support/theme_matchers.rb
@@ -0,0 +1,91 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+RSpec::Matchers.define :have_theme do |theme, options = {}|
+ match do |page|
+ contrast_suffix = options[:high_contrast] ? "_high_contrast" : ""
+ page.has_css?("body[data-color-mode='#{theme}'][data-#{theme}-theme='#{theme}#{contrast_suffix}']")
+ end
+
+ failure_message do |_page|
+ contrast_text = options[:high_contrast] ? " with high contrast" : ""
+ "expected page to have #{theme} theme#{contrast_text}"
+ end
+
+ failure_message_when_negated do |_page|
+ contrast_text = options[:high_contrast] ? " with high contrast" : ""
+ "expected page not to have #{theme} theme#{contrast_text}"
+ end
+end
+
+RSpec::Matchers.define :have_auto_theme_config do |expected_config|
+ match do |page|
+ base_selector = "body[data-auto-theme-switcher-theme-value='sync_with_os']"
+ return false unless page.has_css?(base_selector)
+
+ if expected_config[:enable_auto_light_contrast]
+ light_selector = "body[data-auto-theme-switcher-enable-auto-light-theme-contrast-value='true']"
+ return false unless page.has_css?(light_selector)
+ end
+
+ if expected_config[:enable_auto_dark_contrast]
+ dark_selector = "body[data-auto-theme-switcher-enable-auto-dark-theme-contrast-value='true']"
+ return false unless page.has_css?(dark_selector)
+ end
+
+ true
+ end
+
+ failure_message do |_page|
+ "expected page to have auto theme configuration: #{expected_config}"
+ end
+end
+
+module ThemeTestHelpers
+ def select_theme(theme)
+ select theme, from: "Color mode"
+ click_on "Update look and feel"
+ end
+
+ def enable_contrast_for_single_theme
+ check "Increase contrast"
+ click_on "Update look and feel"
+ end
+
+ def configure_auto_contrast(light: false, dark: false)
+ check "Force high-contrast when in Light mode" if light
+ check "Force high-contrast when in Dark mode" if dark
+ click_on "Update look and feel"
+ end
+
+ def navigate_to_interface_settings
+ click_on "Interface"
+ end
+end