diff options
author | Robert Speicher <rspeicher@gmail.com> | 2016-03-08 20:04:13 -0500 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-03-11 18:16:34 -0500 |
commit | a63eba9a2bebd6e33c3d1051a0d2fd08e024f546 (patch) | |
tree | c9c2ad7650f50b6baa3df85adc96be09dceff62b /spec/models/note_spec.rb | |
parent | 491d0ae1c306f60919f8048844a130d89e41f565 (diff) | |
download | gitlab-ce-a63eba9a2bebd6e33c3d1051a0d2fd08e024f546.tar.gz |
Add unit specs for `Note#active?`rs-note-active-spec
Diffstat (limited to 'spec/models/note_spec.rb')
-rw-r--r-- | spec/models/note_spec.rb | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index cd620ea5440..b3ed7d6f8bd 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -152,7 +152,7 @@ describe Note, models: true do end end - describe :grouped_awards do + describe '.grouped_awards' do before do create :note, note: "smile", is_award: true create :note, note: "smile", is_award: true @@ -169,6 +169,66 @@ describe Note, models: true do end end + describe '#active?' do + it 'is always true when the note has no associated diff' do + note = build(:note) + + expect(note).to receive(:diff).and_return(nil) + + expect(note).to be_active + end + + it 'is never true when the note has no noteable associated' do + note = build(:note) + + expect(note).to receive(:diff).and_return(double) + expect(note).to receive(:noteable).and_return(nil) + + expect(note).not_to be_active + end + + it 'returns the memoized value if defined' do + note = build(:note) + + expect(note).to receive(:diff).and_return(double) + expect(note).to receive(:noteable).and_return(double) + + 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) + note = build(:note, noteable: merge) + + allow(note).to receive(:diff).and_return(double) + 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. + diff = merge.diffs.to_a.sample + 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 + note = create(:note, noteable: merge, line_code: code) + + # 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 + describe "editable?" do it "returns true" do note = build(:note) |