Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Milestone: #Capybara specs-pair programming #9

Merged
merged 23 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Blog-app is a fully functioning website that show the list of posts and empower
- **Controllers specs**
- **Users and posts views**
- **Creating Forms**
- **Intergration specs and fix N+1**

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down Expand Up @@ -106,12 +107,17 @@ it will install the required gemfile for running the project
- GitHub: [@cosywasswa](https://github.com/cosywasswa)
- LinkedIn: [cosmas-wasswa](https://www.linkedin.com/in/cosmas-wasswa)

👤 **Manzi Tresor**

- GitHub: [@githubhandle](https://github.com/manzitresor)
- Twitter: [@githubhandle](https://twitter.com/MANZITresor3)
- LinkedIn: [LinkedIn](https://www.linkedin.com/in/manzi-tresor-783b4022a/)

<p align="right">(<a href="#readme-top">back to top</a>)</p>

<!-- FUTURE FEATURES -->

## 🔭 Future Features <a name="future-features"></a>
- Integration specs for Views and fixing n+1 problems.
- Add Devise.
- Add authorization rules.
- Add API endpoints.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PostsController < ApplicationController
def index
@user = User.find(params[:user_id])
@user = User.includes(posts: :comments).find(params[:user_id])
@posts = @user.posts.includes(:comments)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def index
end

def show
@user = User.find(params[:id])
@user = User.includes(:posts).find(params[:id])
@posts = @user.posts
end
end
5 changes: 3 additions & 2 deletions app/views/posts/_posts_details.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="posts-details">
<h2><%= link_to "Post ", user_post_url(@user.id, post.id) %> #<%= post.id %></h2>
<h3><%= post.text %></h3>
<h2><%= link_to "Post ##{post.id}", user_post_url(@user.id, post.id) %></h2>
<h3><%= post.title %></h3>
<p><%= post.text %></p>
<p>Comments:<%= post.comments_counter %>,Likes:<%= post.likes.size %></p>
</div>
2 changes: 1 addition & 1 deletion app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section class="container">
<article class="users-article">
<div class="photo"><%= @user.photo %></div>
<div class="photo"><%=image_tag(@user.photo, :alt => "User profile photo.", :class => "display-photo") %></div>
<div class="details">
<h2><%= @user.name %></h2>
<p>Number of posts: <%= @user.posts_counter %></p>
Expand Down
1 change: 1 addition & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</div>
</div>
<div class="post-body">
<h4><%= @post.title %></h4>
<h4><%= @post.text %></h4>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user_info.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<article class="users-article">
<div class="photo"><%= user.photo %></div>
<div class="photo"><%=image_tag(user.photo, :alt => "User profile photo.", :class => "display-photo") %></div>
<div class="details">
<h2><%= link_to user.name, user_url(user.id) %></h2>
<p>Number of posts: <%= user.posts_counter %></p>
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section class="container">
<article class="users-article">
<div class="photo"><%= @user.photo %></div>
<div class="photo"><%=image_tag(@user.photo, :alt => "User profile photo.", :class => "display-photo") %></div>
<div class="details">
<h2><%= @user.name %></h2>
<p><%= link_to "Number of posts:", user_posts_url(@user.id) %> <%= @user.posts_counter %></p>
Expand All @@ -12,7 +12,7 @@
</div>
<% @user.recent_posts.each do |post| %>
<div class="post-details">
<h2><%= link_to "Post #", user_post_url(@user.id, post.id) %><%= post.id %></h2>
<h2><%= link_to "Post ##{post.id}", user_post_url(@user, post) %></h2>
<h3><%= post.text %></h3>
<p>Comments:<%= post.comments_counter %>,Likes:<%= post.likes_counter %></p>
</div>
Expand Down
76 changes: 76 additions & 0 deletions spec/features/post_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'rails_helper'

RSpec.describe 'posts#index', type: :feature do
before(:each) do
@user_one = User.create(
id: 1,
name: 'Cosmas',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Web developer from Uganda',
posts_counter: 0
)

@posts = [
@post1 = Post.create(
author: @user_one,
title: 'Blog1',
text: 'This is my first post'
),
@post2 = Post.create(
author: @user_one,
title: 'Blog2',
text: 'This is my second post'
)
]
visit user_posts_url(user_id: @user_one.id)
end
describe '#Indexpage' do
it 'can see the user profile picture.' do
expect(page).to have_css("img[src='#{@user_one.photo}']")
end
it 'I can see the user username.' do
expect(page).to have_content(@user_one.name.to_s)
end
it 'I can see the number of posts the user has written.' do
expect(page).to have_content(@user_one.posts_counter.to_s)
end

it 'should see title of the post' do
@posts.each do |post|
expect(page).to have_content(post.title.to_s)
end
end

it 'should see body of the post' do
@posts.each do |post|
expect(page).to have_content(post.text.to_s)
end
end

it 'I can see the first comments on a post.' do
@posts.each do |post|
post.recent_comments.each do |comment|
expect(page).to have_content(comment.user.text)
end
end
end
it 'I can see how many comments a post has' do
@posts.each do |post|
expect(page).to have_content("Comments:#{post.comments_counter}")
end
end
it 'I can see how many likes a post has' do
@posts.each do |post|
expect(page).to have_content("Likes:#{post.likes_counter}")
end
end
end
describe 'GET show/page' do
it 'When I click on a post, I am redirected to that postshow page.' do
visit user_posts_url(user_id: @user_one.id)
post = @posts.first
click_link(post.id)
expect(page).to have_current_path(user_post_path(@user_one.id, post.id))
end
end
end
64 changes: 64 additions & 0 deletions spec/features/post_show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'rails_helper'

RSpec.describe 'posts#index', type: :feature do
before(:each) do
@user_one = User.create(
id: 1,
name: 'Cosmas',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Web developer from Uganda',
posts_counter: 0
)

@posts = [
@post1 = Post.create(
author: @user_one,
title: 'Blog1',
text: 'This is my first post',
comments_counter: 0,
likes_counter: 0
),
@post2 = Post.create(
author: @user_one,
title: 'Blog2',
text: 'This is my second post',
comments_counter: 0,
likes_counter: 0
)
]
visit user_posts_url(user_id: @user_one.id)
end

describe 'show page' do
before(:each) do
visit user_post_path(@user_one, @post1)
end
it 'should see title of the post' do
expect(page).to have_content(@post1.title.to_s)
end
it 'I can see who wrote the post.' do
expect(page).to have_content(@user_one.name.to_s)
end
it 'I can see how many comments it has' do
expect(page).to have_content(@post1.comments_counter)
end
it 'I can see how many likes a post has' do
expect(page).to have_content("Likes:#{@post1.likes_counter}")
end

it 'should see body of the post' do
expect(page).to have_content(@post1.text.to_s)
end

it 'I can see the username of each commentor' do
@post1.recent_comments.each do |comment|
expect(page).to have_content(comment.user.name)
end
end
it 'I can see the comment each commentor left.' do
@post1.recent_comments.each do |comment|
expect(page).to have_content(comment.user.text)
end
end
end
end
44 changes: 44 additions & 0 deletions spec/features/user_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rails_helper'

RSpec.describe 'users#index', type: :feature do
before(:each) do
@users = [
@user1 = User.create(
name: 'Cosmas',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Web developer from Uganda',
posts_counter: 0
),
@user2 = User.create(
name: 'Martin',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Software developer.',
posts_counter: 0
)
]
visit users_url
end
describe '#indexpage' do
it 'can see the username of all other users.' do
expect(page).to have_content(@users[0].name)
expect(page).to have_content(@users[1].name)
end

it 'I can see the profile picture for each user.' do
@users.each do |user|
expect(page).to have_css("img[src='#{user.photo}']")
end
end

it 'I can see the number of posts each user has written.' do
@users.each do |user|
expect(page).to have_content("Number of posts: #{user.posts_counter}")
end
end

it 'When I click on a user, I am redirected to that user show page.' do
click_link(@users[0].name)
expect(page).to have_current_path(user_path(@users[0].id))
end
end
end
75 changes: 75 additions & 0 deletions spec/features/user_show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'rails_helper'

RSpec.describe 'users#show page', type: :feature do
before(:each) do
@user1 = User.create(
name: 'Cosmas',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Web developer from Uganda',
posts_counter: 0
)

@user2 = User.create(
name: 'Martin',
photo: 'https://unsplash.com/photos/F_-0BxGuVvo',
bio: 'Software developer.',
posts_counter: 0
)
@posts = [
Post.create(author: @user1, title: 'My test post', text: 'this is a test post1',
comments_counter: 0, likes_counter: 0),
Post.create(author: @user1, title: 'My test post2', text: 'this is a test post2',
comments_counter: 0, likes_counter: 0),
Post.create(author: @user1, title: 'My test post3', text: 'this is a test post3',
comments_counter: 0, likes_counter: 0)
]

visit users_url
end
describe 'show page' do
before(:each) do
visit user_path(@user1)
end

it 'can see the user profile picture.' do
expect(page).to have_css("img[src='#{@user1.photo}']")
end

it 'can see the user username' do
expect(page).to have_content(@user1.name.to_s)
end

it 'I can see the number of posts the user has written.' do
expect(page).to have_content("Number of posts: #{@user1.posts_counter}")
end

it 'I can see the user bio.' do
expect(page).to have_content(@user1.bio.to_s)
end

it 'I can see the user first 3 posts.' do
@user1.recent_posts.each do |post|
expect(page).to have_content(post.text)
end
end

it 'can see a button to view all user posts' do
expect(page).to have_selector('button', text: 'See all posts')
end
end
describe 'GET/posts/show' do
it 'When I click a user post, it redirects me to that post show page' do
visit user_path(@user1)

post = @user1.recent_posts.first
click_link(post.id)
expect(page).to have_current_path(user_post_path(@user1, post))
end

it 'When I click to see all posts, it redirects me to the user posts index page.' do
visit user_path(@user1)
click_link 'See all posts'
expect(page).to have_current_path(user_posts_path(@user1))
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'capybara'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand Down