summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage_data_counters/work_item_activity_unique_counter_spec.rb
blob: 4561d898479a356eb4edad1ac19964226922b034 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter, :clean_gitlab_redis_shared_state do
  let(:user) { build(:user, id: 1) }

  shared_examples 'counter that does not track the event' do
    it 'does not track the event' do
      expect { 3.times { track_event } }.to not_change {
        Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(
          event_names: event_name,
          start_date: 2.weeks.ago,
          end_date: 2.weeks.from_now
        )
      }
    end
  end

  shared_examples 'work item unique counter' do
    context 'when track_work_items_activity FF is enabled' do
      it 'tracks a unique event only once' do
        expect { 3.times { track_event } }.to change {
          Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(
            event_names: event_name,
            start_date: 2.weeks.ago,
            end_date: 2.weeks.from_now
          )
        }.by(1)
      end

      context 'when author is nil' do
        let(:user) { nil }

        it_behaves_like 'counter that does not track the event'
      end
    end

    context 'when track_work_items_activity FF is disabled' do
      before do
        stub_feature_flags(track_work_items_activity: false)
      end

      it_behaves_like 'counter that does not track the event'
    end
  end

  describe '.track_work_item_created_action' do
    subject(:track_event) { described_class.track_work_item_created_action(author: user) }

    let(:event_name) { described_class::WORK_ITEM_CREATED }

    it_behaves_like 'work item unique counter'
  end

  describe '.track_work_item_title_changed_action' do
    subject(:track_event) { described_class.track_work_item_title_changed_action(author: user) }

    let(:event_name) { described_class::WORK_ITEM_TITLE_CHANGED }

    it_behaves_like 'work item unique counter'
  end
end