From c57ced6ac61e7ae8e26a372010d701926ac26e82 Mon Sep 17 00:00:00 2001 From: Rafael Martins Date: Fri, 24 Feb 2012 15:05:22 -0200 Subject: [PATCH] SearchHelper fixes and unit tests --- app/helpers/search_helper.rb | 21 +++++++++++++-------- test/unit/search_helper_test.rb | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+), 8 deletions(-) create mode 100644 test/unit/search_helper_test.rb diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index e02740a..db85894 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -17,7 +17,7 @@ module SearchHelper end def display_results(use_map = false) - if params[:display] == 'map' && use_map && GoogleMaps.enabled?(environment.default_hostname) + if params[:display] == 'map' && use_map partial = 'google_maps' klass = 'map' else @@ -31,15 +31,19 @@ module SearchHelper def display_map_list_button button(:search, params[:display] == 'map' ? _('Display in list') : _('Display in map'), params.merge(:display => (params[:display] == 'map' ? 'list' : 'map')), - :class => "map-toggle-button" ) if GoogleMaps.enabled?(environment.default_hostname) + :class => "map-toggle-button" ) end def city_with_state(city) - s = city.parent - if city and city.kind_of?(City) and s and s.kind_of?(State) and s.acronym - city.name + ', ' + s.acronym + if city and city.kind_of?(City) + s = city.parent + if s and s.kind_of?(State) and s.acronym + city.name + ', ' + s.acronym + else + city.name + end else - city.name + nil end end @@ -55,6 +59,7 @@ module SearchHelper end def facet_javascript(input_id, facet, array) + array = [] if array.nil? hintText = _('Type in an option') text_field_tag('facet['+input_id+']', '', :id => input_id) + javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, @@ -63,13 +68,13 @@ module SearchHelper end def facet_link_html(facet, params, value, label, count) - params = params.dup + params = params ? params.dup : {} has_extra = label.kind_of?(Array) link_label = has_extra ? label[0] : label id = facet[:solr_field].to_s params[:facet] ||= {} params[:facet][id] ||= {} - params[:page] = {} + params[:page] = {} if params[:page] selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) diff --git a/test/unit/search_helper_test.rb b/test/unit/search_helper_test.rb new file mode 100644 index 0000000..0c2661a --- /dev/null +++ b/test/unit/search_helper_test.rb @@ -0,0 +1,252 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class SearchHelperTest < ActiveSupport::TestCase + + include SearchHelper + + + + should 'display search page title' do + title = 'page_title' + assert_equal search_page_title(title), '

page_title

' + end + + should 'display search page title with category name' do + title = 'page_title' + category = mock + category.stubs(:name).returns('category_name') + assert_equal '

page_titlecategory_name

