Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
class ApplicationController < ActionController::Base
helper Mitlibraries::Theme::Engine.helpers

# Set active tab based on feature flag and params
# Also stores the last used tab in a cookie for future searches when passed via params.
# We set this in a session cookie to persist user preference across searches.
# Clicking on a different tab will update the cookie.
# Set active tab based on params (no persistent cookie). This intentionally
# avoids storing the user's last-used tab in a cookie per UXWS request.
def set_active_tab
# GeoData doesn't use the tab system.
return if Feature.enabled?(:geodata)

@active_tab = if params[:tab].present? && valid_tab?(params[:tab])
# If params[:tab] is set and valid, use it and set session
cookies[:last_tab] = params[:tab]
elsif cookies[:last_tab].present? && valid_tab?(cookies[:last_tab])
# Otherwise, check for last used tab in session if valid
cookies[:last_tab]
params[:tab]
else
# Default behavior when no tab is specified in params or session
cookies[:last_tab] = 'all'
'all'
end
end

Expand Down
34 changes: 7 additions & 27 deletions test/controllers/application_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

class ApplicationControllerTest < ActionDispatch::IntegrationTest
test 'set_active_tab sets default to all when no feature flag or params' do
assert_nil cookies[:last_tab]

get root_path
assert_select '#tab-to-target' do
assert_select '[value=?]', 'all'
refute_select '[value=?]', 'geodata'
refute_select '[value=?]', 'primo'
refute_select '[value=?]', 'timdex'
end

assert_equal cookies[:last_tab], 'all'
end

test 'set_active_tab sets to geodata when feature flag enabled' do
Expand All @@ -37,8 +33,7 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
end
end

test 'set_active_tab sets to param tab when provided even if cookie is set and updates cookie' do
cookies[:last_tab] = 'timdex'
test 'set_active_tab sets to param tab when provided (param takes precedence)' do
get root_path, params: { tab: 'primo' }

assert_select '#tab-to-target' do
Expand All @@ -47,25 +42,22 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
refute_select '[value=?]', 'timdex'
refute_select '[value=?]', 'all'
end

assert_equal cookies[:last_tab], 'primo'
end

test 'set_active_tab uses cookie last_tab when no param provided' do
cookies[:last_tab] = 'timdex'
test 'set_active_tab defaults to all when no param provided' do
get root_path
assert_select '#tab-to-target' do
refute_select '[value=?]', 'all'
assert_select '[value=?]', 'all'
refute_select '[value=?]', 'primo'
assert_select '[value=?]', 'timdex'
refute_select '[value=?]', 'timdex'
end
end

test 'valid_tab returns true for valid tabs' do
app_controller = ApplicationController.new

assert app_controller.send(:valid_tab?, 'primo')
assert app_controller.send(:valid_tab?, 'timdex')
assert app_controller.send(:valid_tab?, 'timdex')
assert app_controller.send(:valid_tab?, 'all')
end

Expand All @@ -84,43 +76,31 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
assert_select '[value=?]', 'all'
refute_select '[value=?]', 'invalid_tab'
end

assert_equal cookies[:last_tab], 'all'
end

test 'set_active_tab ignores invalid cookie value and uses default' do
cookies[:last_tab] = 'invalid_cookie_value'
get root_path

assert_select '#tab-to-target' do
assert_select '[value=?]', 'all'
refute_select '[value=?]', 'invalid_cookie_value'
end

assert_equal cookies[:last_tab], 'all'
end

test 'set_active_tab prioritizes valid param over invalid cookie' do
cookies[:last_tab] = 'invalid_cookie'
get root_path, params: { tab: 'timdex' }

assert_select '#tab-to-target' do
refute_select '[value=?]', 'invalid_cookie'
assert_select '[value=?]', 'timdex'
end

assert_equal cookies[:last_tab], 'timdex'
end

test 'set_active_tab falls back to valid cookie when param is invalid' do
cookies[:last_tab] = 'primo'
test 'set_active_tab with invalid param uses default' do
get root_path, params: { tab: 'foo' }

assert_select '#tab-to-target' do
refute_select '[value=?]', 'foo'
assert_select '[value=?]', 'primo'
assert_select '[value=?]', 'all'
end

assert_equal cookies[:last_tab], 'primo'
end
end
21 changes: 2 additions & 19 deletions test/controllers/application_controller_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,20 @@ class ApplicationControllerUnitTest < ActionController::TestCase
refute @controller.send(:valid_tab?, '')
end

test 'set_active_tab defaults @active_tab to all when no params or cookies are set' do
test 'set_active_tab defaults @active_tab to all when no params are set' do
@controller.stubs(:params).returns({})
@controller.stubs(:cookies).returns({})
@controller.set_active_tab
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
end

test 'set_active_tab sets @active_tab to tab when tab params is valid' do
@controller.stubs(:params).returns({ tab: 'primo' })
@controller.stubs(:cookies).returns({})
@controller.set_active_tab
assert_equal 'primo', @controller.instance_variable_get(:@active_tab)
end

test 'set_active_tab sets @active_tab to all when tab params is invalid and no cookie is set' do
test 'set_active_tab sets @active_tab to all when tab param is invalid' do
@controller.stubs(:params).returns({ tab: 'supertab' })
@controller.stubs(:cookies).returns({})
@controller.set_active_tab
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
end

test 'set_active_tab sets @active_tab to cookie value when tab params is invalid and valid cookie is set' do
@controller.stubs(:params).returns({ tab: 'supertab' })
@controller.stubs(:cookies).returns({ last_tab: 'timdex' })
@controller.set_active_tab
assert_equal 'timdex', @controller.instance_variable_get(:@active_tab)
end

test 'set_active_tab sets @active_tab to all value when tab params is invalid and cookie is invalid' do
@controller.stubs(:params).returns({ tab: 'supertab' })
@controller.stubs(:cookies).returns({ last_tab: 'woohoo' })
@controller.set_active_tab
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
end
Expand Down