Sha256: ce1dffef76f2e0cceb8725037c7cf51513475b7535e9609fea960152f180315d

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'

describe RailsSortable::Model, type: :model do

  describe "before_create" do
    context "when sort is nil" do
      it "should be automatically set maximum sort value" do
        Item.create! sort: 1000
        new_item = Item.create!
        expect(new_item.sort).to eql 1001
      end
    end
    context "when sort has value" do
      it "should not set sort value" do
        item = Item.create! sort: 1000
        expect(item.sort).to eql 1000
      end
    end
  end

  describe "set_sortable" do
    describe "attribute" do
      it "should change sort_attribute" do
        other_item = SequencedItem.create!
        expect(other_item.sequence).to eql(1)
      end
    end

    describe "without_updating_timestamps" do
      context "when optional value is true" do
        before do
          Item.class_eval do
            set_sortable :sort, without_updating_timestamps: true
          end
        end
        it "should NOT modify timestamps" do
          item = Item.create!
          expect { item.update_sort!(1000) }.to_not change(item, :updated_at)
        end
      end

      context "when optional value is NOT true" do
        before do
          Item.class_eval do
            set_sortable :sort, without_updating_timestamps: false
          end
        end
        it "should modify timestamps" do
          item = Item.create!
          expect { item.update_sort!(1000) }.to change(item, :updated_at)
        end
      end
    end
  end

  describe "each_with_sortable_id" do
    it "should make models iterable with sortable ids" do
      items = 2.times.map { |i| Item.create! sort: i }
      expect { |b| Item.order(:sort).each_with_sortable_id(&b) }.to yield_successive_args(
        [items[0], "Item_#{items[0].id}"],
        [items[1], "Item_#{items[1].id}"],
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_sortable-1.0.0 spec/models/rails_sortable/model_spec.rb