-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Blacklight::SearchBuilder methods
- Loading branch information
Showing
4 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,134 @@ | ||
require 'spec_helper' | ||
|
||
describe Blacklight::SearchBuilder do | ||
let(:processor_chain) { [] } | ||
let(:blacklight_config) { Blacklight::Configuration.new } | ||
let(:scope) { double blacklight_config: blacklight_config } | ||
subject { described_class.new processor_chain, scope } | ||
describe "#with" do | ||
it "should set the blacklight params" do | ||
params = {} | ||
subject.with(params) | ||
expect(subject.blacklight_params).to eq params | ||
end | ||
end | ||
|
||
end | ||
describe "#processor_chain" do | ||
let(:processor_chain) { [:a, :b, :c] } | ||
it "should be mutable" do | ||
subject.processor_chain.insert(-1, :d) | ||
expect(subject.processor_chain).to match_array [:a, :b, :c, :d] | ||
end | ||
end | ||
|
||
describe "#append" do | ||
let(:processor_chain) { [:a, :b, :c] } | ||
it "should provide a new search builder with the processor chain" do | ||
builder = subject.append(:d, :e) | ||
expect(subject.processor_chain).to eq processor_chain | ||
expect(builder.processor_chain).not_to eq subject.processor_chain | ||
expect(builder.processor_chain).to match_array [:a, :b, :c, :d, :e] | ||
end | ||
end | ||
|
||
describe "#query" do | ||
it "should append the extra parameters to the result" do | ||
actual = subject.query({a: 1}) | ||
expect(actual).to include a: 1 | ||
end | ||
end | ||
|
||
describe "#processed_parameters" do | ||
let(:processor_chain) { [:step_1] } | ||
it "should try to run the processor method on the provided scope" do | ||
allow(scope).to receive(:respond_to?).and_return(true) | ||
allow(scope).to receive(:step_1) do |req_params, user_params| | ||
req_params[:step_1] = 'scope' | ||
end | ||
|
||
Deprecation.silence(Blacklight::SearchBuilder) do | ||
expect(subject.processed_parameters).to include step_1: 'scope' | ||
end | ||
end | ||
|
||
it "should try to run the processor method on the search builder" do | ||
allow(subject).to receive(:step_1) do |req_params, user_params| | ||
req_params[:step_1] = 'builder' | ||
end | ||
|
||
expect(subject.processed_parameters).to include step_1: 'builder' | ||
end | ||
end | ||
|
||
describe "#blacklight_config" do | ||
it "should get the blacklight_config from the scope" do | ||
expect(subject.blacklight_config).to eq scope.blacklight_config | ||
end | ||
end | ||
|
||
describe "#page" do | ||
it "should be the current user parameter page number" do | ||
expect(subject.with(page: 2).send(:page)).to eq 2 | ||
end | ||
|
||
it "should be page 1 if not page number given" do | ||
expect(subject.send(:page)).to eq 1 | ||
end | ||
|
||
it "should coerce parameters to integers" do | ||
expect(subject.with(page: '2b').send(:page)).to eq 2 | ||
end | ||
end | ||
|
||
describe "#rows" do | ||
it "should be the per_page parameter" do | ||
expect(subject.with(per_page: 5).send(:rows)).to eq 5 | ||
end | ||
|
||
it "should support the legacy 'rows' parameter" do | ||
expect(subject.with(rows: 10).send(:rows)).to eq 10 | ||
end | ||
|
||
it "should use the provided default" do | ||
expect(subject.send(:rows, 17)).to eq 17 | ||
end | ||
|
||
it "should be set to the configured default" do | ||
blacklight_config.default_per_page = 42 | ||
expect(subject.send(:rows)).to eq 42 | ||
end | ||
|
||
it "should default to 10" do | ||
blacklight_config.default_per_page = nil | ||
expect(subject.send(:rows)).to eq 10 | ||
end | ||
|
||
it "should limit the number of rows to the configured maximum" do | ||
blacklight_config.max_per_page = 1000 | ||
expect(subject.send(:rows, 1001)).to eq 1000 | ||
end | ||
end | ||
|
||
describe "#sort" do | ||
it "should pass through the sort parameter" do | ||
expect(subject.with(sort: 'x').send(:sort)).to eq 'x' | ||
end | ||
|
||
it "should use the default if no sort parameter is given" do | ||
blacklight_config.default_sort_field = double(sort: 'x desc') | ||
expect(subject.send(:sort)).to eq 'x desc' | ||
end | ||
|
||
it "should use the requested sort field" do | ||
blacklight_config.add_sort_field 'x', sort: 'x asc' | ||
expect(subject.with(sort: 'x').send(:sort)).to eq 'x asc' | ||
end | ||
end | ||
|
||
describe "#search_field" do | ||
it "should use the requested search field" do | ||
blacklight_config.add_search_field 'x' | ||
expect(subject.with(search_field: 'x').send(:search_field)).to eq blacklight_config.search_fields['x'] | ||
end | ||
end | ||
end |