From dfee92da9cc155fdf0133eed89bf1f2109563359 Mon Sep 17 00:00:00 2001 From: riccardo Date: Sat, 28 Sep 2019 13:49:37 +0200 Subject: [PATCH] Add system tests for likes --- spec/system/comments_spec.rb | 19 +++++------ spec/system/likes_spec.rb | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 spec/system/likes_spec.rb diff --git a/spec/system/comments_spec.rb b/spec/system/comments_spec.rb index 66ba47ed..350c9cd4 100644 --- a/spec/system/comments_spec.rb +++ b/spec/system/comments_spec.rb @@ -95,16 +95,17 @@ feature 'comments', type: :system, js: true do end end - # it 'enables users to comment' do - # log_in_as user - # visit post_path(post) + 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!' + 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' + find(newCommentBodySelector).fill_in with: comment_body + click_button 'Submit' + visit post_path(post) - # expect(Comment.where(post_id: post.id).count).to eq(comments_count + 1) - # end + expect(Comment.where(post_id: post.id).count).to eq(comments_count + 1) + end end \ No newline at end of file diff --git a/spec/system/likes_spec.rb b/spec/system/likes_spec.rb new file mode 100644 index 00000000..5dcab4dc --- /dev/null +++ b/spec/system/likes_spec.rb @@ -0,0 +1,62 @@ +require 'rails_helper' + +feature 'likes', type: :system, js: true do + let(:board) { FactoryBot.create(:board) } + let(:post1) { FactoryBot.create(:post, board: board) } + let(:post2) { FactoryBot.create(:post, board: board) } + let(:user) { FactoryBot.create(:user) } + + let(:board_container) { '.boardContainer' } + let(:like_button_container_selector) { '.likeButtonContainer' } + let(:like_button_selector) { '.likeButton' } + let(:likes_count_label_selector) { '.likesCountLabel' } + + before(:each) do + board + post1 + post2 + end + + context 'in post list of Board component' do + it 'renders correctly for each post' do + visit board_path(board) + + within board_container do + expect(page).to have_selector(like_button_container_selector, count: 2) + expect(page).to have_selector(like_button_selector, count: 2) + expect(page).to have_selector(likes_count_label_selector, count: 2) + end + end + + it 'redirects when user not logged in' do + visit board_path(board) + + within board_container do + find(like_button_selector, match: :first).click + expect(page.current_path).to eq(new_user_session_path) + end + end + + it 'likes and unlikes' do + user.confirm + sign_in user + visit board_path(board) + + within board_container do + first_like_button = find(like_button_selector, match: :first) + like_container = find(like_button_container_selector, match: :first) + + # starts at zero likes + expect(like_container).to have_content(0) + + # like + first_like_button.click + expect(like_container).to have_content(1) + + # unlike + first_like_button.click + expect(like_container).to have_content(0) + end + end + end +end \ No newline at end of file