Skip to content

Commit

Permalink
Merge pull request #27 from HafizMassamTabraizKhan/integrations
Browse files Browse the repository at this point in the history
Added Integration tests
  • Loading branch information
ProgramKingAbel authored Sep 22, 2023
2 parents b01c32c + 4af68ad commit 54a3e80
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AllCops:
NewCops: enable
Exclude:
- "db/**/*"
- "bin/*"
- "bin/*"
- "config/**/*"
- "Guardfile"
- "Rakefile"
Expand All @@ -25,8 +25,8 @@ Metrics/AbcSize:
Metrics/ClassLength:
Max: 150
Metrics/BlockLength:
AllowedMethods: ['describe']
Max: 30
AllowedMethods: ["describe"]
Max: 50

Style/Documentation:
Enabled: false
Expand Down
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem 'importmap-rails'
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem 'turbo-rails'

gem 'capybara', '>= 2.13.0'

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem 'stimulus-rails'

Expand Down Expand Up @@ -51,6 +53,7 @@ gem 'bootsnap', require: false
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'rspec-rails'
gem 'shoulda-matchers'
end
Expand All @@ -68,8 +71,8 @@ end

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem 'capybara'
gem 'selenium-webdriver'
gem 'warden'
gem 'webdrivers'
end

Expand Down
9 changes: 8 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ GEM
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.12.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand Down Expand Up @@ -291,10 +296,11 @@ PLATFORMS
DEPENDENCIES
bootsnap
cancancan
capybara
capybara (>= 2.13.0)
debug
devise (~> 4.9)
dotenv-rails
factory_bot_rails
importmap-rails
jbuilder
pg (~> 1.1)
Expand All @@ -308,6 +314,7 @@ DEPENDENCIES
stimulus-rails
turbo-rails
tzinfo-data
warden
web-console
webdrivers

Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# 📖 Recipe App<a name="about-project"></a>

**Recipe App**
**Recipe App**
The Recipe app keeps track of all your recipes, ingredients, and inventory. It will allow you to save ingredients, keep track of what you have, create recipes, and generate a shopping list based on what you have and what you are missing from a recipe. The app allows you to make recipes public so anyone can access them.

## 🛠 Built With <a name="built-with"></a>
Expand Down Expand Up @@ -68,7 +68,6 @@ The Recipe app keeps track of all your recipes, ingredients, and inventory. It w
- **A general shopping list view**
- **A list of all public recipes from other users**


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

<!-- GETTING STARTED -->
Expand All @@ -89,7 +88,7 @@ In order to run this project you need:

Clone this repository to your desired folder:

