From 8121adf636d36f9663028f677d490dc1f0956c3e Mon Sep 17 00:00:00 2001 From: lukaszreszke Date: Wed, 25 Sep 2024 07:21:29 +0200 Subject: [PATCH] Add basic test for supplies Before changing this part of the code it is good to know that we are not going to break anything. In order to make testing easier I decided to add `id` of product to each table row --- .../app/views/products/index.html.erb | 2 +- .../test/integration/supplies_test.rb | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 rails_application/test/integration/supplies_test.rb diff --git a/rails_application/app/views/products/index.html.erb b/rails_application/app/views/products/index.html.erb index dfa14fea..65f3ec27 100644 --- a/rails_application/app/views/products/index.html.erb +++ b/rails_application/app/views/products/index.html.erb @@ -20,7 +20,7 @@ <% @products.each do |product| %> - + > <%= link_to product.name, product_path(product), class: "hover:underline text-blue-500" %> <%= number_to_currency product.price %> <%= product.vat_rate %> diff --git a/rails_application/test/integration/supplies_test.rb b/rails_application/test/integration/supplies_test.rb new file mode 100644 index 00000000..5ab6fd68 --- /dev/null +++ b/rails_application/test/integration/supplies_test.rb @@ -0,0 +1,50 @@ +require "test_helper" + +class SuppliesTest < InMemoryRESIntegrationTestCase + def setup + super + end + + def test_supply_new_product + create_product + product = Product.find_by(sku:) + increase_stock_level_by_10(product.id) + + follow_redirect! + + assert_select "tr#product_#{product.id}" do + assert_select "td", "10" + end + assert_equal(10, product.reload.stock_level) + end + + def test_supply_product_with_existing_stock + create_product + product = Product.find_by(sku:) + increase_stock_level_by_10(product.id) + + assert_changes -> { Product.find(product.id).stock_level }, from: 10, to: 20 do + increase_stock_level_by_10(product.id) + end + + follow_redirect! + + assert_select "tr#product_#{product.id}" do + assert_select "td", "20" + end + end + + private + + def increase_stock_level_by_10(product_id) + post "/products/#{product_id}/supplies", params: { product_id: product_id, quantity: 10 } + end + + def create_product + post "/products", params: { product: { name: "Stanley Cup", price: 100, vat_rate: 23, sku: } } + end + + def sku + "SKU-ST4NL3Y" + end +end