Skip to content

Commit

Permalink
Add basic test for supplies
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lukaszreszke committed Sep 26, 2024
1 parent 00c65f2 commit 8121adf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rails_application/app/views/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<tbody>
<% @products.each do |product| %>
<tr class="border-t">
<tr class="border-t" id=<%= dom_id(product) %>>
<td class="py-2"><%= link_to product.name, product_path(product), class: "hover:underline text-blue-500" %></td>
<td class="py-2"><%= number_to_currency product.price %></td>
<td class="py-2"><%= product.vat_rate %></td>
Expand Down
50 changes: 50 additions & 0 deletions rails_application/test/integration/supplies_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8121adf

Please sign in to comment.