2024-06-20 08:15:09 -04:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class CategoriesControllerTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
sign_in users(:family_admin)
|
2024-07-10 11:22:59 -04:00
|
|
|
@transaction = account_transactions :one
|
2024-06-20 08:15:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "index" do
|
|
|
|
get categories_url
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "new" do
|
|
|
|
get new_category_url
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create" do
|
|
|
|
color = Category::COLORS.sample
|
|
|
|
|
|
|
|
assert_difference "Category.count", +1 do
|
|
|
|
post categories_url, params: {
|
|
|
|
category: {
|
|
|
|
name: "New Category",
|
|
|
|
color: color } }
|
|
|
|
end
|
|
|
|
|
|
|
|
new_category = Category.order(:created_at).last
|
|
|
|
|
2024-12-20 11:37:26 -05:00
|
|
|
assert_redirected_to categories_url
|
2024-06-20 08:15:09 -04:00
|
|
|
assert_equal "New Category", new_category.name
|
|
|
|
assert_equal color, new_category.color
|
|
|
|
end
|
|
|
|
|
2024-12-30 16:04:38 +01:00
|
|
|
test "create fails if name is not unique" do
|
|
|
|
assert_no_difference "Category.count" do
|
|
|
|
post categories_url, params: {
|
|
|
|
category: {
|
|
|
|
name: categories(:food_and_drink).name,
|
|
|
|
color: Category::COLORS.sample } }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_response :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2024-06-20 08:15:09 -04:00
|
|
|
test "create and assign to transaction" do
|
|
|
|
color = Category::COLORS.sample
|
|
|
|
|
|
|
|
assert_difference "Category.count", +1 do
|
|
|
|
post categories_url, params: {
|
2024-07-10 11:22:59 -04:00
|
|
|
transaction_id: @transaction.id,
|
2024-06-20 08:15:09 -04:00
|
|
|
category: {
|
|
|
|
name: "New Category",
|
|
|
|
color: color } }
|
|
|
|
end
|
|
|
|
|
|
|
|
new_category = Category.order(:created_at).last
|
|
|
|
|
2024-12-20 11:37:26 -05:00
|
|
|
assert_redirected_to categories_url
|
2024-06-20 08:15:09 -04:00
|
|
|
assert_equal "New Category", new_category.name
|
|
|
|
assert_equal color, new_category.color
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_equal @transaction.reload.category, new_category
|
2024-06-20 08:15:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "edit" do
|
|
|
|
get edit_category_url(categories(:food_and_drink))
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update" do
|
|
|
|
new_color = Category::COLORS.without(categories(:income).color).sample
|
|
|
|
|
|
|
|
assert_changes -> { categories(:income).name }, to: "New Name" do
|
|
|
|
assert_changes -> { categories(:income).reload.color }, to: new_color do
|
|
|
|
patch category_url(categories(:income)), params: {
|
|
|
|
category: {
|
|
|
|
name: "New Name",
|
|
|
|
color: new_color } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-12-20 11:37:26 -05:00
|
|
|
assert_redirected_to categories_url
|
|
|
|
end
|
|
|
|
|
|
|
|
test "bootstrap" do
|
2025-01-16 14:36:37 -05:00
|
|
|
assert_difference "Category.count", 10 do
|
2024-12-20 11:37:26 -05:00
|
|
|
post bootstrap_categories_url
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_redirected_to categories_url
|
2024-06-20 08:15:09 -04:00
|
|
|
end
|
|
|
|
end
|