require 'spec_helper'

module Pageflow
  describe AutoGeneratedPermaId do
    let(:model) do
      Class.new(ActiveRecord::Base) do
        self.table_name = 'test_revision_components'

        include AutoGeneratedPermaId

        belongs_to :revision, class_name: 'Pageflow::Revision'

        def self.name
          'AutoGeneratedPermaIdSpecComponent'
        end

        def entry_for_auto_generated_perma_id
          revision.entry
        end
      end
    end

    describe '#perma_id' do
      it 'is set on creation' do
        entry = create(:entry)
        instance = model.create(revision: entry.draft)

        expect(instance.perma_id).to be_present
      end

      it 'differs for separate instances of the same model' do
        entry = create(:entry)
        instance1 = model.create(revision: entry.draft)
        instance2 = model.create(revision: entry.draft)

        expect(instance1.perma_id).not_to eq(instance2.perma_id)
      end

      it 'remains the same on subsequent saves' do
        entry = create(:entry)
        instance = model.create(revision: entry.draft)

        expect {
          instance.save!
        }.not_to(change { instance.perma_id })
      end
    end
  end
end
