2016-06-02 11:05:42 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe LegacyDiffNote, models: true do
|
|
|
|
describe "Commit diff line notes" do
|
2016-08-24 12:49:21 +05:30
|
|
|
let!(:note) { create(:legacy_diff_note_on_commit, note: "+1 from me") }
|
2016-06-02 11:05:42 +05:30
|
|
|
let!(:commit) { note.noteable }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it "saves a valid note" do
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(note.commit_id).to eq(commit.id)
|
|
|
|
expect(note.noteable.id).to eq(commit.id)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it "is recognized by #legacy_diff_note?" do
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(note).to be_legacy_diff_note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#active?' do
|
2016-08-24 12:49:21 +05:30
|
|
|
it 'is always true when the note has no associated diff line' do
|
|
|
|
note = build(:legacy_diff_note_on_merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(note).to receive(:diff_line).and_return(nil)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(note).to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is never true when the note has no noteable associated' do
|
2016-08-24 12:49:21 +05:30
|
|
|
note = build(:legacy_diff_note_on_merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(note).to receive(:diff_line).and_return(double)
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(note).to receive(:noteable).and_return(nil)
|
|
|
|
|
|
|
|
expect(note).not_to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the memoized value if defined' do
|
2016-08-24 12:49:21 +05:30
|
|
|
note = build(:legacy_diff_note_on_merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
note.instance_variable_set(:@active, 'foo')
|
|
|
|
expect(note).not_to receive(:find_noteable_diff)
|
|
|
|
|
|
|
|
expect(note.active?).to eq 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a merge request noteable' do
|
|
|
|
it 'is false when noteable has no matching diff' do
|
|
|
|
merge = build_stubbed(:merge_request, :simple)
|
2016-08-24 12:49:21 +05:30
|
|
|
note = build(:legacy_diff_note_on_merge_request, noteable: merge)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
allow(note).to receive(:diff_line).and_return(double)
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(note).to receive(:find_noteable_diff).and_return(nil)
|
|
|
|
|
|
|
|
expect(note).not_to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when noteable has a matching diff' do
|
|
|
|
merge = create(:merge_request, :simple)
|
|
|
|
|
|
|
|
# Generate a real line_code value so we know it will match. We use a
|
|
|
|
# random line from a random diff just for funsies.
|
2016-09-13 17:45:13 +05:30
|
|
|
diff = merge.raw_diffs.to_a.sample
|
2016-06-02 11:05:42 +05:30
|
|
|
line = Gitlab::Diff::Parser.new.parse(diff.diff.each_line).to_a.sample
|
|
|
|
code = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos)
|
|
|
|
|
|
|
|
# We're persisting in order to trigger the set_diff callback
|
2016-08-24 12:49:21 +05:30
|
|
|
note = create(:legacy_diff_note_on_merge_request, noteable: merge,
|
|
|
|
line_code: code,
|
|
|
|
project: merge.source_project)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
# Make sure we don't get a false positive from a guard clause
|
|
|
|
expect(note).to receive(:find_noteable_diff).and_call_original
|
|
|
|
expect(note).to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
describe "#discussion_id" do
|
|
|
|
let(:note) { create(:note) }
|
|
|
|
|
|
|
|
context "when it is newly created" do
|
|
|
|
it "has a discussion id" do
|
|
|
|
expect(note.discussion_id).not_to be_nil
|
|
|
|
expect(note.discussion_id).to match(/\A\h{40}\z/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when it didn't store a discussion id before" do
|
|
|
|
before do
|
|
|
|
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 = Note.find(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
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|