Sha256: 84572d27ed888faaba901678f6df4cc2df4d40ee8a39232d62ea05b07d88ee4b

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

shared_examples_for "has reference" do
  before do
    allow(Decidim).to receive(:calculate_reference_method).and_return(nil)
  end

  context "when the reference is nil" do
    before do
      subject[:reference] = nil
    end

    context "when there is not a custom resource reference generator present" do
      it "generates a valid reference" do
        expect(subject.reference).to match(/[A-z]+/)
      end
    end

    context "when there is a custom resource reference generator present" do
      before do
        allow(Decidim).to receive(:resource_reference_generator).and_return(->(resource, _feature) { "1234-#{resource.id}" })
      end

      it "generates a valid reference" do
        expect(subject.reference).to eq("1234-#{subject.id}")
      end
    end
  end

  context "when the reference is already set" do
    before do
      subject[:reference] = "ARBITRARYREF"
    end

    it "keeps the pre-existing reference" do
      expect(subject.reference).to eq("ARBITRARYREF")
    end
  end

  context "when saving" do
    it "stores the reference" do
      subject.reference = nil
      subject.save!
      expect(subject.reload.reference).to_not be_blank
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-core-0.4.0 lib/decidim/core/test/shared_examples/has_reference.rb