Skip to content

Commit 5db99e3

Browse files
committed
add tests
1 parent 15d0652 commit 5db99e3

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

test/polymorphic_embed_test.exs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,6 +3204,97 @@ defmodule PolymorphicEmbedTest do
32043204
end)
32053205
end
32063206

3207+
test "form with improved param handling for different param types" do
3208+
reminder_module = get_module(Reminder, :polymorphic)
3209+
3210+
# Test with nil params - when contexts is empty, safe_inputs_for returns empty string
3211+
changeset_nil_params =
3212+
struct(reminder_module)
3213+
|> reminder_module.changeset(%{text: "Test reminder", contexts: []})
3214+
|> Map.put(:params, %{"contexts" => nil})
3215+
|> Map.put(:action, :insert)
3216+
3217+
safe_form_for(changeset_nil_params, fn _f ->
3218+
safe_inputs_for(changeset_nil_params, :contexts, :polymorphic, fn f ->
3219+
assert f.impl == Phoenix.HTML.FormData.Ecto.Changeset
3220+
assert f.errors == []
3221+
assert f.params == %{}
3222+
3223+
1
3224+
end)
3225+
3226+
1
3227+
end)
3228+
3229+
# Test with list params - need to add some contexts to the data
3230+
changeset_list_params =
3231+
struct(reminder_module)
3232+
|> reminder_module.changeset(%{
3233+
text: "Test reminder",
3234+
contexts: [
3235+
%{__type__: "device", ref: "123", type: "cellphone"},
3236+
%{__type__: "location", address: "456 Main St"}
3237+
]
3238+
})
3239+
|> Map.put(:params, %{"contexts" => [%{"ref" => "123"}, %{"address" => "456 Main St"}]})
3240+
|> Map.put(:action, :insert)
3241+
3242+
safe_form_for(changeset_list_params, fn _f ->
3243+
safe_inputs_for(changeset_list_params, :contexts, :polymorphic, fn f ->
3244+
assert f.impl == Phoenix.HTML.FormData.Ecto.Changeset
3245+
assert f.errors == []
3246+
# First context should have params from index 0
3247+
if f.index == 0 do
3248+
assert f.params == %{"ref" => "123"}
3249+
end
3250+
3251+
# Second context should have params from index 1
3252+
if f.index == 1 do
3253+
assert f.params == %{"address" => "456 Main St"}
3254+
end
3255+
3256+
1
3257+
end)
3258+
3259+
1
3260+
end)
3261+
3262+
# Test with map params - need to add some contexts to the data
3263+
changeset_map_params =
3264+
struct(reminder_module)
3265+
|> reminder_module.changeset(%{
3266+
text: "Test reminder",
3267+
contexts: [
3268+
%{__type__: "device", ref: "789", type: "cellphone"},
3269+
%{__type__: "location", address: "012 Oak Ave"}
3270+
]
3271+
})
3272+
|> Map.put(:params, %{
3273+
"contexts" => %{"0" => %{"ref" => "789"}, "1" => %{"address" => "012 Oak Ave"}}
3274+
})
3275+
|> Map.put(:action, :insert)
3276+
3277+
safe_form_for(changeset_map_params, fn _f ->
3278+
safe_inputs_for(changeset_map_params, :contexts, :polymorphic, fn f ->
3279+
assert f.impl == Phoenix.HTML.FormData.Ecto.Changeset
3280+
assert f.errors == []
3281+
# First context should have params from key "0"
3282+
if f.index == 0 do
3283+
assert f.params == %{"ref" => "789"}
3284+
end
3285+
3286+
# Second context should have params from key "1"
3287+
if f.index == 1 do
3288+
assert f.params == %{"address" => "012 Oak Ave"}
3289+
end
3290+
3291+
1
3292+
end)
3293+
3294+
1
3295+
end)
3296+
end
3297+
32073298
describe "get_polymorphic_type/3" do
32083299
test "returns the type for a module" do
32093300
assert PolymorphicEmbed.get_polymorphic_type(

0 commit comments

Comments
 (0)