Fix integration tests for comments component

This commit is contained in:
riggraz
2019-09-21 16:11:13 +02:00
parent 7729057180
commit 31828d48ba

View File

@@ -7,7 +7,7 @@ feature 'comments', type: :system, js: true do
let(:comment3) { FactoryBot.create(:comment, post: post, parent: comment2) } let(:comment3) { FactoryBot.create(:comment, post: post, parent: comment2) }
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:commentsSelector) { '.comments' } let(:commentsSelector) { '.commentsContainer' }
let(:commentListSelector) { '.commentList' } let(:commentListSelector) { '.commentList' }
let(:commentSelector) { '.comment' } let(:commentSelector) { '.comment' }
let(:commentAuthorSelector) { '.commentAuthor' } let(:commentAuthorSelector) { '.commentAuthor' }
@@ -21,16 +21,43 @@ feature 'comments', type: :system, js: true do
comment3 comment3
end end
def log_in_as(user)
user.confirm
sign_in user
end
before(:each) do before(:each) do
create_comments create_comments
visit post_path(post)
end end
it 'renders correctly' do it 'renders correctly' do
visit post_path(post)
expect(page).to have_selector(commentsSelector, count: 1) expect(page).to have_selector(commentsSelector, count: 1)
end end
it 'renders a new comment form and replies form if logged in' do
log_in_as user
visit post_path(post)
comments_count = Comment.where(post_id: post.id).count
expect(page).to have_selector(newCommentFormSelector, count: 1, visible: true)
expect(page).to have_selector(newCommentBodySelector, count: 1, visible: true)
# don't know why don't work
# expect(page).to have_selector(newCommentFormSelector, count: comments_count, visible: false)
# expect(page).to have_selector(newCommentBodySelector, count: comments_count, visible: false)
end
it 'does not render a new comment form if not logged in' do
visit post_path(post)
expect(page).to have_no_selector(newCommentBodySelector)
end
it 'renders all comments of a post' do it 'renders all comments of a post' do
visit post_path(post)
within commentsSelector do within commentsSelector do
expect(page).to have_selector(commentSelector, count: 3) expect(page).to have_selector(commentSelector, count: 3)
expect(page).to have_content(/#{comment1.body}/i) expect(page).to have_content(/#{comment1.body}/i)
@@ -40,15 +67,19 @@ feature 'comments', type: :system, js: true do
end end
it 'renders nested comments' do it 'renders nested comments' do
visit post_path(post)
within commentsSelector do within commentsSelector do
expect(page).to have_selector( expect(page).to have_selector(
"#{commentListSelector} > #{commentListSelector}", "#{commentListSelector} > #{commentListSelector}",
count: 1 count: 1
) # 1 nested comment ) # one nested comment
end end
end end
it 'renders the author full name for each comment' do it 'renders the author full name for each comment' do
visit post_path(post)
page.all(:css, commentSelector).each do |comment| page.all(:css, commentSelector).each do |comment|
expect(comment).to have_selector(commentAuthorSelector) expect(comment).to have_selector(commentAuthorSelector)
expect(comment).to have_content(/#{post.user.full_name}/i) expect(comment).to have_content(/#{post.user.full_name}/i)
@@ -56,9 +87,25 @@ feature 'comments', type: :system, js: true do
end end
it 'renders a reply button for each comment' do it 'renders a reply button for each comment' do
visit post_path(post)
page.all(:css, commentSelector).each do |comment| page.all(:css, commentSelector).each do |comment|
expect(comment).to have_selector(commentReplyBtnSelector) expect(comment).to have_selector(commentReplyBtnSelector)
expect(comment).to have_content(/#{'reply'}/i) expect(comment).to have_content(/#{'reply'}/i)
end end
end end
it 'enables users to comment' do
log_in_as user
visit post_path(post)
comments_count = Comment.where(post_id: post.id).count
comment_body = 'this is a comment!'
find(newCommentBodySelector).fill_in with: comment_body
click_button 'Submit'
expect(page).to have_content(comment_body, count: 2)
expect(Comment.where(post_id: post.id).count).to eq(comments_count + 1)
end
end end