From 2915602b8ef76113a9cd8b63d10f641041addfd9 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Wed, 3 Jan 2024 01:45:28 +0100 Subject: [PATCH] add tree view links controller tests --- app/components/tree_link_component.rb | 5 ++- app/controllers/application_controller.rb | 3 +- app/controllers/concepts_controller.rb | 6 ++-- app/controllers/properties_controller.rb | 5 ++- app/helpers/application_helper.rb | 12 ++----- app/helpers/components_helper.rb | 4 +-- .../controllers/ontologies_controller_test.rb | 32 +++++++++++++++---- 7 files changed, 41 insertions(+), 26 deletions(-) diff --git a/app/components/tree_link_component.rb b/app/components/tree_link_component.rb index 4fd5ec79a6..18e3dabe63 100644 --- a/app/components/tree_link_component.rb +++ b/app/components/tree_link_component.rb @@ -48,11 +48,14 @@ def li_id @child.id.eql?('bp_fake_root') ? 'bp_fake_root' : short_uuid end + def self.tree_close_icon + "".html_safe + end def open_children_link return unless @child.hasChildren if @child.expanded? - tree_close_icon + self.class.tree_close_icon else content_tag('turbo_frame', id: "#{child_id}_open_link") do link_to @children_link, diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index dd70b770a6..21f424018c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -430,7 +430,8 @@ def get_class(params) @root.children = @roots.sort{|x,y| (x.prefLabel || "").downcase <=> (y.prefLabel || "").downcase} # get the initial concept to display - root_child = @root.children.first + root_child = @root.children&.first + not_found("Missing roots #{@roots.id}") if root_child.nil? @concept = root_child.explore.self(full: true, lang: lang) # Some ontologies have "too many children" at their root. These will not process and are handled here. diff --git a/app/controllers/concepts_controller.rb b/app/controllers/concepts_controller.rb index ba1ace6104..852d6c019a 100644 --- a/app/controllers/concepts_controller.rb +++ b/app/controllers/concepts_controller.rb @@ -54,7 +54,7 @@ def show @concept.children = @concept.explore.children(pagesize: 750, concept_schemes: Array(@schemes).join(','), language: request_lang, display: 'prefLabel,obsolete,hasChildren').collection || [] @concept.children.sort! { |x, y| (x.prefLabel || "").downcase <=> (y.prefLabel || "").downcase } unless @concept.children.empty? render turbo_stream: [ - replace(helpers.child_id(@concept) + '_open_link') { helpers.tree_close_icon }, + replace(helpers.child_id(@concept) + '_open_link') { TreeLinkComponent.tree_close_icon }, replace(helpers.child_id(@concept) + '_childs') do helpers.concepts_tree_component(@concept, @concept, @ontology.acronym, Array(@schemes), request_lang, sub_tree: true) end @@ -95,12 +95,12 @@ def show_tree return end @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - if @ontology.nil? + if @ontology.nil? || @ontology.errors ontology_not_found(params[:ontology]) else get_class(params) #application_controller render inline: helpers.concepts_tree_component(@root, @concept, - @ontology.acronym, params[:concept_schemes]&.split(','), request_lang, + @ontology.acronym, Array(params[:concept_schemes]&.split(',')), request_lang, id: 'concepts_tree_view', auto_click: params[:auto_click] || true) end end diff --git a/app/controllers/properties_controller.rb b/app/controllers/properties_controller.rb index 5ea015b88e..7784c5eb7e 100644 --- a/app/controllers/properties_controller.rb +++ b/app/controllers/properties_controller.rb @@ -11,11 +11,14 @@ def show_tree @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first ontology_not_found(params[:ontology]) if @ontology.nil? @root = OpenStruct.new({children: property_roots(params[:ontology])}) + not_found(@root.children.errors.join) if @root.children.respond_to?(:errors) + if params[:propertyid] @property = get_property(params[:propertyid]) else @property ||= @root.children.first end + render inline: helpers.property_tree_component(@root, @property, @ontology.acronym, request_lang, id: 'properties_tree_view', auto_click: true) @@ -29,7 +32,7 @@ def show_children @property.children = property_children(id, acronym) render turbo_stream: [ - replace(helpers.child_id(@property) + '_open_link') { helpers.tree_close_icon }, + replace(helpers.child_id(@property) + '_open_link') { TreeLinkComponent.tree_close_icon }, replace(helpers.child_id(@property) + '_childs') do helpers.property_tree_component(@property, @property, acronym, request_lang, sub_tree: true) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e6a6d05249..56853c7de4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -299,17 +299,7 @@ def get_groups_data(groups = nil) @groups_for_js = @groups_map.to_json end - def metadata_for_select - get_metadata - return @metadata_for_select - end - def get_metadata - @metadata_for_select = [] - submission_metadata.each do |data| - @metadata_for_select << data["attribute"] - end - end def ontologies_to_acronyms(ontologyIDs) @@ -324,6 +314,8 @@ def at_slice? !@subdomain_filter.nil? && !@subdomain_filter[:active].nil? && @subdomain_filter[:active] == true end + + # TODO this helper is not used but can be usefully def truncate_with_more(text, options = {}) length ||= options[:length] ||= 30 trailing_text ||= options[:trailing_text] ||= " ... " diff --git a/app/helpers/components_helper.rb b/app/helpers/components_helper.rb index e550710312..c1aa0e1fb5 100644 --- a/app/helpers/components_helper.rb +++ b/app/helpers/components_helper.rb @@ -23,9 +23,7 @@ def tree_component(root, selected, target_frame:, sub_tree: false, id: nil, auto end end - def tree_close_icon - content_tag(:i, nil, class: "fas fa-chevron-down text-primary", data:{action:'click->simple-tree#toggleChildren'}) - end + def chart_component(title: '', type: , labels: , datasets: , index_axis: 'x', show_legend: false) diff --git a/test/controllers/ontologies_controller_test.rb b/test/controllers/ontologies_controller_test.rb index b5998de7a6..eeefd4b728 100644 --- a/test/controllers/ontologies_controller_test.rb +++ b/test/controllers/ontologies_controller_test.rb @@ -11,14 +11,32 @@ class OntologiesControllerTest < ActionDispatch::IntegrationTest assert_response :success end - ONTOLOGIES.flat_map { |ont| PAGES.map { |page| [ont, page] } }.each do |ont, page| - test "should get page #{page} of #{ont.acronym} ontology" do - path = "#{ontologies_path}/#{ont.acronym}?p=#{page}" - get path - if response.redirect? - follow_redirect! + ONTOLOGIES.each do |ont| + PAGES.each do |page| + test "should get page #{page} of #{ont.acronym} ontology" do + path = "#{ontologies_path}/#{ont.acronym}?p=#{page}" + get path + if response.redirect? + follow_redirect! + end + assert_response :success, "GET #{path} returned #{response.status}" end - assert_response :success, "GET #{path} returned #{response.status}" + end + + test "should open the tree views of #{ont.acronym} ontology" do + paths = [ + ajax_classes_treeview_path(ontology: ont.acronym), + ajax_properties_treeview_path(ontology: ont.acronym) + ] + paths.each do |path| + begin + get path + assert_includes [404, 200], response.status, "GET #{path} returned #{response.status}" + rescue StandardError => e + assert_equal ActiveRecord::RecordNotFound, e.class + end + end + end end end