11# frozen_string_literal: true
22
33RSpec . describe Blacklight ::Solr ::Response ::Facets , :api do
4+ let ( :search_builder ) do
5+ Blacklight ::SearchBuilder . new ( view_context )
6+ end
7+ let ( :view_context ) do
8+ double ( "View context" , blacklight_config : CatalogController . blacklight_config . deep_copy )
9+ end
10+
411 describe Blacklight ::Solr ::Response ::Facets ::FacetField do
512 describe "A field with default options" do
613 subject ( :field ) { described_class . new "my_field" , [ ] }
6067 end
6168
6269 describe "#aggregations" do
63- subject { Blacklight ::Solr ::Response . new ( { responseHeader : response_header , facet_counts : { facet_fields : [ facet_field ] } } , request_params ) }
70+ subject { Blacklight ::Solr ::Response . new ( { responseHeader : { } , facet_counts : { facet_fields : [ facet_field ] } } , search_builder ) }
6471
6572 let ( :facet_field ) { [ 'my_field' , [ ] ] }
66- let ( :response_header ) { { params : request_params } }
67- let ( :request_params ) { { } }
6873
6974 describe "#limit" do
70- it "extracts a field-specific limit value" do
71- request_params [ 'f.my_field.facet.limit' ] = "10"
72- request_params [ 'facet.limit' ] = "15"
73- expect ( subject . aggregations [ 'my_field' ] . limit ) . to eq 10
75+ context 'with a field-specific limit value' do
76+ before do
77+ search_builder . merge ( 'f.my_field.facet.limit' => '10' , 'facet.limit' => '15' )
78+ end
79+
80+ it "extracts a field-specific limit value" do
81+ expect ( subject . aggregations [ 'my_field' ] . limit ) . to eq 10
82+ end
7483 end
7584
76- it "extracts a global limit value" do
77- request_params [ 'facet.limit' ] = "15"
78- expect ( subject . aggregations [ 'my_field' ] . limit ) . to eq 15
85+ context 'with a global limit value' do
86+ before do
87+ search_builder . merge ( 'facet.limit' => '15' )
88+ end
89+
90+ it "extracts a global limit value" do
91+ expect ( subject . aggregations [ 'my_field' ] . limit ) . to eq 15
92+ end
7993 end
8094
8195 it "is the solr default limit if no value is found" do
8498 end
8599
86100 describe "#offset" do
87- it "extracts a field-specific offset value" do
88- request_params [ 'f.my_field.facet.offset' ] = "10"
89- request_params [ 'facet.offset' ] = "15"
90- expect ( subject . aggregations [ 'my_field' ] . offset ) . to eq 10
101+ context 'with a field-specific offset value' do
102+ before do
103+ search_builder . merge ( 'f.my_field.facet.offset' => '10' , 'facet.offset' => '15' )
104+ end
105+
106+ it "extracts a field-specific offset value" do
107+ expect ( subject . aggregations [ 'my_field' ] . offset ) . to eq 10
108+ end
91109 end
92110
93- it "extracts a global offset value" do
94- request_params [ 'facet.offset' ] = "15"
95- expect ( subject . aggregations [ 'my_field' ] . offset ) . to eq 15
111+ context 'with a global offset value' do
112+ before do
113+ search_builder . merge ( 'facet.offset' => '15' )
114+ end
115+
116+ it "extracts a global offset value" do
117+ expect ( subject . aggregations [ 'my_field' ] . offset ) . to eq 15
118+ end
96119 end
97120
98121 it "is nil if no value is found" do
101124 end
102125
103126 describe "#sort" do
104- it "extracts a field-specific sort value" do
105- request_params [ 'f.my_field.facet.sort' ] = "alpha"
106- request_params [ 'facet.sort' ] = "index"
107- expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'alpha'
127+ context 'with a field-specific sort value' do
128+ before do
129+ search_builder . merge ( 'f.my_field.facet.sort' => 'alpha' , 'facet.sort' => 'index' )
130+ end
131+
132+ it "extracts a field-specific sort value" do
133+ expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'alpha'
134+ end
108135 end
109136
110- it "extracts a global sort value" do
111- request_params [ 'facet.sort' ] = "alpha"
112- expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'alpha'
137+ context 'with a global sort value' do
138+ before do
139+ search_builder . merge ( 'facet.sort' => 'alpha' )
140+ end
141+
142+ it "extracts a global sort value" do
143+ expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'alpha'
144+ end
113145 end
114146
115147 it "defaults to count if no value is found and the default limit is used" do
116148 expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'count'
117149 expect ( subject . aggregations [ 'my_field' ] . count? ) . to be true
118150 end
119151
120- it "defaults to index if no value is found and the limit is unlimited" do
121- request_params [ 'facet.limit' ] = -1
122- expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'index'
123- expect ( subject . aggregations [ 'my_field' ] . index? ) . to be true
152+ context 'when no value is found and the limit is unlimited' do
153+ before do
154+ search_builder . merge ( 'facet.limit' => -1 )
155+ end
156+
157+ it "defaults to index" do
158+ expect ( subject . aggregations [ 'my_field' ] . sort ) . to eq 'index'
159+ expect ( subject . aggregations [ 'my_field' ] . index? ) . to be true
160+ end
124161 end
125162 end
126163
127164 describe '#prefix' do
128- it 'extracts field-specific prefix values' do
129- request_params [ 'f.my_field.facet.prefix' ] = "a"
130- request_params [ 'facet.prefix' ] = "b"
131- expect ( subject . aggregations [ 'my_field' ] . prefix ) . to eq 'a'
165+ context 'with a field-specific prefix value' do
166+ before do
167+ search_builder . merge ( 'f.my_field.facet.prefix' => 'a' , 'facet.prefix' => 'b' )
168+ end
169+
170+ it "extracts a field-specific prefix value" do
171+ expect ( subject . aggregations [ 'my_field' ] . prefix ) . to eq 'a'
172+ end
132173 end
133174
134- it "extracts a global sort value" do
135- request_params [ 'facet.prefix' ] = "abc"
136- expect ( subject . aggregations [ 'my_field' ] . prefix ) . to eq 'abc'
175+ context 'with a global prefix value' do
176+ before do
177+ search_builder . merge ( 'facet.prefix' => 'abc' )
178+ end
179+
180+ it "extracts a global prefix value" do
181+ expect ( subject . aggregations [ 'my_field' ] . prefix ) . to eq 'abc'
182+ end
137183 end
138184
139185 it "defaults to no prefix value" do
149195 end
150196
151197 describe "#merge_facet" do
152- let ( :response ) { Blacklight ::Solr ::Response . new ( facet_counts , { } , { } ) }
198+ let ( :response ) { Blacklight ::Solr ::Response . new ( facet_counts , search_builder , { } ) }
153199 let ( :facet ) { { name : "foo" , value : "bar" , hits : 1 } }
154200
155201 before do
@@ -186,7 +232,7 @@ def facet_counts
186232 end
187233
188234 context "facet.missing" do
189- subject { Blacklight ::Solr ::Response . new ( response , { } ) }
235+ subject { Blacklight ::Solr ::Response . new ( response , search_builder ) }
190236
191237 let ( :response ) do
192238 {
@@ -213,7 +259,7 @@ def facet_counts
213259 end
214260
215261 describe "query facets" do
216- subject { Blacklight ::Solr ::Response . new ( response , { } , blacklight_config : blacklight_config ) }
262+ subject { Blacklight ::Solr ::Response . new ( response , search_builder , blacklight_config : blacklight_config ) }
217263
218264 let ( :facet_config ) do
219265 double (
@@ -288,7 +334,7 @@ def facet_counts
288334 end
289335
290336 describe "pivot facets" do
291- subject { Blacklight ::Solr ::Response . new ( response , { } , blacklight_config : blacklight_config ) }
337+ subject { Blacklight ::Solr ::Response . new ( response , search_builder , blacklight_config : blacklight_config ) }
292338
293339 let ( :facet_config ) do
294340 double ( key : 'my_pivot_facet_field' , query : nil , pivot : %w[ field_a field_b ] )
@@ -330,7 +376,7 @@ def facet_counts
330376 end
331377
332378 describe 'json facets' do
333- subject { Blacklight ::Solr ::Response . new ( response , { } , blacklight_config : blacklight_config ) }
379+ subject { Blacklight ::Solr ::Response . new ( response , search_builder , blacklight_config : blacklight_config ) }
334380
335381 let ( :response ) do
336382 {
0 commit comments