Skip to content

Commit

Permalink
Merge pull request #3306 from hmdc/accessible-navbar-tests
Browse files Browse the repository at this point in the history
Fixes to navigation tests after accessibility improvements
  • Loading branch information
johrstrom authored Jan 19, 2024
2 parents 61a4b66 + 2d38b02 commit 68f5211
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% if @featured_group.present? %>
<% group = @featured_group %>
<li class="nav-item dropdown" role="none">
<a role="menuitem" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><%= group.title %><span class="caret"></span></a>
<a role="menuitem" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" role="menuitem" title="<%= group.title %>"><%= group.title %><span class="caret"></span></a>

<ul class="dropdown-menu" role="menu" title="<%= group.title %>">
<% OodAppGroup.groups_for(apps: group.apps, group_by: :subcategory, nav_limit: group.nav_limit).each_with_index do |g, index| %>
Expand Down
12 changes: 10 additions & 2 deletions apps/dashboard/test/html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
# integration tests.
class ActiveSupport::TestCase

def nav_link(title)
css_select("nav a.nav-link[title='#{title}']")
end

def dropdown_list(title)
css_select("li.dropdown[title='#{title}'] ul")
css_select("nav a.nav-link.dropdown-toggle[title='#{title}'] + ul")
end

def dropdown_links
'nav a.nav-link.dropdown-toggle[title]'
end

def dropdown_link(order)
".navbar-expand-md > #navbar li.dropdown:nth-of-type(#{order}) a"
"nav #navbar li.dropdown:nth-of-type(#{order}) a[title]"
end

# given a dropdown list, return the list items as an array of strings
Expand Down
114 changes: 45 additions & 69 deletions apps/dashboard/test/integration/custom_help_navigation_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'test_helper'
require 'html_helper'

