File: discussion_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (160 lines) | stat: -rw-r--r-- 5,058 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# frozen_string_literal: true

RSpec.shared_examples 'Notes::Discussion' do
  describe '#start_of_discussion?' do
    it 'returns true when note is the start of a discussion' do
      expect(discussion_note).to be_start_of_discussion
    end

    it 'returns false when note is a reply' do
      expect(discussion_reply).not_to be_start_of_discussion
    end
  end

  describe '.find_discussion' do
    let_it_be(:noteable) { discussion_note.noteable }

    it 'returns a discussion with multiple notes' do
      discussion = noteable.notes.find_discussion(discussion_note.discussion_id)

      expect(discussion).not_to be_nil
      expect(discussion.notes).to match_array([discussion_note, discussion_reply])
      expect(discussion.first_note.discussion_id).to eq(discussion_note.discussion_id)
    end
  end

  describe '#discussion_id' do
    context 'when it is newly created' do
      it 'has a discussion id' do
        expect(discussion_note.discussion_id).not_to be_nil
        expect(discussion_note.discussion_id).to match(/\A\h{40}\z/)
      end
    end

    context "when it didn't store a discussion id before" do
      before do
        discussion_note.update_column(:discussion_id, nil)
      end

      it 'has a discussion id' do
        # The discussion_id is set in `after_initialize`, so `reload` won't work
        reloaded_note = described_class.find(discussion_note.id)

        expect(reloaded_note.discussion_id).not_to be_nil
        expect(reloaded_note.discussion_id).to match(/\A\h{40}\z/)
      end
    end
  end

  describe '#to_discussion' do
    it 'returns a discussion with just this note' do
      discussion = discussion_note.to_discussion

      expect(discussion.id).to eq(discussion_note.discussion_id)
      expect(discussion.notes).to eq([discussion_note])
    end
  end

  describe '#discussion' do
    context 'when the note is part of a discussion' do
      subject { discussion_reply }

      it 'returns the discussion this note is in' do
        discussion = subject.discussion

        expect(discussion.id).to eq(subject.discussion_id)
        expect(discussion.notes).to eq([discussion_note, subject])
      end
    end

    context 'when the note is not part of a discussion' do
      subject { create(factory) } # rubocop:disable Rails/SaveBang -- create used as factory method

      it 'returns a discussion with just this note' do
        discussion = subject.discussion

        expect(discussion.id).to eq(subject.discussion_id)
        expect(discussion.notes).to eq([subject])
      end
    end
  end

  describe '#part_of_discussion?' do
    context 'for a regular note' do
      it 'returns false' do
        expect(note1.part_of_discussion?).to be_falsey
      end
    end

    context 'for a discussion note' do
      it 'returns true' do
        expect(discussion_note.part_of_discussion?).to be_truthy
      end
    end
  end

  describe '#in_reply_to?' do
    context 'for a note' do
      context 'when part of a discussion' do
        it 'checks if the note is in reply to the other discussion' do
          expect(discussion_note).to receive(:in_reply_to?).with(discussion_reply).and_call_original
          expect(discussion_note).to receive(:in_reply_to?).with(discussion_reply.noteable).and_call_original
          expect(discussion_note).to receive(:in_reply_to?).with(discussion_reply.to_discussion).and_call_original

          discussion_note.in_reply_to?(discussion_reply)
        end
      end

      context 'when not part of a discussion' do
        let(:reply) { create(factory, in_reply_to: note1) }

        it 'checks if the note is in reply to the other noteable' do
          expect(note1).to receive(:in_reply_to?).with(reply).and_call_original
          expect(note1).to receive(:in_reply_to?).with(reply.noteable).and_call_original

          note1.in_reply_to?(reply)
        end
      end
    end

    context 'for a discussion' do
      context 'when part of the same discussion' do
        it 'returns true' do
          expect(discussion_note.in_reply_to?(discussion_reply.to_discussion)).to be_truthy
        end
      end

      context 'when not part of the same discussion' do
        # rubocop: disable Rails/SaveBang -- creation using factory
        let(:reply) { create(discussion_factory) }
        # rubocop: enable Rails/SaveBang

        it 'returns false' do
          expect(discussion_note.in_reply_to?(reply.to_discussion)).to be_falsey
        end
      end
    end

    context 'for a noteable' do
      context 'when a comment on the same noteable' do
        it 'returns true' do
          expect(note1.in_reply_to?(reply.noteable)).to be_truthy
        end
      end

      context 'when not a comment on the same noteable' do
        it 'returns false' do
          expect(note1.in_reply_to?(note2.noteable)).to be_falsey
        end
      end
    end

    context 'for a different entity' do
      let(:user) { create(:user) }

      it 'returns false' do
        expect(note1.in_reply_to?(user)).to be_falsey
      end
    end
  end
end