summaryrefslogtreecommitdiff
path: root/spec/services/merge_requests/add_spent_time_service_spec.rb
blob: db3380e9582ddc60e35fcbc077de00a6deb4915e (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 MergeRequests::AddSpentTimeService do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be_with_reload(:merge_request) { create(:merge_request, :simple, :unique_branches, source_project: project) }

  let(:duration) { 1500 }
  let(:params) { { spend_time: { duration: duration, user_id: user.id } } }
  let(:service) { described_class.new(project: project, current_user: user, params: params) }

  describe '#execute' do
    before do
      project.add_developer(user)
    end

    it 'creates a new timelog with the specified duration' do
      expect { service.execute(merge_request) }.to change { Timelog.count }.from(0).to(1)

      timelog = merge_request.timelogs.last

      expect(timelog).not_to be_nil
      expect(timelog.time_spent).to eq(1500)
    end

    it 'creates a system note with the time added' do
      expect { service.execute(merge_request) }.to change { Note.count }.from(0).to(1)

      system_note = merge_request.notes.last

      expect(system_note).not_to be_nil
      expect(system_note.note_html).to include('added 25m of time spent')
    end

    it 'saves usage data' do
      expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
        .to receive(:track_time_spent_changed_action).once.with(user: user)

      service.execute(merge_request)
    end

    it 'is more efficient than using the full update-service' do
      other_mr = create(:merge_request, :simple, :unique_branches, source_project: project)

      update_service = ::MergeRequests::UpdateService.new(project: project, current_user: user, params: params)
      other_mr.reload

      expect { service.execute(merge_request) }
        .to issue_fewer_queries_than { update_service.execute(other_mr) }
    end

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

      it 'does not create a timelog with the specified duration' do
        expect { service.execute(merge_request) }.not_to change { Timelog.count }
        expect(merge_request).not_to be_valid
      end
    end
  end
end