|
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 | + describe '#to_h' do |
| 16 | + let(:data) { { a: '1'} } |
| 17 | + let(:params) { parameter_class.new data } |
| 18 | + |
| 19 | + it 'returns a copy of the original parameters' do |
| 20 | + expect(search_state.to_h).to eq data.with_indifferent_access |
| 21 | + expect(search_state.to_h.object_id).not_to eq params.object_id |
| 22 | + end |
| 23 | + |
| 24 | + context 'with AC::Parameters' do |
| 25 | + let(:parameter_class) { ActionController::Parameters } |
| 26 | + |
| 27 | + it 'returns the hash data' do |
| 28 | + expect(search_state.to_h).to eq data.with_indifferent_access |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'with HashWithIndifferentAccess' do |
| 33 | + let(:parameter_class) { HashWithIndifferentAccess } |
| 34 | + |
| 35 | + it 'returns the hash data' do |
| 36 | + expect(search_state.to_h).to eq data.with_indifferent_access |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context 'with Hash' do |
| 41 | + let(:params) { data } |
| 42 | + |
| 43 | + it 'returns the hash data' do |
| 44 | + expect(search_state.to_h).to eq data.with_indifferent_access |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
15 | 49 | describe "params_for_search" do
|
16 | 50 | let(:params) { parameter_class.new 'default' => 'params' }
|
17 | 51 |
|
18 | 52 | it "takes original params" do
|
19 |
| - result = helper.params_for_search |
| 53 | + result = search_state.params_for_search |
20 | 54 | expect(result).to eq({ 'default' => 'params' })
|
21 | 55 | expect(params.object_id).to_not eq result.object_id
|
22 | 56 | end
|
23 | 57 |
|
24 | 58 | it "accepts params to merge into the controller's params" do
|
25 |
| - result = helper.params_for_search(q: 'query') |
| 59 | + result = search_state.params_for_search(q: 'query') |
26 | 60 | expect(result).to eq('q' => 'query', 'default' => 'params')
|
27 | 61 | end
|
28 | 62 |
|
29 | 63 | context "when params have blacklisted keys" do
|
30 | 64 | let(:params) { parameter_class.new action: 'action', controller: 'controller', id: "id", commit: 'commit' }
|
31 | 65 | it "removes them" do
|
32 |
| - result = helper.params_for_search |
| 66 | + result = search_state.params_for_search |
33 | 67 | expect(result.keys).to_not include(:action, :controller, :commit, :id)
|
34 | 68 | end
|
35 | 69 | end
|
|
38 | 72 | context "and per_page changed" do
|
39 | 73 | let(:params) { parameter_class.new per_page: 20, page: 5 }
|
40 | 74 | it "adjusts the current page" do
|
41 |
| - result = helper.params_for_search(per_page: 100) |
| 75 | + result = search_state.params_for_search(per_page: 100) |
42 | 76 | expect(result[:page]).to eq 1
|
43 | 77 | end
|
44 | 78 | end
|
45 | 79 |
|
46 | 80 | context "and per_page didn't change" do
|
47 | 81 | let(:params) { parameter_class.new per_page: 20, page: 5 }
|
48 | 82 | it "doesn't change the current page" do
|
49 |
| - result = helper.params_for_search(per_page: 20) |
| 83 | + result = search_state.params_for_search(per_page: 20) |
50 | 84 | expect(result[:page]).to eq 5
|
51 | 85 | end
|
52 | 86 | end
|
53 | 87 |
|
54 | 88 | context "and the sort changes" do
|
55 | 89 | let(:params) { parameter_class.new sort: 'field_a', page: 5 }
|
56 | 90 | it "adjusts the current page" do
|
57 |
| - result = helper.params_for_search(sort: 'field_b') |
| 91 | + result = search_state.params_for_search(sort: 'field_b') |
58 | 92 | expect(result[:page]).to eq 1
|
59 | 93 | end
|
60 | 94 | end
|
61 | 95 |
|
62 | 96 | context "and the sort didn't change" do
|
63 | 97 | let(:params) { parameter_class.new sort: 'field_a', page: 5 }
|
64 | 98 | it "doesn't change the current page" do
|
65 |
| - result = helper.params_for_search(sort: 'field_a') |
| 99 | + result = search_state.params_for_search(sort: 'field_a') |
66 | 100 | expect(result[:page]).to eq 5
|
67 | 101 | end
|
68 | 102 | end
|
|
71 | 105 | context "with a block" do
|
72 | 106 | let(:params) { parameter_class.new a: 1, b: 2 }
|
73 | 107 | it "evalutes the block and allow it to add or remove keys" do
|
74 |
| - result = helper.params_for_search(c: 3) do |params| |
| 108 | + result = search_state.params_for_search(c: 3) do |params| |
75 | 109 | params.extract! :a, :b
|
76 | 110 | params[:d] = 'd'
|
77 | 111 | end
|
|
100 | 134 | context "when there are no pre-existing facets" do
|
101 | 135 | let(:params) { params_no_existing_facet }
|
102 | 136 | it "adds facet value" do
|
103 |
| - result_params = helper.add_facet_params("facet_field", "facet_value") |
| 137 | + result_params = search_state.add_facet_params("facet_field", "facet_value") |
104 | 138 | expect(result_params[:f]).to be_a Hash
|
105 | 139 | expect(result_params[:f]["facet_field"]).to be_a_kind_of(Array)
|
106 | 140 | expect(result_params[:f]["facet_field"]).to eq ["facet_value"]
|
107 | 141 | end
|
108 | 142 |
|
109 | 143 | it "leaves non-facet params alone" do
|
110 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 144 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
111 | 145 |
|
112 | 146 | params.each_pair do |key, value|
|
113 | 147 | next if key == :f
|
|
116 | 150 | end
|
117 | 151 |
|
118 | 152 | 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")) |
| 153 | + 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 | 154 |
|
121 |
| - result_params = helper.add_facet_params('some_field', 'my_value') |
| 155 | + result_params = search_state.add_facet_params('some_field', 'my_value') |
122 | 156 |
|
123 | 157 | expect(result_params[:f]['some_key']).to have(1).item
|
124 | 158 | expect(result_params[:f]['some_key'].first).to eq 'my_value'
|
|
128 | 162 | context "when there are pre-existing facets" do
|
129 | 163 | let(:params) { params_existing_facets }
|
130 | 164 | it "adds a facet param to existing facet constraints" do
|
131 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 165 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
132 | 166 |
|
133 | 167 | expect(result_params[:f]).to be_a Hash
|
134 | 168 |
|
|
143 | 177 | end
|
144 | 178 |
|
145 | 179 | it "leaves non-facet params alone" do
|
146 |
| - result_params = helper.add_facet_params("facet_field_2", "new_facet_value") |
| 180 | + result_params = search_state.add_facet_params("facet_field_2", "new_facet_value") |
147 | 181 |
|
148 | 182 | params.each_pair do |key, value|
|
149 | 183 | next if key == 'f'
|
|
155 | 189 | context "with a facet selected in the params" do
|
156 | 190 | let(:params) { parameter_class.new f: { 'single_value_facet_field' => 'other_value' } }
|
157 | 191 | 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') |
| 192 | + allow(search_state).to receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(double(single: true, key: "single_value_facet_field")) |
| 193 | + result_params = search_state.add_facet_params('single_value_facet_field', 'my_value') |
160 | 194 |
|
161 | 195 | expect(result_params[:f]['single_value_facet_field']).to have(1).item
|
162 | 196 | expect(result_params[:f]['single_value_facet_field'].first).to eq 'my_value'
|
163 | 197 | end
|
164 | 198 | end
|
165 | 199 |
|
166 | 200 | it "accepts a FacetItem instead of a plain facet value" do
|
167 |
| - result_params = helper.add_facet_params('facet_field_1', double(value: 123)) |
| 201 | + result_params = search_state.add_facet_params('facet_field_1', double(value: 123)) |
168 | 202 |
|
169 | 203 | expect(result_params[:f]['facet_field_1']).to include(123)
|
170 | 204 | end
|
171 | 205 |
|
172 | 206 | 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)) |
| 207 | + result_params = search_state.add_facet_params('facet_field_1', double(:field => 'facet_field_2', :value => 123)) |
174 | 208 |
|
175 | 209 | expect(result_params[:f]['facet_field_1']).to be_blank
|
176 | 210 | expect(result_params[:f]['facet_field_2']).to include(123)
|
177 | 211 | end
|
178 | 212 |
|
179 | 213 | 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' })) |
| 214 | + result_params = search_state.add_facet_params('facet_field_1', double(:value => 123, fq: { 'facet_field_2' => 'abc' })) |
181 | 215 |
|
182 | 216 | expect(result_params[:f]['facet_field_1']).to include(123)
|
183 | 217 | expect(result_params[:f]['facet_field_2']).to include('abc')
|
|
197 | 231 | }
|
198 | 232 |
|
199 | 233 | 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") |
| 234 | + params = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
201 | 235 |
|
202 | 236 | bad_keys = Blacklight::Solr::FacetPaginator.request_keys.values + [:id]
|
203 | 237 | bad_keys.each do |paginator_key|
|
|
206 | 240 | end
|
207 | 241 |
|
208 | 242 | it 'removes :page request key' do
|
209 |
| - params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value") |
| 243 | + params = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
210 | 244 | expect(params).to_not have_key(:page)
|
211 | 245 | end
|
212 | 246 |
|
213 | 247 | 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") |
| 248 | + added_facet_params = search_state.add_facet_params("facet_field_2", "facet_value") |
| 249 | + added_facet_params_from_facet_action = search_state.add_facet_params_and_redirect("facet_field_2", "facet_value") |
216 | 250 |
|
217 | 251 | 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 | 252 | end
|
|
225 | 259 | let(:facet_params) { { 'some_field' => ['some_value', 'another_value'] } }
|
226 | 260 |
|
227 | 261 | it "removes the facet / value tuple from the query parameters" do
|
228 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 262 | + params = search_state.remove_facet_params('some_field', 'some_value') |
229 | 263 | expect(params[:f]['some_field']).not_to include 'some_value'
|
230 | 264 | expect(params[:f]['some_field']).to include 'another_value'
|
231 | 265 | end
|
232 | 266 | end
|
233 | 267 |
|
234 | 268 | context "when the facet has configuration" do
|
235 | 269 | before do
|
236 |
| - allow(helper).to receive(:facet_configuration_for_field).with('some_field').and_return( |
| 270 | + allow(search_state).to receive(:facet_configuration_for_field).with('some_field').and_return( |
237 | 271 | double(single: true, field: "a_solr_field", key: "some_key"))
|
238 | 272 | end
|
239 | 273 | let(:facet_params) { { 'some_key' => ['some_value', 'another_value'] } }
|
240 | 274 |
|
241 | 275 | it "uses the facet's key configuration" do
|
242 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 276 | + params = search_state.remove_facet_params('some_field', 'some_value') |
243 | 277 | expect(params[:f]['some_key']).not_to eq 'some_value'
|
244 | 278 | expect(params[:f]['some_key']).to include 'another_value'
|
245 | 279 | end
|
|
249 | 283 | facet_params['another_field'] = ['some_value']
|
250 | 284 | facet_params['some_field'] = ['some_value']
|
251 | 285 |
|
252 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 286 | + params = search_state.remove_facet_params('some_field', 'some_value') |
253 | 287 |
|
254 | 288 | expect(params[:f]).not_to have_key 'some_field'
|
255 | 289 | end
|
256 | 290 |
|
257 | 291 | it "removes the 'f' parameter entirely when no facets remain" do
|
258 | 292 | facet_params['some_field'] = ['some_value']
|
259 | 293 |
|
260 |
| - params = helper.remove_facet_params('some_field', 'some_value') |
| 294 | + params = search_state.remove_facet_params('some_field', 'some_value') |
261 | 295 |
|
262 | 296 | expect(params).not_to have_key :f
|
263 | 297 | end
|
|
0 commit comments