From 7267afa18d5dcdd77c9fb8a918897715b3865ee0 Mon Sep 17 00:00:00 2001 From: Sina Eftekhar Date: Wed, 3 Jan 2024 12:30:55 +0200 Subject: [PATCH] Add tests; fix redirect to page nil --- .../admin/translations_controller.rb | 13 ++++++++-- .../test/rspec_support/capybara.rb | 25 +++++++++++++++++++ .../admin/translations_controller_spec.rb | 16 +++++++++++- spec/spec_helper.rb | 4 +++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 lib/decidim/term_customizer/test/rspec_support/capybara.rb diff --git a/app/controllers/decidim/term_customizer/admin/translations_controller.rb b/app/controllers/decidim/term_customizer/admin/translations_controller.rb index 8e1b671..3241f5c 100644 --- a/app/controllers/decidim/term_customizer/admin/translations_controller.rb +++ b/app/controllers/decidim/term_customizer/admin/translations_controller.rb @@ -51,12 +51,12 @@ def update current_organization: current_organization, translation_set: set ) - page_param = params[:translation][:page] UpdateTranslation.call(@form, translation) do on(:ok) do flash[:notice] = I18n.t("translations.update.success", scope: "decidim.term_customizer.admin") - redirect_to translation_set_translations_path(set, page: page_param) + + redirect_to redirect_path_for_set(set) end on(:invalid) do @@ -139,6 +139,15 @@ def translation @translation ||= Decidim::TermCustomizer::Translation.find(params[:id]) end + def redirect_path_for_set(set) + page_param = params[:translation][:page] + if page_param.nil? + translation_set_translations_path(set) + else + translation_set_translations_path(set, page: page_param) + end + end + alias collection translations alias set translation_set end diff --git a/lib/decidim/term_customizer/test/rspec_support/capybara.rb b/lib/decidim/term_customizer/test/rspec_support/capybara.rb new file mode 100644 index 0000000..ba81607 --- /dev/null +++ b/lib/decidim/term_customizer/test/rspec_support/capybara.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "selenium-webdriver" + +module Decidim + # This is being added because of the issues with the chrome-driver + # in version 120 or later, and this can be removed after this pr#12160 + # being merged(more info https://github.com/decidim/decidim/pull/12159). + Capybara.register_driver :headless_chrome do |app| + options = ::Selenium::WebDriver::Chrome::Options.new + options.args << "--headless=new" + options.args << "--no-sandbox" + options.args << if ENV["BIG_SCREEN_SIZE"].present? + "--window-size=1920,3000" + else + "--window-size=1920,1080" + end + options.args << "--ignore-certificate-errors" if ENV["TEST_SSL"] + Capybara::Selenium::Driver.new( + app, + browser: :chrome, + capabilities: [options] + ) + end +end diff --git a/spec/controllers/decidim/term_customizer/admin/translations_controller_spec.rb b/spec/controllers/decidim/term_customizer/admin/translations_controller_spec.rb index 8115e5c..2fb7540 100644 --- a/spec/controllers/decidim/term_customizer/admin/translations_controller_spec.rb +++ b/spec/controllers/decidim/term_customizer/admin/translations_controller_spec.rb @@ -77,12 +77,26 @@ module TermCustomizer put :update, params: params.merge( id: translation.id, key: "updated.translation.key", - value: { en: "Lorem ipsum dolor" } + value: { en: "Lorem ipsum dolor" }, + translation: { page: nil } ) expect(flash[:notice]).not_to be_empty expect(response).to have_http_status(:found) end + + context "when page params exist" do + it "redirects to the same paginated page" do + put :update, params: params.merge( + id: translation.id, + key: "updated.translation.key", + value: { en: "Lorem ipsum dolor" }, + translation: { page: "2" } + ) + + expect(response).to redirect_to("/sets/#{translation_set.id}/translations?page=2") + end + end end describe "POST export" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0b80de0..6e46fd4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,10 @@ File.expand_path(File.join(__dir__, "decidim_dummy_app")) require "decidim/dev/test/base_spec_helper" +# This is being added because of the issues with the chrome-driver +# in version 120 or later, and this can be removed after this pr#12160 +# being merged(more info https://github.com/decidim/decidim/pull/12159). +require "#{Dir.pwd}/lib/decidim/term_customizer/test/rspec_support/capybara.rb" RSpec.configure do |config| # Add extra traslation load path for the tests