summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/usage_data_counters/usage_counter_shared_examples.rb
blob: 848437577d7ffdd2d7df9c6db145ff1582b37756 (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
# frozen_string_literal: true

RSpec.shared_examples 'a usage counter' do
  describe '.increment' do
    let(:project_id) { 12 }

    it 'intializes and increments the counter for the project by 1' do
      expect do
        described_class.increment(project_id)
      end.to change { described_class.usage_totals[project_id] }.from(nil).to(1)
    end
  end

  describe '.usage_totals' do
    let(:usage_totals) { described_class.usage_totals }

    context 'when the feature has not been used' do
      it 'returns the total counts and counts per project' do
        expect(usage_totals.keys).to eq([:total])
        expect(usage_totals[:total]).to eq(0)
      end
    end

    context 'when the feature has been used in multiple projects' do
      let(:project1_id) { 12 }
      let(:project2_id) { 16 }

      before do
        described_class.increment(project1_id)
        described_class.increment(project2_id)
      end

      it 'returns the total counts and counts per project' do
        expect(usage_totals[project1_id]).to eq(1)
        expect(usage_totals[project2_id]).to eq(1)
        expect(usage_totals[:total]).to eq(2)
      end
    end
  end
end