OSDN Git Service

Make changes to tests
[wvm/gitlab.git] / spec / support / mentionable_shared_examples.rb
1 # Specifications for behavior common to all Mentionable implementations.
2 # Requires a shared context containing:
3 # - let(:subject) { "the mentionable implementation" }
4 # - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
5 # - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
6
7 def common_mentionable_setup
8   # Avoid name collisions with let(:project) or let(:author) in the surrounding scope.
9   let(:mproject) { create :project }
10   let(:mauthor) { subject.author }
11
12   let(:mentioned_issue) { create :issue, project: mproject }
13   let(:other_issue) { create :issue, project: mproject }
14   let(:mentioned_mr) { create :merge_request, source_project: mproject, source_branch: 'different' }
15   let(:mentioned_commit) { double('commit', sha: '1234567890abcdef').as_null_object }
16
17   # Override to add known commits to the repository stub.
18   let(:extra_commits) { [] }
19
20   # A string that mentions each of the +mentioned_.*+ objects above. Mentionables should add a self-reference
21   # to this string and place it in their +mentionable_text+.
22   let(:ref_string) do
23     "mentions ##{mentioned_issue.iid} twice ##{mentioned_issue.iid}, !#{mentioned_mr.iid}, " +
24     "#{mentioned_commit.sha[0..5]} and itself as #{backref_text}"
25   end
26
27   before do
28     # Wire the project's repository to return the mentioned commit, and +nil+ for any
29     # unrecognized commits.
30     commitmap = { '123456' => mentioned_commit }
31     extra_commits.each { |c| commitmap[c.sha[0..5]] = c }
32
33     repo = double('repository')
34     repo.stub(:commit) { |sha| commitmap[sha] }
35     mproject.stub(repository: repo)
36
37     set_mentionable_text.call(ref_string)
38   end
39 end
40
41 shared_examples 'a mentionable' do
42   common_mentionable_setup
43
44   it 'generates a descriptive back-reference' do
45     subject.gfm_reference.should == backref_text
46   end
47
48   it "extracts references from its reference property" do
49     # De-duplicate and omit itself
50     refs = subject.references(mproject)
51
52     refs.should have(3).items
53     refs.should include(mentioned_issue)
54     refs.should include(mentioned_mr)
55     refs.should include(mentioned_commit)
56   end
57
58   it 'creates cross-reference notes' do
59     [mentioned_issue, mentioned_mr, mentioned_commit].each do |referenced|
60       Note.should_receive(:create_cross_reference_note).with(referenced, subject.local_reference, mauthor, mproject)
61     end
62
63     subject.create_cross_references!(mproject, mauthor)
64   end
65
66   it 'detects existing cross-references' do
67     Note.create_cross_reference_note(mentioned_issue, subject.local_reference, mauthor, mproject)
68
69     subject.has_mentioned?(mentioned_issue).should be_true
70     subject.has_mentioned?(mentioned_mr).should be_false
71   end
72 end
73
74 shared_examples 'an editable mentionable' do
75   common_mentionable_setup
76
77   it_behaves_like 'a mentionable'
78
79   it 'creates new cross-reference notes when the mentionable text is edited' do
80     new_text = "this text still mentions ##{mentioned_issue.iid} and #{mentioned_commit.sha[0..5]}, " +
81       "but now it mentions ##{other_issue.iid}, too."
82
83     [mentioned_issue, mentioned_commit].each do |oldref|
84       Note.should_not_receive(:create_cross_reference_note).with(oldref, subject.local_reference,
85         mauthor, mproject)
86     end
87
88     Note.should_receive(:create_cross_reference_note).with(other_issue, subject.local_reference, mauthor, mproject)
89
90     subject.save
91     set_mentionable_text.call(new_text)
92     subject.notice_added_references(mproject, mauthor)
93   end
94 end