Skip to content

Commit 8d77734

Browse files
committed
adding spec for #304
1 parent 010d66a commit 8d77734

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

spec/usage/components/form_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,95 @@ def page4
486486

487487
end
488488

489+
describe "Example 6 - Async submit update request with success, which does not rest the input fields" do
490+
# https://github.com/basemate/matestack-ui-core/issues/304
491+
492+
# This example uses the `TestModel` with attributes `title` and `description`
493+
# defined in `spec/dummy/app/models/test_model.rb`.
494+
495+
before do
496+
class Pages::TestModelPage < Matestack::Ui::Page
497+
def response
498+
components {
499+
form form_config, :include do
500+
form_input id: 'title', key: :title, type: :text
501+
form_input id: 'description', key: :description, type: :text
502+
form_submit { button text: "Save" }
503+
end
504+
async show_on: "form_has_errors", hide_after: 5000 do
505+
plain "Form has errors"
506+
end
507+
async show_on: "update_successful", hide_after: 5000 do
508+
plain "Update successful"
509+
end
510+
}
511+
end
512+
513+
private
514+
515+
def form_config
516+
{
517+
for: @test_model,
518+
method: :put,
519+
path: "/test_models/#{@test_model.id}",
520+
success: { emit: "update_successful" },
521+
failure: { emit: "form_has_errors" }
522+
}
523+
end
524+
end
525+
526+
class TestModelsController < ApplicationController
527+
include Matestack::Ui::Core::ApplicationHelper
528+
529+
def show
530+
@test_model = TestModel.find params[:id]
531+
responder_for Pages::TestModelPage
532+
end
533+
534+
def update
535+
@test_model = TestModel.find params[:id]
536+
@test_model.update test_model_params
537+
if @test_model.errors.any?
538+
render json: {
539+
errors: user.errors
540+
}, status: :unproccessable_entity
541+
else
542+
render json: {}, status: :ok
543+
end
544+
end
545+
546+
protected
547+
548+
def test_model_params
549+
params.require(:test_model).permit(:title, :description)
550+
end
551+
end
552+
553+
Rails.application.routes.draw do
554+
resources :test_models
555+
end
556+
end
557+
558+
after do
559+
Rails.application.reload_routes!
560+
end
561+
562+
specify do
563+
test_model = TestModel.create title: "Foo", description: "This is a very nice foo!"
564+
565+
visit Rails.application.routes.url_helpers.test_model_path(test_model)
566+
expect(find_field(:title).value).to eq "Foo"
567+
568+
fill_in :title, with: "Bar"
569+
fill_in :description, with: "This is a equally nice bar!"
570+
click_on "Save"
571+
572+
expect(page).to have_text "Update successful"
573+
expect(find_field(:title).value).to eq "Bar"
574+
expect(find_field(:description).value).to eq "This is a equally nice bar!"
575+
end
576+
end
577+
489578
describe "Form Input Component" do
490579

491580
it "Example 1 - Supports 'text', 'password', 'number', 'email', 'textarea' type" do

0 commit comments

Comments
 (0)