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

Food tests #16

Merged
merged 8 commits into from
Aug 2, 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
2 changes: 1 addition & 1 deletion app/controllers/public_recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class PublicRecipesController < ApplicationController
def index
@public_recipes = Recipe.where(public: true)
@public_recipes = Recipe.includes(:user).where(public: true).order(created_at: :desc)
end
end
35 changes: 23 additions & 12 deletions app/views/public_recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<% @public_recipes.each do |recipe| %>
<div class="w-full">

<div class="flex justify-between text-center w-4/5 m-auto">
<h1 class="font-bold text-4xl">Public Recipes</h1>
<%= link_to 'New recipe', new_recipe_path, class: "invisible rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus" %>

</div>

<div class="w-2/3 m-auto mt-10 flex flex-col items-center">
<% @public_recipes.each do |recipe| %>
<div class="p-2 border-2 w-full max-w-lg flex gap-2 h-24">
<div class="flex flex-col gap-1 w-1/3 ">
<%= link_to recipe_path(recipe) do %>
<p class="w-1/3 font-bold text-xl"> <%= recipe.name %> </p>
<% end %>
<div>
by: <%=recipe.user.name%>
</div>
</div>

<div class="p-2 border-2 w-full max-w-lg flex gap-2">
<div class="flex flex-col gap-1 w-1/3">
<%= link_to recipe_path(recipe) do %>
<p class="w-1/3 font-bold text-xl"> <%= recipe.name %> </p>
<p class="w-2/3"> <%=recipe.description %> </p>
<% end %>
<%= button_to 'REMOVE', recipe_path(recipe), method: :delete, class:"bg-rose-500 hover:bg-rose-800 text-white rounded-lg px-4 py-2" %>
</div>

<%= link_to recipe_path(recipe) do %>
<p class="w-2/3"> <%=recipe.description %> </p>
<% end %>

</div>

<% end %>
<% end %>
</div>
</div>
18 changes: 9 additions & 9 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<div class="w-full flex justify-end">
<div class="flex justify-between text-center w-4/5 m-auto">
<h1 class="font-bold text-4xl">Recipes</h1>

<%= link_to 'New recipe', new_recipe_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus" %>

<%= link_to 'New recipe', new_recipe_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus" %>
</div>
</div>

<% @recipes.each do |recipe| %>

<div class="p-2 border-2 w-full max-w-lg flex gap-2">
<div class="w-2/3 m-auto mt-10 flex flex-col items-center">
<% @recipes.each do |recipe| %>
<div class="p-2 border-2 w-full max-w-lg flex gap-2 h-24">
<div class="flex flex-col gap-1 w-1/3">
<%= link_to recipe_path(recipe) do %>
<p class="w-1/3 font-bold text-xl"> <%= recipe.name %> </p>
Expand All @@ -17,7 +18,6 @@
<%= link_to recipe_path(recipe) do %>
<p class="w-2/3"> <%=recipe.description %> </p>
<% end %>

</div>

<% end %>
<% end %>
</div>
10 changes: 8 additions & 2 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

<div class="w-full flex justify-between p-4 flex-wrap">
<%= link_to 'Generate Shopping List', shopping_list_recipe_path(@recipe), class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus"%>
<%= link_to 'Add Ingedrient', new_recipe_recipe_food_path(@recipe), class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus"%>
<%if can? :edit, @recipe %>
<%= link_to 'Add Ingedrient', new_recipe_recipe_food_path(@recipe), class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium hover:bg-primary-focus"%>
<% end %>
</div>

<div class="min-w-full text-center">
Expand All @@ -39,7 +41,9 @@
<th class="py-3"> Food </th>
<th class="py-3"> Quantity </th>
<th class="py-3"> Value </th>
<th class="py-3"> Actions </th>
<%if can? :edit, @recipe %>
<th class="py-3"> Actions </th>
<% end %>
</tr>

<tbody class="w-full max-w-lg p-4">
Expand All @@ -53,9 +57,11 @@

<td class="">
<div class="flex gap-1 justify-center">
<%if can? :edit, @recipe %>
<%= link_to 'Modify', edit_recipe_recipe_food_path(@recipe, recipe_food), class:"bg-green-500 hover:bg-green-600 text-white rounded-lg px-4 py-2 " %>

<%= button_to "Delete", recipe_recipe_food_path(@recipe, recipe_food), method: :delete, class:"bg-rose-500 hover:bg-rose-800 text-white rounded-lg px-4 py-2" %>
<% end %>
</div>
</td>

Expand Down
42 changes: 42 additions & 0 deletions spec/models/food_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe Food, type: :model do
describe 'associations' do
it 'should have many recipe_foods' do
expect(Food.reflect_on_association(:recipe_foods).macro).to eq(:has_many)
end
it 'should have many recipes' do
expect(Food.reflect_on_association(:recipes).macro).to eq(:has_many)
end
it 'should belong to a user' do
expect(Food.reflect_on_association(:user).macro).to eq(:belongs_to)
end
end
describe 'validations' do
let(:user) { User.create(name: 'test', email: 'test@test.com', password: 'password') }
let(:food) { Food.create(name: 'food', measurement_unit: 'unit', quantity: 10, price: 10, user_id: user.id) }
it 'should be valid with attributes' do
expect(food).to be_valid
end
it 'should not be valid without a name' do
food.name = nil
expect(food).not_to be_valid
end
it 'should not have a name longer than 250 characters' do
food.name = 'a' * 251
expect(food).not_to be_valid
end
it 'should not be valid without a measurement_unit' do
food.measurement_unit = nil
expect(food).not_to be_valid
end
it 'should not be valid without a quantity' do
food.quantity = nil
expect(food).not_to be_valid
end
it 'should not be valid without a price' do
food.price = nil
expect(food).not_to be_valid
end
end
end
62 changes: 62 additions & 0 deletions spec/requests/foods_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'rails_helper'

