summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/versioned_description_shared_examples.rb
blob: 59124af19ec720dc5c295b703e33890524d26590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

RSpec.shared_examples 'versioned description' do
  describe 'associations' do
    it { is_expected.to have_many(:description_versions) }
  end

  describe 'save_description_version' do
    let(:factory_name) { described_class.name.underscore.to_sym }
    let!(:model) { create(factory_name, description: 'Original description') }

    context 'when feature is enabled' do
      before do
        stub_feature_flags(save_description_versions: true)
      end

      context 'when description was changed' do
        before do
          model.update!(description: 'New description')
        end

        it 'saves the old and new description for the first update' do
          expect(model.description_versions.first.description).to eq('Original description')
          expect(model.description_versions.last.description).to eq('New description')
        end

        it 'only saves the new description for subsequent updates' do
          expect { model.update!(description: 'Another description') }.to change { model.description_versions.count }.by(1)

          expect(model.description_versions.last.description).to eq('Another description')
        end

        it 'sets the new description version to `saved_description_version`' do
          expect(model.saved_description_version).to eq(model.description_versions.last)
        end

        it 'clears `saved_description_version` after another save that does not change description' do
          model.save!

          expect(model.saved_description_version).to be_nil
        end
      end

      context 'when description was not changed' do
        it 'does not save any description version' do
          expect { model.save! }.not_to change { model.description_versions.count }

          expect(model.saved_description_version).to be_nil
        end
      end
    end

    context 'when feature is disabled' do
      before do
        stub_feature_flags(save_description_versions: false)
      end

      it 'does not save any description version' do
        expect { model.update!(description: 'New description') }.not_to change { model.description_versions.count }

        expect(model.saved_description_version).to be_nil
      end
    end
  end
end