|
| 1 | +require_relative "../../test_helper" |
| 2 | + |
| 3 | +class ScopedSearchResultsPresenterTest < ActiveSupport::TestCase |
| 4 | + setup do |
| 5 | + @scope_title = stub |
| 6 | + @unscoped_result_count = stub |
| 7 | + |
| 8 | + @scoped_results = [{"title"=> "scoped_result_1"}, |
| 9 | + {"title"=> "scoped_result_2"}, |
| 10 | + {"title"=> "scoped_result_3"}, |
| 11 | + {"title"=> "scoped_result_4"}, |
| 12 | + ] |
| 13 | + @unscoped_results = [{"title"=> "unscoped_result_1"}, |
| 14 | + {"title"=> "unscoped_result_2"}, |
| 15 | + {"title"=> "unscoped_result_3"}, |
| 16 | + ] |
| 17 | + |
| 18 | + @search_response = { "result_count" => "50", |
| 19 | + "results" => @scoped_results, |
| 20 | + "scope" => { |
| 21 | + "title" => @scope_title, |
| 22 | + }, |
| 23 | + "unscoped_results" => { |
| 24 | + "total" => @unscoped_result_count, |
| 25 | + "results" => @unscoped_results, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + @search_parameters = stub(search_term: 'words', |
| 31 | + debug_score: 1, |
| 32 | + start: 1, |
| 33 | + count: 1, |
| 34 | + build_link: 1, |
| 35 | + ) |
| 36 | + end |
| 37 | + |
| 38 | + should "return a hash that has is_scoped set to true" do |
| 39 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters) |
| 40 | + assert_equal true, results.to_hash[:is_scoped?] |
| 41 | + end |
| 42 | + |
| 43 | + should "return a hash with the scope_title set to the scope title from the @search_response" do |
| 44 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters) |
| 45 | + assert_equal @scope_title, results.to_hash[:scope_title] |
| 46 | + end |
| 47 | + |
| 48 | + should "return a hash result count set to the scope title from the @search_response" do |
| 49 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters) |
| 50 | + assert_equal "#{@unscoped_result_count} results", results.to_hash[:unscoped_result_count] |
| 51 | + end |
| 52 | + |
| 53 | + context "presentable result list" do |
| 54 | + |
| 55 | + should "return all scoped results with unscoped results inserted at position 4" do |
| 56 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters).to_hash |
| 57 | + |
| 58 | + ## |
| 59 | + # This test is asserting that the format of `presentable_list` is: |
| 60 | + # [result, result, result, {results: list_of_results, is_multiple_results: true}, result ...] |
| 61 | + # Where list_of_results are the top three results from an unscoped request to rummager |
| 62 | + # and a flag `is_multiple_results` set to true. |
| 63 | + ## |
| 64 | + |
| 65 | + # Scoped results |
| 66 | + @scoped_results[0..2].each_with_index do | result, i | |
| 67 | + assert_equal result["title"], results[:results][i][:title] |
| 68 | + end |
| 69 | + |
| 70 | + # Check un-scoped sub-list has flag |
| 71 | + assert_equal true, results[:results][3][:is_multiple_results] |
| 72 | + |
| 73 | + # iterate unscoped sublist of results |
| 74 | + @unscoped_results.each_with_index do | result, i | |
| 75 | + assert_equal result["title"], results[:results][3][:results][i][:title] |
| 76 | + end |
| 77 | + |
| 78 | + # iterate remaining results |
| 79 | + @scoped_results[3..-1].each_with_index do | result, i | |
| 80 | + assert_equal result["title"], results[:results][i+4][:title] |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context "no scoped results returned" do |
| 86 | + setup do |
| 87 | + @no_results = [] |
| 88 | + @search_response["unscoped_results"]["results"] = @no_results |
| 89 | + end |
| 90 | + |
| 91 | + should "not not include unscoped results in the presentable_list if there aren't any" do |
| 92 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters).to_hash |
| 93 | + |
| 94 | + @scoped_results.each_with_index do | result, i | |
| 95 | + assert_equal result["title"], results[:results][i][:title] |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + should "not set unscoped_results_any? to false" do |
| 100 | + results = ScopedSearchResultsPresenter.new(@search_response, @search_parameters).to_hash |
| 101 | + assert_equal false, results.to_hash[:unscoped_results_any?] |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments