|
9 | 9 | end
|
10 | 10 |
|
11 | 11 | let(:parameter_class) { ActionController::Parameters }
|
12 |
| - let(:helper) { described_class.new(params, blacklight_config) } |
| 12 | + let(:search_state) { described_class.new(params, blacklight_config) } |
13 | 13 | let(:params) { parameter_class.new }
|
14 | 14 |
|
15 | 15 | describe "params_for_search" do
|
16 | 16 | let(:params) { parameter_class.new 'default' => 'params' }
|
17 | 17 |
|
18 | 18 | it "takes original params" do
|
19 |
| - result = helper.params_for_search |
| 19 | + result = search_state.params_for_search |
20 | 20 | expect(result).to eq({ 'default' => 'params' })
|
21 | 21 | expect(params.object_id).to_not eq result.object_id
|
22 | 22 | end
|
23 | 23 |
|
24 | 24 | it "accepts params to merge into the controller's params" do
|
25 |
| - result = helper.params_for_search(q: 'query') |
| 25 | + result = search_state.params_for_search(q: 'query') |
26 | 26 | expect(result).to eq('q' => 'query', 'default' => 'params')
|
27 | 27 | end
|
28 | 28 |
|
29 | 29 | context "when params have blacklisted keys" do
|
30 | 30 | let(:params) { parameter_class.new action: 'action', controller: 'controller', id: "id", commit: 'commit' }
|
31 | 31 | it "removes them" do
|
32 |
| - result = helper.params_for_search |
| 32 | + result = search_state.params_for_search |
33 | 33 | expect(result.keys).to_not include(:action, :controller, :commit, :id)
|
34 | 34 | end
|
35 | 35 | end
|
|
38 | 38 | context "and per_page changed" do
|
39 | 39 | let(:params) { parameter_class.new per_page: 20, page: 5 }
|
40 | 40 | it "adjusts the current page" do
|
41 |
| - result = helper.params_for_search(per_page: 100) |
| 41 | + result = search_state.params_for_search(per_page: 100) |
42 | 42 | expect(result[:page]).to eq 1
|
43 | 43 | end
|
44 | 44 | end
|
45 | 45 |
|
46 | 46 | context "and per_page didn't change" do
|
47 | 47 | let(:params) { parameter_class.new per_page: 20, page: 5 }
|
48 | 48 | it "doesn't change the current page" do
|
49 |
| - result = helper.params_for_search(per_page: 20) |
| 49 | + result = search_state.params_for_search(per_page: 20) |
50 | 50 | expect(result[:page]).to eq 5
|
51 | 51 | end
|
52 | 52 | end
|
53 | 53 |
|
54 | 54 | context "and the sort changes" do
|
55 | 55 | let(:params) { parameter_class.new sort: 'field_a', page: 5 }
|
56 | 56 | it "adjusts the current page" do
|
57 |
| - result = helper.params_for_search(sort: 'field_b') |
| 57 | + result = search_state.params_for_search(sort: 'field_b') |
58 | 58 | expect(result[:page]).to eq 1
|
59 | 59 | end
|
60 | 60 | end
|
61 | 61 |
|
62 | 62 | context "and the sort didn't change" do
|
63 | 63 | let(:params) { parameter_class.new sort: 'field_a', page: 5 }
|
64 | 64 | it "doesn't change the current page" do
|
65 |
| - result = helper.params_for_search(sort: 'field_a') |
| 65 | + result = search_state.params_for_search(sort: 'field_a') |
66 | 66 | expect(result[:page]).to eq 5
|
67 | 67 | end
|
68 | 68 | end
|
|
71 | 71 | context "with a block" do
|
72 | 72 | let(:params) { parameter_class.new a: 1, b: 2 }
|
73 | 73 | it "evalutes the block and allow it to add or remove keys" do
|
74 |
| - result = helper.params_for_search(c: 3) do |params| |
| 74 | + result = search_state.params_for_search(c: 3) do |params| |
75 | 75 | params.extract! :a, :b
|
76 | 76 | params[:d] = 'd'
|
77 | 77 | end
|
|
100 | 100 | context "when there are no pre-existing facets" do
|
101 | 101 | let(:params) { params_no_existing_facet }
|
102 | 102 | it "adds facet value" do
|
103 |
| - result_params = helper.add_facet_params("facet_field", "facet_value") |
| 103 | + result_params = search_state.add_facet_params("facet_field", "facet_value") |
104 | 104 | expect(result_params[:f]).to be_a Hash
|
105 | 105 | expect(result_params[:f]["facet_field"]).to be_a_kind_of(Array)
|
106 | 106 | expect(result_params[:f]["facet_field"]).to eq ["facet_value"]
|
107 | 107 | end
|
108 | 108 |
|
109 | 109 | it "leaves non-facet params alone" do
|
110 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 110 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
111 | 111 |
|
112 | 112 | params.each_pair do |key, value|
|
113 | 113 | next if key == :f
|
|
116 | 116 | end
|
117 | 117 |
|
118 | 118 | it "uses the facet's key in the url" do
|
119 |
| - allow(helper).to receive(:facet_configuration_for_field).with('some_field').and_return(double(single: true, field: "a_solr_field", key: "some_key")) |
| 119 | + allow(search_state).to receive(:facet_configuration_for_field).with('some_field').and_return(double(single: true, field: "a_solr_field", key: "some_key")) |
120 | 120 |
|
121 |
| - result_params = helper.add_facet_params('some_field', 'my_value') |
| 121 | + result_params = search_state.add_facet_params('some_field', 'my_value') |
122 | 122 |
|
123 | 123 | expect(result_params[:f]['some_key']).to have(1).item
|
124 | 124 | expect(result_params[:f]['some_key'].first).to eq 'my_value'
|
|
128 | 128 | context "when there are pre-existing facets" do
|
129 | 129 | let(:params) { params_existing_facets }
|
130 | 130 | it "adds a facet param to existing facet constraints" do
|
131 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 131 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
132 | 132 |
|
133 | 133 | expect(result_params[:f]).to be_a Hash
|
134 | 134 |
|
|
143 | 143 | end
|
144 | 144 |
|
145 | 145 | it "leaves non-facet params alone" do
|
146 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 146 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
147 | 147 |
|
148 | 148 | params.each_pair do |key, value|
|
149 | 149 | next if key == 'f'
|
|
155 | 155 | context "with a facet selected in the params" do
|
156 | 156 | let(:params) { parameter_class.new f: { 'single_value_facet_field' => 'other_value' } }
|
157 | 157 | it "replaces facets configured as single" do
|
158 |
| - allow(helper).to receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(double(single: true, key: "single_value_facet_field")) |
159 |
| - result_params = helper.add_facet_params('single_value_facet_field', 'my_value') |
| 158 | + allow(search_state).to receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(double(single: true, key: "single_value_facet_field")) |
| 159 | + result_params = search_state.add_facet_params('single_value_facet_field', 'my_value') |
160 | 160 |
|
161 | 161 | expect(result_params[:f]['single_value_facet_field']).to have(1).item
|
162 | 162 | expect(result_params[:f]['single_value_facet_field'].first).to eq 'my_value'
|
163 | 163 | end
|
164 | 164 | end
|
165 | 165 |
|
166 | 166 | it "accepts a FacetItem instead of a plain facet value" do
|
167 |
| - result_params = helper.add_facet_params('facet_field_1', double(value: 123)) |
| 167 | + result_params = search_state.add_facet_params('facet_field_1', double(value: 123)) |
168 | 168 |
|
169 | 169 | expect(result_params[:f]['facet_field_1']).to include(123)
|
170 | 170 | end
|
171 | 171 |
|
172 | 172 | it "defers to the field set on a FacetItem" do
|
173 |
| - result_params = helper.add_facet_params('facet_field_1', double(:field => 'facet_field_2', :value => 123)) |
| 173 | + result_params = search_state.add_facet_params('facet_field_1', double(:field => 'facet_field_2', :value => 123)) |
174 | 174 |
|
175 | 175 | expect(result_params[:f]['facet_field_1']).to be_blank
|
176 | 176 | expect(result_params[:f]['facet_field_2']).to include(123)
|
177 | 177 | end
|
178 | 178 |
|
179 | 179 | it "adds any extra fq parameters from the FacetItem" do
|
180 |
| - result_params = helper.add_facet_params('facet_field_1', double(:value => 123, fq: { 'facet_field_2' => 'abc' })) |
| 180 | + result_params = search_state.add_facet_params('facet_field_1', double(:value => 123, fq: { 'facet_field_2' => 'abc' })) |
181 | 181 |
|
182 | 182 | expect(result_params[:f]['facet_field_1']).to include(123)
|
183 | 183 | expect(result_params[:f]['facet_field_2']).to include('abc')
|
|
197 | 197 | }
|
198 | 198 |
|
199 | 199 | it "does not include request parameters used by the facet paginator" do
|
200 |
| - params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value") |
| 200 | + params = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
201 | 201 |
|
202 | 202 | bad_keys = Blacklight::Solr::FacetPaginator.request_keys.values + [:id]
|
203 | 203 | bad_keys.each do |paginator_key|
|
|
206 | 206 | end
|
207 | 207 |
|
208 | 208 | it 'removes :page request key' do
|
209 |
| - params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value") |
| 209 | + params = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
210 | 210 | expect(params).to_not have_key(:page)
|
211 | 211 | end
|
212 | 212 |
|
213 | 213 | it "otherwise does the same thing as add_facet_params" do
|
214 |
| - added_facet_params = helper.add_facet_params("facet_field_2", "facet_value") |
215 |
| - added_facet_params_from_facet_action = helper.add_facet_params_and_redirect("facet_field_2", "facet_value") |
| 214 | + added_facet_params = search_state.add_facet_params("facet_field_2", "facet_value") |
| 215 | + added_facet_params_from_facet_action = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
216 | 216 |
|
217 | 217 | expect(added_facet_params_from_facet_action).to eq added_facet_params.except(Blacklight::Solr::FacetPaginator.request_keys[:page], Blacklight::Solr::FacetPaginator.request_keys[:sort])
|
218 | 218 | end
|
|
225 | 225 | let(:facet_params) { { 'some_field' => ['some_value', 'another_value'] } }
|
226 | 226 |
|
227 | 227 | it "removes the facet / value tuple from the query parameters" do
|
228 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 228 | + params = search_state.remove_facet_params('some_field', 'some_value') |
229 | 229 | expect(params[:f]['some_field']).not_to include 'some_value'
|
230 | 230 | expect(params[:f]['some_field']).to include 'another_value'
|
231 | 231 | end
|
232 | 232 | end
|
233 | 233 |
|
234 | 234 | context "when the facet has configuration" do
|
235 | 235 | before do
|
236 |
| - allow(helper).to receive(:facet_configuration_for_field).with('some_field').and_return( |
| 236 | + allow(search_state).to receive(:facet_configuration_for_field).with('some_field').and_return( |
237 | 237 | double(single: true, field: "a_solr_field", key: "some_key"))
|
238 | 238 | end
|
239 | 239 | let(:facet_params) { { 'some_key' => ['some_value', 'another_value'] } }
|
240 | 240 |
|
241 | 241 | it "uses the facet's key configuration" do
|
242 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 242 | + params = search_state.remove_facet_params('some_field', 'some_value') |
243 | 243 | expect(params[:f]['some_key']).not_to eq 'some_value'
|
244 | 244 | expect(params[:f]['some_key']).to include 'another_value'
|
245 | 245 | end
|
|
249 | 249 | facet_params['another_field'] = ['some_value']
|
250 | 250 | facet_params['some_field'] = ['some_value']
|
251 | 251 |
|
252 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 252 | + params = search_state.remove_facet_params('some_field', 'some_value') |
253 | 253 |
|
254 | 254 | expect(params[:f]).not_to have_key 'some_field'
|
255 | 255 | end
|
256 | 256 |
|
257 | 257 | it "removes the 'f' parameter entirely when no facets remain" do
|
258 | 258 | facet_params['some_field'] = ['some_value']
|
259 | 259 |
|
260 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 260 | + params = search_state.remove_facet_params('some_field', 'some_value') |
261 | 261 |
|
262 | 262 | expect(params).not_to have_key :f
|
263 | 263 | end
|
|
0 commit comments