Skip to content

Commit

Permalink
Show error messages
Browse files Browse the repository at this point in the history
It's kinda hard to test reflexes..
  • Loading branch information
dacook committed Jul 28, 2023
1 parent 2c1fae9 commit 5775aff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/reflexes/products_reflex.rb
Expand Up @@ -40,10 +40,10 @@ def bulk_update
if product_set.save
# flash[:success] = I18n.t('.success') # todo
elsif product_set.errors.present?
# flash there are errors..
@error_msg = product_set.errors.full_messages.to_sentence
end

render_products_form(product_set)
render_products_form
end

private
Expand Down Expand Up @@ -84,11 +84,11 @@ def render_products
morph :nothing
end

def render_products_form(product_set)
def render_products_form
cable_ready.replace(
selector: "#products-form",
html: render(partial: "admin/products_v3/table",
locals: { products: @products, errors: product_set.errors })
locals: { products: @products, error_msg: @error_msg })
).broadcast
morph :nothing

Expand Down
6 changes: 5 additions & 1 deletion app/views/admin/products_v3/_table.html.haml
@@ -1,11 +1,15 @@
= form_with url: bulk_update_admin_products_v3_index_path, method: :patch,
= form_with url: bulk_update_admin_products_v3_index_path, method: :patch, id: "products-form",

html: {'data-reflex-serialize-form': true, 'data-reflex': 'submit->products#bulk_update'} do |form|
.container
.sixteen.columns.align-right
#fieldset
= t('.products_modified', count: 'X')
= form.submit t('.reset'), type: :reset
= form.submit t('.save')
- if defined?(error_msg) && error_msg.present?
.error
= error_msg
%table.products
%col{ width:"15%" }
%col{ width:"5%", style: "max-width:5em" }
Expand Down
35 changes: 32 additions & 3 deletions spec/reflexes/products_reflex_spec.rb
Expand Up @@ -49,7 +49,7 @@
}

expect{
reflex(:bulk_update, params:)
run_reflex(:bulk_update, params:)
product_a.reload
}.to change(product_a, :name).to("Pommes")
end
Expand All @@ -68,7 +68,7 @@
]
}
}
subject{ reflex(:bulk_update, params:) }
subject{ run_reflex(:bulk_update, params:) }

it "Should retain sort order, even when names change" do
expect(subject.get(:products).map(&:id)).to eq [
Expand All @@ -77,13 +77,42 @@
]
end
end

describe "error messages" do
it "summarises duplicate error messages" do
params = {
"products" => [
{
"id" => product_a.id.to_s,
"name" => "",
},
{
"id" => product_b.id.to_s,
"name" => "",
},
]
}

reflex = run_reflex(:bulk_update, params:)
expect(reflex.get(:error_msg)).to eq "Product Name can't be blank"

# # WTF
# expect{ reflex(:bulk_update, params:) }.to broadcast(
# replace: {
# selector: "#products-form",
# html: /Product Name can't be blank/,
# },
# broadcast: nil
# )
end
end
end
end

# Build and run a reflex using the context
# Parameters can be added with params: option
# For more options see https://github.com/podia/stimulus_reflex_testing#usage
def reflex(method_name, opts = {})
def run_reflex(method_name, opts = {})
build_reflex(method_name:, **context.merge(opts)).tap{ |reflex|
reflex.run(method_name)
}
Expand Down

0 comments on commit 5775aff

Please sign in to comment.