generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Add 'food#index view' integration-test
- Loading branch information
Showing
1 changed file
with
35 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'foods/index', type: :view do | ||
before(:each) do | ||
assign(:foods, [ | ||
Food.create!( | ||
name: 'Name', | ||
measurement_unit: 'Measurement Unit', | ||
price: '9.99', | ||
quantity: 2, | ||
user: nil | ||
), | ||
Food.create!( | ||
name: 'Name', | ||
measurement_unit: 'Measurement Unit', | ||
price: '9.99', | ||
quantity: 2, | ||
user: nil | ||
) | ||
]) | ||
end | ||
RSpec.describe 'Foods', type: :system do | ||
it 'displays a list of foods or a message if empty' do | ||
new_user = create(:user) | ||
|
||
visit new_user_session_path | ||
|
||
fill_in 'Email', with: new_user.email | ||
fill_in 'Password', with: new_user.password | ||
|
||
click_button 'Log in' | ||
|
||
create(:food, name: 'Pizza', measurement_unit: 'kg', price: 10.99, quantity: 5, user: new_user) | ||
create(:food, name: 'Salad', measurement_unit: 'g', price: 5.5, quantity: 10, user: new_user) | ||
|
||
visit foods_path | ||
|
||
within('#foods', wait: 5) do | ||
expect(page).to have_link('New food', href: new_food_path) | ||
|
||
expect(page).to have_selector('.p-2 a.btn', text: 'New food') | ||
|
||
if Food.all.empty? | ||
expect(page).to have_selector('.alert.alert-info', text: 'There are no foods in your inventory.') | ||
else | ||
expect(page).to have_selector('table') | ||
expect(page).to have_selector('thead th', text: 'Food') | ||
expect(page).to have_selector('thead th', text: 'Measurement unit') | ||
expect(page).to have_selector('thead th', text: 'Unit Price') | ||
expect(page).to have_selector('thead th', text: 'Quantity') | ||
expect(page).to have_selector('thead th', text: 'Actions') | ||
|
||
it 'renders a list of foods' do | ||
render | ||
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td' | ||
assert_select cell_selector, text: Regexp.new('Name'.to_s), count: 2 | ||
assert_select cell_selector, text: Regexp.new('Measurement Unit'.to_s), count: 2 | ||
assert_select cell_selector, text: Regexp.new('9.99'.to_s), count: 2 | ||
assert_select cell_selector, text: Regexp.new(2.to_s), count: 2 | ||
assert_select cell_selector, text: Regexp.new(nil.to_s), count: 2 | ||
expect(page).to have_selector('tbody tr', count: 2) # Assuming there are two food items created | ||
expect(page).to have_content('Pizza') | ||
expect(page).to have_content('Salad') | ||
end | ||
end | ||
end | ||
end |