```sh
```sh
git clone git@github.com:HafizMassamTabraizKhan/Recipe-App.git
cd Recipe-App
```
Expand Down Expand Up @@ -137,6 +136,12 @@ To run tests, run the following command:
- Twitter: [@CEOAbel1](https://twitter.com/CEOAbel1)
- LinkedIn: [LinkedIn](https://www.linkedin.com/in/abel-morara)

### 👤 **Peter Dumbari**

- GitHub: [@Peter-Dumbari](https://github.com/Peter-Dumbari)
- Twitter: [@Peterdumbari](https://twitter.com/peterdumbari)
- LinkedIn: [@Peter-Dumbari](https://www.linkedin.com/in/peterdumbari)

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

<!-- FUTURE FEATURES -->
Expand All @@ -147,7 +152,7 @@ To run tests, run the following command:
- [ ] **Inventory Shopping Lists functionality**
- [ ] **Cooking Group meetups and formation**
- [ ] **Publish Statuses on trending recipes**
**
\*\*

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

Expand All @@ -165,7 +170,7 @@ Feel free to check the [issues page](../../issues/).

## ⭐️ Show your support <a name="support"></a>

If you like this project consider leaving a star in the repo.
If you like this project consider leaving a star in the repo.

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

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/inventories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ def destroy
def inventory_params
params.require(:inventory).permit(:name, :description)
end

def can_manage_inventory?(inventory)
inventory.user == current_user
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/factories/food.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :food do
name { 'Food Name' }
measurement_unit { 'Unit' }
price { 10 }
end
end
8 changes: 8 additions & 0 deletions spec/factories/inventory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :inventory do
name { 'Inventory Name' }
user { create(:user) }
user_id { user.id }
description { 'Inventory Description' }
end
end
7 changes: 7 additions & 0 deletions spec/factories/inventory_food.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :inventory_food do
inventory { create(:inventory) }
food { create(:food) }
quantity { 10 }
end
end
9 changes: 9 additions & 0 deletions spec/factories/recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :recipe do
name { 'Recipe Name' }
preparation_time { 3 }
cooking_time { 4 }
description { 'Recipe Description' }
public { true }
end
end
7 changes: 7 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :user do
name { 'User Name' }
email { 'user@example.com' }
password { 'password123' }
end
end
19 changes: 19 additions & 0 deletions spec/features/foods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.feature 'Food Details', type: :feature do
let(:food) { create(:food) }

before do
visit food_path(food)
end

scenario 'shows the food details' do
expect(page).to have_content(food.name)
expect(page).to have_content(food.measurement_unit)
expect(page).to have_content("$ #{food.price}")
end

scenario 'has a back link' do
expect(page).to have_link('Back', href: foods_path)
end
end
22 changes: 22 additions & 0 deletions spec/features/inventories_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'

RSpec.feature 'Inventory Index', type: :feature do
let(:inventory) { create(:inventory) }

before do
create(:inventory_food, inventory:, food: create(:food))
visit inventory_path(inventory)
end

scenario 'shows the inventory name' do
expect(page).to have_content(inventory.name)
end

scenario 'has an add food link' do
expect(page).to have_link('Add Food', href: new_inventory_inventory_food_path(inventory))
end

scenario 'shows the inventory foods' do
expect(page).to have_content(inventory.inventory_foods.first.food.name)
end
end
33 changes: 33 additions & 0 deletions spec/features/public_recipes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# spec/features/public_recipes_spec.rb

require 'rails_helper'

RSpec.feature 'Public Recipes List', type: :feature do
scenario 'User views the list of public recipes' do
# Create some sample data for public recipes
user = FactoryBot.create(:user, name: 'John Doe')
public_recipe1 = FactoryBot.create(:recipe, name: 'Recipe 1', user:)
public_recipe2 = FactoryBot.create(:recipe, name: 'Recipe 2', user:)

# Visit the public recipes page
visit root_path

# Assertions to check the content of the page
expect(page).to have_selector('h1', text: 'Public Recipes list')

# Check for each public recipe in the list
within('.card', text: 'Recipe 1') do
expect(page).to have_link('Recipe 1', href: recipe_path(public_recipe1))
expect(page).to have_content('by John Doe')
expect(page).to have_content('Total food items: 0') # Customize as needed
expect(page).to have_content('Total price: $0') # Adjusted expectation
end

within('.card', text: 'Recipe 2') do
expect(page).to have_link('Recipe 2', href: recipe_path(public_recipe2))
expect(page).to have_content('by John Doe')
expect(page).to have_content('Total food items: 0') # Customize as needed
expect(page).to have_content('Total price: $0') # Adjusted expectation
end
end
end
46 changes: 46 additions & 0 deletions spec/features/recipes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.feature 'Shopping List Page', type: :feature do
let(:user) { FactoryBot.create(:user) }
let(:inventory) { FactoryBot.create(:inventory, user:, name: 'Sample Inventory') }
let(:recipe) { FactoryBot.create(:recipe, user:, name: 'Sample Recipe') }
let(:missing_foods) { [] }

before do
login_as(user)
bread = FactoryBot.create(:food, name: 'Bread', price: 2.5)
milk = FactoryBot.create(:food, name: 'Milk', price: 3.5)
recipe.recipe_foods.create(food: bread, quantity: 1)
recipe.recipe_foods.create(food: milk, quantity: 2)

visit shopping_list_path(recipe_id: recipe.id, inventory_id: inventory.id, selected_inventory_id: inventory.id)
end

def calculate_missing_foods
recipe.recipe_foods.each do |recipe_food|
missing_quantity = recipe_food.quantity - inventory.inventory_foods.where(food: recipe_food.food).sum(:quantity)
missing_foods << { food: recipe_food.food, quantity_needed: missing_quantity } if missing_quantity.positive?
end
end

scenario 'User views the shopping list details' do
# Calculate the missing foods
calculate_missing_foods

# Assertions to check the content of the page
expect(page).to have_content('Shopping List')
expect(page).to have_content('Amount of food to buy: 2')
expect(page).to have_link('Sample Recipe', href: recipe_path(recipe))
expect(page).to have_content('Total value of food needed: $9.5')
expect(page).to have_link('Sample Inventory', href: inventory_path(inventory))

within('table') do
expect(page).to have_content('Bread')
expect(page).to have_content('1')
expect(page).to have_content('$2.5')
expect(page).to have_content('Milk')
expect(page).to have_content('2')
expect(page).to have_content('$7.0')
end
end
end
52 changes: 52 additions & 0 deletions spec/features/shopping_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# spec/features/shopping_list_spec.rb

require 'rails_helper'

RSpec.feature 'Shopping List Page', type: :feature do
let(:user) { FactoryBot.create(:user) }
let(:inventory) { FactoryBot.create(:inventory, user:, name: 'Sample Inventory') }
let(:recipe) { FactoryBot.create(:recipe, user:, name: 'Sample Recipe') }
let(:missing_foods) { [] }

before do
login_as(user)

# Create two missing foods
bread = FactoryBot.create(:food, name: 'Bread', price: 2.5)
milk = FactoryBot.create(:food, name: 'Milk', price: 3.5)

recipe.recipe_foods.create(food: bread, quantity: 1)
recipe.recipe_foods.create(food: milk, quantity: 2)

# Calculate the missing foods
recipe.recipe_foods.each do |recipe_food|
missing_quantity = recipe_food.quantity - inventory.inventory_foods.where(food: recipe_food.food).sum(:quantity)
missing_foods << { food: recipe_food.food, quantity_needed: missing_quantity } if missing_quantity.positive?
end

visit shopping_list_path(recipe_id: recipe.id, inventory_id: inventory.id, selected_inventory_id: inventory.id)
end

scenario 'User views the shopping list details' do
# Assertions to check the content of the page
expect(page).to have_content('Shopping List')
expect(page).to have_content("Amount of food to buy: #{missing_foods.count}")
expect(page).to have_link('Sample Recipe', href: recipe_path(recipe))
# Calculate the total value of food needed
total_value = missing_foods.map do |missing_food|
missing_food[:food].price * missing_food[:quantity_needed]
end.sum.to_f
expect(page).to have_content("Total value of food needed: $#{total_value}")

expect(page).to have_link('Sample Inventory', href: inventory_path(inventory))
within('table') do
expect(page).to have_content('Bread')
expect(page).to have_content('1')
expect(page).to have_content('$2.5')

expect(page).to have_content('Milk')
expect(page).to have_content('2')
expect(page).to have_content('$7.0')
end
end
end
12 changes: 11 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{Rails.root}/spec/fixtures"

config.include Devise::Test::IntegrationHelpers, type: :feature
config.include Warden::Test::Helpers
config.include FactoryBot::Syntax::Methods
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
Expand Down Expand Up @@ -66,4 +68,12 @@
with.library :rails
end
end

# spec/rails_helper.rb or spec/spec_helper.rb

# Add the following lines at the beginning of the file
require 'factory_bot_rails'

# RSpec
# In your rails_helper.rb
end

0 comments on commit 54a3e80

Please sign in to comment.