class CustomHelpNavigationTest < ActionDispatch::IntegrationTest
test 'should render a custom help navigation menu when help_bar is defined in UserConfiguration' do
Expand All @@ -19,7 +20,8 @@ class CustomHelpNavigationTest < ActionDispatch::IntegrationTest
url: '/menu/link',
new_tab: false },
{ title: 'Profile Link',
profile: 'profile1' }
profile: 'profile1',
icon: 'fa://user'}
] },
{ title: 'Docs Menu',
links: [
Expand All @@ -40,60 +42,52 @@ class CustomHelpNavigationTest < ActionDispatch::IntegrationTest
assert_response :success

# Check nav menus
assert_select '#navbar li.dropdown[title]', 2

# Check custom "Help Menu"
assert_select nav_menu(1) do |menu|
assert_select menu.first, 'a', text: 'Help Menu'
assert_select menu.first, 'ul' do |menu_items|
menu_items.first['class'].include?('dropdown-menu-right')
end
end
assert_select nav_menu_header('Help Menu'), text: 'Help Menu Dropdown Header'
assert_select nav_menu_links('Help Menu') do |elements|
assert_equal 2, elements.size
assert_match(/Menu Link/, elements[0].text)
assert_equal '/menu/link', elements[0]['href']
assert_nil elements[0]['target']
check_icon(elements[0], 'fa-cog')
end
assert_select dropdown_links, 2
assert_select dropdown_link(1), text: 'Help Menu'
assert_select dropdown_link(2), text: 'Docs Menu'

# Check custom 'Help Menu'
menu_items = dropdown_list('Help Menu')
assert_equal true, menu_items.first['class'].include?('dropdown-menu-right')
menu_links = css_select(menu_items, 'a')
assert_equal 2, menu_links.size
# Check first expected link
assert_match(/Menu Link/, menu_links[0].text)
assert_equal '/menu/link', menu_links[0]['href']
assert_nil menu_links[0]['target']
check_icon(menu_links[0], 'fa-cog')
# Check second expected link
assert_match(/Profile Link/, menu_links[1].text)
assert_equal settings_path("settings[profile]" => "profile1"), menu_links[1]['href']
assert_nil menu_links[1]['target']
check_icon(menu_links[1], 'fa-user')

# Check custom 'Docs Menu'
assert_select nav_menu(2) do |menu|
assert_select menu.first, 'a', text: 'Docs Menu'
assert_select menu.first, 'ul' do |menu_items|
menu_items.first['class'].include?('dropdown-menu-right')
end
end
assert_select nav_menu_header('Docs Menu'), text: 'Docs Menu Dropdown Header'
assert_select nav_menu_links('Docs Menu') do |elements|
assert_equal 1, elements.size
assert_match(/Jupyter Notebook/, elements[0].text)
assert_equal '/batch_connect/sys/bc_jupyter/session_contexts/new', elements[0]['href']
assert_nil elements[0]['target']
check_icon(elements[0], 'fa-gear')
end
menu_items = dropdown_list('Docs Menu')
assert_equal true, menu_items.first['class'].include?('dropdown-menu-right')
menu_links = css_select(menu_items, 'a')
assert_equal 1, menu_links.size
# Check first expected link
assert_match(/Jupyter Notebook/, menu_links[0].text)
assert_equal '/batch_connect/sys/bc_jupyter/session_contexts/new', menu_links[0]['href']
assert_nil menu_links[0]['target']
check_icon(menu_links[0], 'fa-gear')

# Check custom link
assert_select nav_link('Custom Help Link'), text: 'Custom Help Link'
assert_select nav_link('Custom Help Link') do |link|
assert_equal '/custom/link', link.first['href']
assert_equal '_blank', link.first['target']
check_icon(link.first, 'fa-desktop')
end

link = nav_link('Custom Help Link')[0]
assert_match /Custom Help Link/, link.text
assert_equal '/custom/link', link['href']
assert_equal '_blank', link['target']
check_icon(link, 'fa-desktop')
# User
assert_select '#navbar .navbar-nav a.nav-link.disabled' do |link|
assert_match(/Logged in as/, link.first.text.strip)
check_icon(link.first, 'fa-user')
end
user = nav_link("Logged in as #{CurrentUser.name}")[0]
check_icon(user, 'fa-user')

# Log out link
assert_select "#navbar .navbar-nav a.nav-link[href='/logout']" do |link|
assert_equal '/logout', link.first['href']
assert_equal 'Log Out', link.first.text.strip
check_icon(link.first, 'fa-sign-out-alt')
end
logout = nav_link('Log Out')[0]
assert_match /Log Out/, logout.text
assert_equal '/logout', logout['href']
check_icon(logout, 'fa-sign-out-alt')
end

test 'develop template should not render when Configuration.app_development_enabled? is false' do
Expand All @@ -107,31 +101,13 @@ class CustomHelpNavigationTest < ActionDispatch::IntegrationTest
Configuration.stubs(:app_development_enabled?).returns(true)
get root_path
assert_response :success
assert_select '#navbar li.dropdown[title]', 1
assert_select nav_menu(1) do |menu|
assert_select menu.first, 'a', text: 'Develop'
end
assert_select dropdown_links, 1
assert_select dropdown_link(1), text: 'Develop'

Configuration.stubs(:app_development_enabled?).returns(false)
get root_path
assert_response :success
assert_select '#navbar li.dropdown[title]', 0
end

def nav_menu(order)
"#navbar .navbar-nav li.dropdown:nth-of-type(#{order})"
end

def nav_menu_links(title)
"#navbar .navbar-nav li.dropdown[title='#{title}'] ul.dropdown-menu a"
end

def nav_menu_header(title)
"#navbar .navbar-nav li.dropdown[title='#{title}'] ul.dropdown-menu li.dropdown-header"
end

def nav_link(title)
"#navbar .navbar-nav a.nav-link[title='#{title}']"
assert_select dropdown_links, 0
end

def check_icon(parent_element, icon_class)
Expand Down
110 changes: 51 additions & 59 deletions apps/dashboard/test/integration/custom_navigation_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'test_helper'
require 'html_helper'

class CustomNavigationTest < ActionDispatch::IntegrationTest
def setup
Expand Down Expand Up @@ -40,52 +41,59 @@ def setup

get root_path
assert_response :success
# Check nav menus
assert_select '#navbar li.dropdown[title]', 3 # +1 here is 'Help'
# Check nav links
assert_select '#navbar > ul > a.nav-link[title]', 2

assert_select nav_menu(1), text: 'Custom Menu'
assert_select nav_menu_header('Custom Menu'), text: 'Custom Menu Dropdown Header'
assert_select nav_menu_links('Custom Menu') do |elements|
assert_equal 2, elements.size
assert_match(/Menu Link/, elements[0].text)
assert_equal '/menu/link', elements[0]['href']
assert_nil elements[0]['target']
check_icon(elements[0], 'fa-cog')

assert_match(/Profile Link/, elements[1].text)
assert_equal '/settings?settings%5Bprofile%5D=profile1', elements[1]['href']
assert_nil elements[1]['target']
check_icon(elements[1], 'fa-cog')
end

assert_select nav_menu(2), text: 'Custom Apps'
assert_select nav_menu_header('Custom Apps'), text: 'Custom Apps Dropdown Header'
assert_select nav_menu_links('Custom Apps') do |elements|
assert_equal 1, elements.size
assert_match(/Jupyter Notebook/, elements[0].text)
assert_equal '/batch_connect/sys/bc_jupyter/session_contexts/new', elements[0]['href']
assert_nil elements[0]['target']
check_icon(elements[0], 'fa-gear')
end

assert_select nav_link('Paraview'), text: 'Paraview'
assert_select nav_link('Paraview') do |link|
assert_equal '/batch_connect/sys/bc_paraview/session_contexts/new', link.first['href']
assert_nil link.first['target']
check_icon(link.first, 'fa-gear')
end

assert_select nav_link('Custom Link'), text: 'Custom Link'
assert_select nav_link('Custom Link') do |link|
assert_equal '/custom/link', link.first['href']
assert_equal '_blank', link.first['target']
check_icon(link.first, 'fa-desktop')
end
# Check nav menus
assert_select dropdown_links, 3
assert_select dropdown_link(1), text: 'Custom Menu'
assert_select dropdown_link(2), text: 'Custom Apps'
assert_select dropdown_link(3), text: 'Help'

menu_items = dropdown_list('Custom Menu')
menu_links = css_select(menu_items, 'a')
assert_equal 2, menu_links.size
# Check first expected link
assert_match(/Menu Link/, menu_links[0].text)
assert_equal '/menu/link', menu_links[0]['href']
assert_nil menu_links[0]['target']
check_icon(menu_links[0], 'fa-cog')
# Check second expected link
assert_match(/Profile Link/, menu_links[1].text)
assert_equal settings_path("settings[profile]" => "profile1"), menu_links[1]['href']
assert_nil menu_links[1]['target']
check_icon(menu_links[1], 'fa-cog')

menu_headers = css_select(menu_items, 'li.dropdown-header')
assert_equal 1, menu_headers.size
assert_match(/Custom Menu Dropdown Header/, menu_headers[0].text)

menu_items = dropdown_list('Custom Apps')
menu_links = css_select(menu_items, 'a')
assert_equal 1, menu_links.size
# Check first expected link
assert_match(/Jupyter Notebook/, menu_links[0].text)
assert_equal '/batch_connect/sys/bc_jupyter/session_contexts/new', menu_links[0]['href']
assert_nil menu_links[0]['target']
check_icon(menu_links[0], 'fa-gear')

menu_headers = css_select(menu_items, 'li.dropdown-header')
assert_equal 1, menu_headers.size
assert_match(/Custom Apps Dropdown Header/, menu_headers[0].text)

# Check custom links
link = nav_link('Paraview')[0]
assert_match /Paraview/, link.text
assert_equal '/batch_connect/sys/bc_paraview/session_contexts/new', link['href']
assert_nil link['target']
check_icon(link, 'fa-gear')

link = nav_link('Custom Link')[0]
assert_match /Custom Link/, link.text
assert_equal '/custom/link', link['href']
assert_equal '_blank', link['target']
check_icon(link, 'fa-desktop')

# Check all_apps static link.
assert_select "#navbar .navbar-nav li.nav-item[title='All Apps']", text: 'All Apps'
assert_equal 1, nav_link('All Apps').length
end

test 'featured_apps template should not break navigation when no pinned_apps defined' do
Expand All @@ -100,22 +108,6 @@ def setup
assert_response :success
end

def nav_menu(order)
"#navbar .navbar-nav li.dropdown:nth-of-type(#{order}) a"
end

def nav_menu_links(title)
"#navbar .navbar-nav li.dropdown[title='#{title}'] ul.dropdown-menu a"
end

def nav_menu_header(title)
"#navbar .navbar-nav li.dropdown[title='#{title}'] ul.dropdown-menu li.dropdown-header"
end

def nav_link(title)
"#navbar .navbar-nav a.nav-link[title='#{title}']"
end

def check_icon(parent_element, icon_class)
assert_select parent_element, 'i' do |icons|
assert_equal true, icons.first['class'].include?(icon_class)
Expand Down
19 changes: 13 additions & 6 deletions apps/dashboard/test/integration/dashboard_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def teardown

get root_path
assert_response :success
assert_select '.navbar-collapse > .nav li.dropdown[title]', 0
assert_select dropdown_links, 1 # +1 here is 'Help'
assert_select dropdown_link(1), text: 'Help' # ensure is Help
end

test 'should exclude gateway apps if NavConfig.categories is set to default and whitelist is enabled' do
Expand All @@ -167,7 +168,12 @@ def teardown

get root_path
assert_response :success
assert_select ".navbar-collapse > .nav li.dropdown[title='Gateway Apps']", 0
assert_select dropdown_links, 5 # +1 here is 'Help'
assert_select dropdown_link(1), text: 'Files'
assert_select dropdown_link(2), text: 'Jobs'
assert_select dropdown_link(3), text: 'Clusters'
assert_select dropdown_link(4), text: 'Interactive Apps'
assert_select dropdown_link(5), text: 'Help'
end

test 'uses NavConfig.categories as sort order if whitelist is false' do
Expand All @@ -178,7 +184,7 @@ def teardown

get root_path
assert_response :success
assert_select '.navbar-expand-md > #navbar li.dropdown[title]', 6 # +1 here is 'Help'
assert_select dropdown_links, 6 # +1 here is 'Help'
assert_select dropdown_link(1), text: 'Jobs'
assert_select dropdown_link(2), text: 'Interactive Apps'
assert_select dropdown_link(3), text: 'Files'
Expand All @@ -198,7 +204,7 @@ def teardown

get root_path
assert_response :success
assert_select '.navbar-expand-md > #navbar li.dropdown[title]', 4 # +1 here is 'Help'
assert_select dropdown_links, 4 # +1 here is 'Help'
assert_select dropdown_link(1), text: 'Files'
assert_select dropdown_link(2), text: 'Interactive Apps'
assert_select dropdown_link(3), text: 'Clusters'
Expand All @@ -216,7 +222,8 @@ def teardown

get root_path
assert_response :success
assert_select '.navbar-collapse > .nav li.dropdown[title]', 0
assert_select dropdown_links, 1 # +1 here is 'Help'
assert_select dropdown_link(1), text: 'Help' # ensure is Help
end

test 'verify default values for NavConfig' do
Expand All @@ -232,7 +239,7 @@ def teardown

get root_path
assert_response :success
assert_select '.navbar-expand-md > #navbar li.dropdown[title]', 6 # +1 here is 'Help'
assert_select dropdown_links, 6 # +1 here is 'Help'

assert_select dropdown_link(1), text: 'Clusters'
assert_select dropdown_link(2), text: 'Files'
Expand Down

0 comments on commit 68f5211

Please sign in to comment.