Skip to content

Commit

Permalink
Tests: Add 'food#index view' integration-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ITurres committed Dec 19, 2023
1 parent c13d5d0 commit 5564d15
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions spec/views/foods/index.html.erb_spec.rb
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

0 comments on commit 5564d15

Please sign in to comment.