summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/controllers/issuable_notes_filter_shared_examples.rb
blob: 26ed86bfe2627f5ef68533ab8bc7f8e578b67fe2 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

shared_examples 'issuable notes filter' do
  let(:params) do
    if issuable_parent.is_a?(Project)
      { namespace_id: issuable_parent.namespace, project_id: issuable_parent, id: issuable.iid }
    else
      { group_id: issuable_parent, id: issuable.to_param }
    end
  end

  it 'sets discussion filter' do
    notes_filter = UserPreference::NOTES_FILTERS[:only_comments]

    get :discussions, params: params.merge(notes_filter: notes_filter)

    expect(user.reload.notes_filter_for(issuable)).to eq(notes_filter)
    expect(UserPreference.count).to eq(1)
  end

  it 'expires notes e-tag cache for issuable if filter changed' do
    notes_filter = UserPreference::NOTES_FILTERS[:only_comments]

    expect_any_instance_of(issuable.class).to receive(:expire_note_etag_cache)

    get :discussions, params: params.merge(notes_filter: notes_filter)
  end

  it 'does not expires notes e-tag cache for issuable if filter did not change' do
    notes_filter = UserPreference::NOTES_FILTERS[:only_comments]
    user.set_notes_filter(notes_filter, issuable)

    expect_any_instance_of(issuable.class).not_to receive(:expire_note_etag_cache)

    get :discussions, params: params.merge(notes_filter: notes_filter)
  end

  it 'does not set notes filter when database is in read only mode' do
    allow(Gitlab::Database).to receive(:read_only?).and_return(true)
    notes_filter = UserPreference::NOTES_FILTERS[:only_comments]

    get :discussions, params: params.merge(notes_filter: notes_filter)

    expect(user.reload.notes_filter_for(issuable)).to eq(UserPreference::NOTES_FILTERS[:all_notes])
  end

  it 'does not set notes filter when persist_filter param is false' do
    notes_filter = UserPreference::NOTES_FILTERS[:only_comments]

    get :discussions, params: params.merge(notes_filter: notes_filter, persist_filter: false)

    expect(user.reload.notes_filter_for(issuable)).to eq(UserPreference::NOTES_FILTERS[:all_notes])
  end

  it 'returns only user comments' do
    user.set_notes_filter(UserPreference::NOTES_FILTERS[:only_comments], issuable)

    get :discussions, params: params
    discussions = json_response

    expect(discussions.count).to eq(1)
    expect(discussions.first["notes"].first["system"]).to be(false)
  end

  it 'returns only activity notes' do
    user.set_notes_filter(UserPreference::NOTES_FILTERS[:only_activity], issuable)

    get :discussions, params: params
    discussions = json_response

    expect(discussions.count).to eq(1)
    expect(discussions.first["notes"].first["system"]).to be(true)
  end

  context 'when filter is set to "only_comments"' do
    it 'does not merge label event notes' do
      user.set_notes_filter(UserPreference::NOTES_FILTERS[:only_comments], issuable)

      expect(ResourceEvents::MergeIntoNotesService).not_to receive(:new)

      get :discussions, params: params
    end
  end
end