', + search_page_title(title, category) + end + + should 'display category context' do + stubs(:params).returns({:action => 'action'}) + category = mock + category.stubs(:full_name).returns('category_full_name') + expects('link_to').returns('link_to_result').once + expects('content_tag').with('div', 'category_full_name, link_to_result', anything) + category_context(category, {}) + end + + should 'display results without map' do + stubs(:params).returns({:display => ''}) + expects('render').with({:partial => 'display_results'}).returns('render_return') + expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results list') + display_results + end + + should 'display results with map' do + stubs(:params).returns({:display => 'map'}) + expects('render').with({:partial => 'google_maps'}).returns('render_return') + expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results map') + display_results true + end + + should 'show display_list button when in map view' do + stubs(:params).returns({:display => 'map'}) + expects(:button).with(:search, 'Display in list', {:display => 'list'}, anything) + display_map_list_button + end + + should 'show display_map button when in list view' do + stubs(:params).returns({:display => ''}) + expects(:button).with(:search, 'Display in map', {:display => 'map'}, anything) + display_map_list_button + end + + should 'return full city name with state' do + state = mock + state.stubs(:kind_of?).with(State).returns(true) + state.stubs(:acronym).returns('CE') + city = mock + city.stubs(:parent).returns(state) + city.stubs(:kind_of?).with(City).returns(true) + city.stubs(:name).returns('Jijoca de Jericoacoara') + assert_equal 'Jijoca de Jericoacoara, CE', city_with_state(city) + end + + should 'not return city_with_state when city is nil' do + assert_nil city_with_state nil + end + + should 'not return city_with_state when parameter is not a City' do + city = mock + city.stubs(:kind_of?).with(City).returns(false) + assert_nil city_with_state city + end + + should 'return city name when parent is not defined' do + city = mock + city.stubs(:kind_of?).with(City).returns(true) + city.stubs(:parent).returns(nil) + city.stubs(:name).returns('Feliz Deserto') + assert_equal 'Feliz Deserto', city_with_state(city) + end + + should 'return city name when parent is not a State' do + state = mock + state.stubs(:kind_of?).with(State).returns(false) + city = mock + city.stubs(:kind_of?).with(City).returns(true) + city.stubs(:parent).returns(state) + city.stubs(:name).returns('Feliz Deserto') + assert_equal 'Feliz Deserto', city_with_state(city) + end + + should 'return city name when parent has no acronym' do + state = mock + state.stubs(:kind_of?).with(State).returns(true) + state.stubs(:acronym).returns(nil) + city = mock + city.stubs(:kind_of?).with(City).returns(true) + city.stubs(:parent).returns(state) + city.stubs(:name).returns('Feliz Deserto') + assert_equal 'Feliz Deserto', city_with_state(city) + end + + should 'display facets menu' do + expects(:asset_class).with('asset') + expects(:render).with(:partial => 'facets_menu') + facets_menu 'asset', nil + end + + should 'display facets_unselect menu' do + expects(:asset_class).with('asset') + expects(:render).with(:partial => 'facets_unselect_menu') + facets_unselect_menu 'asset' + end + + should 'display facets javascript' do + expects(:text_field_tag).returns('') + expects(:javascript_tag).with(regexp_matches(/id.*[\'array_item\'].*json_message/m)).returns( + '') + stubs(:jquery_token_input_messages_json).returns('json_message') + assert_equal '', + facet_javascript('id', '', ['array_item']) + end + + should 'display empty array in facets javascript if array is nil' do + expects(:text_field_tag).returns('') + expects(:javascript_tag).with(regexp_matches(/id.*\[\].*json_message/m)).returns( + '') + stubs(:jquery_token_input_messages_json).returns('json_message') + assert_equal '', + facet_javascript('id', '', []) + end + + should 'return html code for facet link' do + facet = { + :solr_field => 'facet_solr_field', + :label_id => 'facet_label_id' + } + params = {} + value = 'facet_value' + label = 'facet_label' + count = 1 + + expected_url = {:facet => {'facet_solr_field' => { 'facet_label_id' => ['facet_value']}}} + + expects(:link_to).with('facet_label', expected_url, anything).returns('') + stubs(:content_tag).with(anything, '', anything).returns('') + stubs(:content_tag).with(anything, ' (1)', anything).returns('') + stubs(:content_tag).with(anything, '', anything).returns('') + + assert_equal '', + facet_link_html(facet, params, value, label, count) + end + + should 'return html code for facet link with extra label' do + facet = { + :solr_field => 'facet_solr_field', + :label_id => 'facet_label_id' + } + params = {} + value = 'facet_value' + label = ['facet_label', 'facet_extra'] + count = 1 + + expected_url = {:facet => {'facet_solr_field' => { 'facet_label_id' => ['facet_value']}}} + + expects(:link_to).with('facet_label', expected_url, anything).returns('') + stubs(:content_tag).with(anything, 'facet_extra', anything).returns('') + stubs(:content_tag).with(anything, ' (1)', anything).returns('') + stubs(:content_tag).with(anything, '', anything).returns('') + + assert_equal '', + facet_link_html(facet, params, value, label, count) + end + + should 'return html code for selected facet link' do + facet = { + :solr_field => 'facet_solr_field' + } + params = {:facet => {'facet_solr_field' => 'facet_value'}} + value = 'facet_value' + label = 'facet_label' + count = 1 + + expected_url = {:facet => {'facet_solr_field' => 'facet_value'}} + + expects(:link_to).with('facet_label', expected_url, anything).returns('') + stubs(:content_tag).with(anything, '', anything).returns('') + stubs(:content_tag).with(anything, ' (1)', anything).returns('') + stubs(:content_tag).with(anything, '', {:class => 'facet-menu-item facet-result-link-selected'}).returns('') + + assert_equal '', + facet_link_html(facet, params, value, label, count) + end + + should 'show html for non-hash selected facets' do + klass = mock + klass.stubs(:facet_by_id).with(:facet_id).returns('klass_facet_by_id') + klass.stubs(:facet_label).with('klass_facet_by_id').returns('klass_facet_label') + klass.stubs(:facet_result_name).with('klass_facet_by_id', 'facet_value').returns('klass_facet_result_name') + params = {:facet => {:facet_id => 'facet_value'}} + + expects(:content_tag).with(anything, 'klass_facet_label', anything).returns('') + expects(:content_tag).with(anything, 'klass_facet_result_name', anything).returns('') + expects(:link_to).with(anything, {:facet => {}}, anything).returns('') + expects(:content_tag).with(anything, '', anything).returns('') + + environment = mock + assert_match '', facet_selecteds_html_for(environment, klass, params) + end + + should 'show select tag for order_by' do + [:products, :events, :articles, :enterprises, :people, :communities].each do |asset| + params = {:order_by => 'Relevance'} + + stubs(:params).returns(params) + stubs(:options_for_select).with(instance_of(Array), params[:order_by]).returns('') + stubs(:select_tag).with(regexp_matches(/#{asset}/), '', anything).returns('') + expects(:content_tag).with(anything, regexp_matches(//), anything).returns('') + + assert_equal '', order_by(asset) + end + end + + should 'show total of assets found' do + [:products, :events, :articles, :enterprises, :people, :communities].each do |asset| + expects(:content_tag).with(anything, regexp_matches(/10.*#{asset}.*found/), anything).returns('') + assert_equal '', label_total_found(asset, 10) + end + end + + + should 'return asset class from string' do + asset_names = ['products', 'events', 'articles', 'enterprises', 'people', 'communities'] + asset_classes = [Product, Event, Article, Enterprise, Person, Community] + asset_names.each_index do |i| + assert_equal asset_classes[i], asset_class(asset_names[i]) + end + end + + should 'return asset table from string' do + asset_classes = [Product, Event, Article, Enterprise, Person, Community] + asset_tables = ['products', 'articles', 'articles', 'profiles', 'profiles', 'profiles'] + asset_classes.each_index do |i| + assert_equal asset_tables[i], asset_table(asset_classes[i]) + end + end + +end -- libgit2 0.21.2