Skip to content

Commit

Permalink
Add more specs for RecordDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Mar 20, 2024
1 parent 238bdb9 commit 7982391
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
55 changes: 51 additions & 4 deletions spec/marten-turbo/handlers/record_delete_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe MartenTurbo::Handlers::RecordCreate do
method: "GET",
resource: "",
headers: HTTP::Headers{
"Accept" => "text/html",
"Host" => "example.com",
"Accept" => "text/html",
"Host" => "example.com",
}
)
)
Expand All @@ -36,8 +36,8 @@ describe MartenTurbo::Handlers::RecordCreate do
method: "GET",
resource: "",
headers: HTTP::Headers{
"Accept" => "text/html",
"Host" => "example.com",
"Accept" => "text/html",
"Host" => "example.com",
}
)
)
Expand All @@ -49,6 +49,53 @@ describe MartenTurbo::Handlers::RecordCreate do
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 a 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 a delete turbo stream when a 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/vnd.turbo-stream.html",
"Host" => "example.com",
}
)
)
handler = MartenTurbo::Handlers::RecordDeleteSpec::TestHandler.new(request, params)

response = handler.post

response.should_not be_a Marten::HTTP::Response::Found
response.content.strip.should contain "<turbo-stream action=\"remove\" target=\"tag_#{tag_1.pk!}\"></turbo-stream>"
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/test_project/templates/tags/delete.turbo_stream.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<turbo-stream action="remove" target="{{ dom_id record }}"></turbo-stream>
<turbo-stream action="remove" target="tag_{{ record.pk }}"></turbo-stream>

0 comments on commit 7982391

Please sign in to comment.