Skip to content

Commit

Permalink
Add specs for RecordDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Mar 20, 2024
1 parent 5d3a971 commit 238bdb9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
7 changes: 6 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ dependencies:
marten:
github: martenframework/marten
sqlite3:
github: crystal-lang/crystal-sqlite3
github: crystal-lang/crystal-sqlite3

development_dependencies:
ameba:
github: crystal-ameba/ameba
branch: master
62 changes: 61 additions & 1 deletion spec/marten-turbo/handlers/record_delete_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,67 @@ require "./spec_helper"

describe MartenTurbo::Handlers::RecordCreate do
describe "#post" do
it "" do
it "deletes the record and returns the expected redirect when no turbo request was made and no template is given" do
tag_1 = Tag.create!(name: "Tag 1")
tag_2 = Tag.create!(name: "Tag 2")

params = Marten::Routing::MatchParameters{"pk" => tag_1.pk!}
request = Marten::HTTP::Request.new(
::HTTP::Request.new(
method: "GET",
resource: "",
headers: HTTP::Headers{
"Accept" => "text/html",
"Host" => "example.com",
}
)
)
handler = MartenTurbo::Handlers::RecordDeleteSpec::TestWithoutTemplateHandler.new(request, params)

response = handler.post

response.should be_a Marten::HTTP::Response::Found
Tag.get(pk: tag_1.pk).should be_nil
Tag.get(pk: tag_2.pk).should eq tag_2
end

it "deletes the record and returns the expected redirect when no turbo request was made and a template is given" do
tag_1 = Tag.create!(name: "Tag 1")
tag_2 = Tag.create!(name: "Tag 2")

params = Marten::Routing::MatchParameters{"pk" => tag_1.pk!}
request = Marten::HTTP::Request.new(
::HTTP::Request.new(
method: "GET",
resource: "",
headers: HTTP::Headers{
"Accept" => "text/html",
"Host" => "example.com",
}
)
)
handler = MartenTurbo::Handlers::RecordDeleteSpec::TestHandler.new(request, params)

response = handler.post

response.should be_a Marten::HTTP::Response::Found
Tag.get(pk: tag_1.pk).should be_nil
Tag.get(pk: tag_2.pk).should eq tag_2
end
end
end

module MartenTurbo::Handlers::RecordDeleteSpec
class TestHandler < MartenTurbo::Handlers::RecordDelete
model Tag
success_route_name "dummy"
template_name "tags/delete.html"
turbo_stream_name "tags/delete.turbo_stream.html"
end

class TestWithoutTemplateHandler < MartenTurbo::Handlers::RecordDelete
model Tag
success_route_name "dummy"
template_name "tags/delete.html"
end
end
Empty file.
1 change: 1 addition & 0 deletions spec/test_project/templates/tags/delete.turbo_stream.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<turbo-stream action="remove" target="{{ dom_id record }}"></turbo-stream>
2 changes: 1 addition & 1 deletion src/marten_turbo/handlers/record_delete.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module MartenTurbo
if request.turbo? && turbo_stream_name
render_turbo_stream context
else
HTTP::Response::Found.new(success_url)
Marten::HTTP::Response::Found.new(success_url)
end
end

Expand Down

0 comments on commit 238bdb9

Please sign in to comment.