Skip to content

Commit

Permalink
Merge pull request #13929 from saraycp/extend_comment_component_specs
Browse files Browse the repository at this point in the history
Extend specs for BsRequestCommentComponent
  • Loading branch information
saraycp committed Mar 2, 2023
2 parents f76064d + df8d9be commit 3db2f54
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions src/api/spec/components/bs_request_comment_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,73 @@

RSpec.describe BsRequestCommentComponent, type: :component do
let(:commentable) { create(:bs_request_with_submit_action) }
let(:comment_a) { create(:comment_request, commentable: commentable, body: 'Comment A') }
let(:comment_a) { create(:comment_request, commentable: commentable, body: 'Comment A', created_at: Time.zone.yesterday) }

subject { render_inline(described_class.new(comment: comment_a, commentable: commentable, level: 1)) }
before do
render_inline(described_class.new(comment: comment_a, commentable: commentable, level: 1))
end

it 'displays a comment icon' do
expect(rendered_content).to have_selector('i.fa-comment', count: 1)
end

it 'displays an avatar' do
expect(rendered_content).to have_selector("img[title='#{comment_a.user.realname}']", count: 1)
end

it 'displays who wrote the comment' do
expect(rendered_content).to have_text("#{comment_a.user.realname} (#{comment_a.user.login})\nwrote")
end

it 'displays the time when the comment happened in words' do
expect(rendered_content).to have_text('1 day ago')
end

it 'displays the comment' do
expect(rendered_content).to have_text('Comment A')
end

context 'when the user is not logged in' do
it 'is not possible to reply a comment' do
expect(subject).not_to have_text('Reply')
expect(rendered_content).not_to have_text('Reply')
end
end

context 'when the user is logged in and is the author of the comment' do
before do
User.session = comment_a.user
render_inline(described_class.new(comment: comment_a, commentable: commentable, level: 1))
end

it 'is possible to reply to the comment' do
expect(subject).to have_text('Reply')
expect(rendered_content).to have_text('Reply')
end

it 'is possible to edit the comment' do
expect(subject).to have_selector('.dropdown-menu', text: 'Edit')
expect(rendered_content).to have_selector('.dropdown-menu', text: 'Edit')
end

it 'is possible to remove the comment' do
expect(subject).to have_selector('.dropdown-menu', text: 'Delete')
expect(rendered_content).to have_selector('.dropdown-menu', text: 'Delete')
end
end

context 'when the user is logged in but is not the author of the comment' do
before do
User.session = build(:confirmed_user)
render_inline(described_class.new(comment: comment_a, commentable: commentable, level: 1))
end

it 'is possible to reply to the comment' do
expect(subject).to have_text('Reply')
expect(rendered_content).to have_text('Reply')
end

it 'is not possible to edit the comment' do
expect(subject).not_to have_selector('.dropdown-menu', text: 'Edit')
expect(rendered_content).not_to have_selector('.dropdown-menu', text: 'Edit')
end

it 'is not possible to remove the comment' do
expect(subject).not_to have_selector('.dropdown-menu', text: 'Delete')
expect(rendered_content).not_to have_selector('.dropdown-menu', text: 'Delete')
end
end

Expand All @@ -54,24 +78,28 @@
let(:comment_d) { create(:comment_request, commentable: commentable, body: 'Comment D', parent: comment_c) }
let!(:comment_e) { create(:comment_request, commentable: commentable, body: 'Comment E', parent: comment_d) }

before do
render_inline(described_class.new(comment: comment_a, commentable: commentable, level: 1))
end

it 'displays the parent comment' do
expect(subject).to have_text("(#{comment_a.user.login})\nwrote")
expect(subject).to have_text('Comment A')
expect(rendered_content).to have_text("(#{comment_a.user.login})\nwrote")
expect(rendered_content).to have_text('Comment A')
end

it 'displays the comments on level 2 in the 2nd level' do
expect(subject).to have_selector('.timeline-item-comment > .timeline-item-comment', text: "(#{comment_c.user.login})\nwrote")
expect(subject).to have_selector('.timeline-item-comment > .timeline-item-comment > .timeline-item-comment', text: 'Comment C')
expect(rendered_content).to have_selector('.timeline-item-comment > .timeline-item-comment', text: "(#{comment_c.user.login})\nwrote")
expect(rendered_content).to have_selector('.timeline-item-comment > .timeline-item-comment > .timeline-item-comment', text: 'Comment C')
end

it 'does not display the comments on level 4 in the 4th one' do
expect(subject).not_to have_selector((['.timeline-item-comment'] * 4).join(' > '), text: "(#{comment_e.user.login})\nwrote")
expect(subject).not_to have_selector((['.timeline-item-comment'] * 5).join(' > '), text: "(#{comment_e.user.login})\nwrote")
expect(rendered_content).not_to have_selector((['.timeline-item-comment'] * 4).join(' > '), text: "(#{comment_e.user.login})\nwrote")
expect(rendered_content).not_to have_selector((['.timeline-item-comment'] * 5).join(' > '), text: "(#{comment_e.user.login})\nwrote")
end

it 'displays the comments on level 4 in the 3rd level' do
expect(subject).to have_selector((['.timeline-item-comment'] * 3).join(' > '), text: "(#{comment_e.user.login})\nwrote")
expect(subject).to have_selector((['.timeline-item-comment'] * 4).join(' > '), text: 'Comment E')
expect(rendered_content).to have_selector((['.timeline-item-comment'] * 3).join(' > '), text: "(#{comment_e.user.login})\nwrote")
expect(rendered_content).to have_selector((['.timeline-item-comment'] * 4).join(' > '), text: 'Comment E')
end
end
end

0 comments on commit 3db2f54

Please sign in to comment.