spec/admino/query/base_spec.rb in admino-0.0.16 vs spec/admino/query/base_spec.rb in admino-0.0.17
- old
+ new
@@ -110,36 +110,69 @@
context 'with a set of search_fields, filter_groups and sortings' do
let(:search_field_config) { config.add_search_field(:search_field) }
let(:filter_group_config) { config.add_filter_group(:filter_group, [:one, :two]) }
let(:sorting_config) { config.add_sorting_scopes([:title, :year]) }
- let(:scope_chained_with_search_field) { double('scope 1') }
- let(:scope_chained_with_group_filter) { double('scope 2') }
- let(:final_chain) { double('scope 3') }
+ let(:params) do
+ {
+ query: {
+ search_field: "foo",
+ filter_group: "one",
+ },
+ sorting: "title",
+ sort_order: "desc"
+ }
+ end
before do
search_field_config
filter_group_config
sorting_config
query
+ end
- allow(query.search_field_by_name(:search_field)).
- to receive(:augment_scope).
- with(starting_scope).
- and_return(scope_chained_with_search_field)
+ context 'if query object does not respond to scopes' do
+ it 'chains from starting scope' do
+ expect(result.chain).to eq [
+ :search_field, ["foo"],
+ :one, [],
+ :title, [:desc]
+ ]
+ end
+ end
- allow(query.filter_group_by_name(:filter_group)).
- to receive(:augment_scope).
- with(scope_chained_with_search_field).
- and_return(scope_chained_with_group_filter)
+ context 'else' do
+ let(:query_klass) do
+ Class.new(Base) do
+ def self.model_name
+ ActiveModel::Name.new(self, nil, "temp")
+ end
- allow(query.sorting).to receive(:augment_scope).
- with(scope_chained_with_group_filter).
- and_return(final_chain)
- end
+ def search_field(scope, foo)
+ scope.my_search_field(foo)
+ end
- it 'chains them toghether' do
- expect(result).to eq final_chain
+ def one(scope)
+ scope.my_one
+ end
+
+ def title(scope, order)
+ scope.my_title(order)
+ end
+ end
+ end
+
+ subject(:query) do
+ query_klass.new(params, config)
+ end
+
+ it 'chains from starting scope calling query object methods' do
+ expect(result.chain).to eq [
+ :my_search_field, ["foo"],
+ :my_one, [],
+ :my_title, [:desc]
+ ]
+ end
end
end
context 'with a configured ending scope' do
before do