RSpec.describe FoodsController, type: :request do
describe 'Foods' do
let(:user) { User.new(name: 'test', email: 'test@test.com', password: 'password') }
let(:food) { Food.new(name: 'food', measurement_unit: 'units', price: 10, quantity: 10, user_id: user.id) }
before do
user.skip_confirmation!
user.save
authenticate_user(user)
food.save
end
def authenticate_user(user)
post user_session_path, params: { user: { email: user.email, password: user.password } }
follow_redirect!
end
describe '#index' do
it 'returns a success response' do
get root_path
expect(response).to have_http_status(:ok)
end
end
describe '#show' do
it 'returns a success response' do
get food_path(food)
expect(response).to have_http_status(:ok)
end
end
describe '#new' do
it 'returns a success response' do
get new_food_path
expect(response).to have_http_status(:ok)
end
end
describe '#edit' do
it 'returns a success response' do
get edit_food_path(food)
expect(response).to have_http_status(:ok)
end
end
describe '#create' do
it 'returns a success response' do
post foods_path,
params: { food: { name: 'food', measurement_unit: 'units', price: 10, quantity: 10, user_id: user.id } }
expect(response).to redirect_to(food_path(Food.last))
end
end
describe '#update' do
it 'returns a success response' do
put food_path(food),
params: { food: { name: 'food', measurement_unit: 'units', price: 10, quantity: 10, user_id: user.id } }
expect(response).to redirect_to(food_path(food))
end
end
describe '#destroy' do
it 'returns a success response' do
delete food_path(food)
expect(response).to redirect_to(foods_path)
end
end
end
end
32 changes: 32 additions & 0 deletions spec/requests/public_recipes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe PublicRecipesController, type: :request do
let(:user) { User.new(name: 'User1', email: 'user1@gmail.com', password: '123123') }
let(:recipe) do
Recipe.new(name: 'recipe_1',
preparation_time: 20,
cooking_time: 30,
description: 'interesting recipe',
public: false,
user_id: user.id)
end

before do
user.skip_confirmation!
user.save
recipe.save
authenticate_user(user)
end

def authenticate_user(user)
post user_session_path, params: { user: { email: user.email, password: user.password, name: user.name } }
follow_redirect!
end

describe 'Get #index' do
it 'returns a successful response' do
get public_recipes_path
expect(response).to have_http_status(:ok)
end
end
end
116 changes: 116 additions & 0 deletions spec/system/foods_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require 'rails_helper'

RSpec.describe 'Foods', type: :system do
include Devise::Test::IntegrationHelpers

before(:each) do
@user = User.create(
name: 'User one',
email: 'user@example.com',
password: 'password',
confirmation_token: '23moe234f',
confirmed_at: Time.now, confirmation_sent_at: Time.now
)
@food = Food.create(
name: 'my_food',
measurement_unit: 'units',
price: 10,
quantity: 10,
user_id: @user.id
)
sign_in @user
end
describe '#index' do
before(:each) do
visit root_path
end

scenario 'should have tittle Food' do
assert_text 'Foods'
end
scenario 'should have link to New food' do
assert_text 'New food'
click_on 'New food'
assert_current_path new_food_path
end
scenario 'should have food name, measurement unit, price' do
assert_text @food.name
assert_text @food.measurement_unit
assert_text @food.price
end
scenario 'on clicking on a food name, should redirect to that food show page' do
click_on @food.name
assert_current_path food_path(@food)
end
scenario 'should delete food when delete button is clicked' do
assert_text 'Delete'
click_on 'Delete'
assert_no_text @food.name
end
end
describe '#show' do
before(:each) do
visit food_path(@food)
end
scenario 'should have food name, measurement unit, price' do
assert_text @food.name
assert_text @food.measurement_unit
assert_text @food.price
assert_text @food.quantity
end
scenario 'should have link to Edit food' do
assert_text 'Edit this food'
click_on 'Edit this food'
assert_current_path edit_food_path(@food)
end
scenario 'should have link to Delete food' do
assert_text 'Destroy this food'
click_on 'Destroy this food'
assert_current_path foods_path
end
end
describe '#new' do
before(:each) do
visit new_food_path
end
scenario 'should have tittle New Food' do
assert_text 'New food'
end
scenario 'should have form to create new food' do
assert_text 'Name'
assert_text 'Measurement unit'
assert_text 'Price'
assert_text 'Quantity'
end
scenario 'should create new food' do
fill_in 'Name', with: 'my_food_2'
fill_in 'Measurement unit', with: 'units'
fill_in 'Price', with: 10
fill_in 'Quantity', with: 10
click_on 'Create Food'
assert_current_path food_path(Food.last)
end
end
describe '#edit' do
before(:each) do
visit edit_food_path(@food)
end
scenario 'should have tittle Edit Food' do
assert_text 'Editing food'
end
scenario 'should have form to edit food' do
assert_text 'Name'
assert_text 'Measurement unit'
assert_text 'Price'
assert_text 'Quantity'
end
scenario 'should update food' do
fill_in 'Name', with: 'my_food_3'
fill_in 'Measurement unit', with: 'units'
fill_in 'Price', with: 12
fill_in 'Quantity', with: 15
click_on 'Update Food'
assert_current_path food_path(@food)
end
end
end
Loading
Loading