spec/definition_spec.rb in magic_grid-0.10.4 vs spec/definition_spec.rb in magic_grid-0.11.0
- old
+ new
@@ -65,10 +65,17 @@
its(:collection) { should have(MagicGrid::Definition.runtime_defaults[:per_page]).items }
its(:columns) { should == column_list }
end
+ context "when given a MagicGrid::Collection" do
+ actual_collection = [1,2,3]
+ let(:collection) { MagicGrid::Collection.new(actual_collection, nil) }
+ subject { MagicGrid::Definition.new(column_list, collection, controller) }
+ its(:collection) { should eq(actual_collection) }
+ end
+
context "when given a large collection and some options" do
let(:controller) {
controller = double()
controller.stub(:params) { HashWithIndifferentAccess.new({grid_page: 2}) }
controller
@@ -85,8 +92,112 @@
it "should know how to extract its params" do
subject.param_key(:page).should == :grid_page
subject.param_key(:hunkydory).should == :grid_hunkydory
subject.param(:page).should == 2
end
+ end
+ context "sorting" do
+ data = [1,56,7,21,1]
+ let(:controller) {
+ controller = double()
+ controller.stub(:params) { HashWithIndifferentAccess.new({grid_order: 1}) }
+ controller
+ }
+ let(:collection) { data }
+ it "should sort collection using #order" do
+ collection.should_receive(:order).with("foo DESC") { data.sort.reverse }
+ grid = MagicGrid::Definition.new([{:sql => "foo"}], collection, controller, id: :grid)
+
+ grid.collection.should == data.sort.reverse
+ end
+ pending "test #order_sql directly"
end
+
+ context "filtering with #where" do
+ data = [1,56,7,21,1]
+ let(:controller) {
+ controller = double.tap do |c|
+ c.stub(:params) { HashWithIndifferentAccess.new({f1: 1}) }
+ end
+ }
+ let(:collection) {
+ data.tap do |d|
+ d.stub(:where) do |h|
+ d.select { |d| d < 10 }
+ end
+ end
+ }
+ subject { MagicGrid::Definition.new([{:sql => "foo"}],
+ collection,
+ controller,
+ id: :grid, listeners: {f1: :f1}) }
+ its(:collection) { should == [1, 7, 1] }
+ end
+
+ context "filtering with a callback" do
+ data = [1,56,7,21,1]
+ filter = Proc.new do |c|
+ c.select { |i| i > 10 }
+ end
+ let(:controller) {
+ controller = double.tap do |c|
+ c.stub(:params) { HashWithIndifferentAccess.new({f1: 1}) }
+ end
+ }
+ let(:collection) {
+ data
+ }
+ subject { MagicGrid::Definition.new([{:sql => "foo"}],
+ collection,
+ controller,
+ id: :grid, listener_handler: filter) }
+ its(:collection) { should == [56, 21] }
+ end
+
+ pending "test listening on a dumb collection"
+
+ context "post_filtering with a callable post_filter" do
+ data = [1,56,7,21,1]
+ filter = Proc.new do |c|
+ c.select { |i| i > 10 }
+ end
+ let(:controller) {
+ controller = double.tap do |c|
+ c.stub(:params) { HashWithIndifferentAccess.new({f1: 1}) }
+ end
+ }
+ let(:collection) {
+ data
+ }
+ subject { MagicGrid::Definition.new([{:sql => "foo"}],
+ collection,
+ controller,
+ id: :grid, post_filter: filter) }
+ its(:collection) { should == [56, 21] }
+ end
+
+ context "post_filtering with a collection post_filter" do
+ data = [1,56,7,21,1]
+ filter = Proc.new do |c|
+ c.select { |i| i > 10 }
+ end
+ let(:controller) {
+ controller = double.tap do |c|
+ c.stub(:params) { HashWithIndifferentAccess.new({f1: 1}) }
+ end
+ }
+ let(:collection) {
+ data.tap do |d|
+ d.stub(:post_filter) do |h|
+ d.select { |d| d > 10 }
+ end
+ end
+ }
+ subject { MagicGrid::Definition.new([{:sql => "foo"}],
+ collection,
+ controller,
+ id: :grid, collection_post_filter?: true) }
+ its(:collection) { should == [56, 21] }
+ end
+
end