Skip to content

Commit 6d6cb78

Browse files
committed
add tests for rendering hidden inputs for custom type field
1 parent 27c2668 commit 6d6cb78

1 file changed

Lines changed: 69 additions & 2 deletions

File tree

test/polymorphic_embed_test.exs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ defmodule PolymorphicEmbedTest do
15821582
end
15831583

15841584
describe "polymorphic_embed_inputs_for/2" do
1585-
test "generates forms that can be rendered" do
1585+
test "generates forms that can be rendered (custom type field/identify_by_fields)" do
15861586
reminder_module = get_module(Reminder, :polymorphic)
15871587

15881588
attrs = %{
@@ -1606,14 +1606,81 @@ defmodule PolymorphicEmbedTest do
16061606
%{changeset: changeset, field: :channel}
16071607
)
16081608
|> Floki.parse_fragment!()
1609+
|> dbg()
16091610

1610-
assert [input] = Floki.find(html, "#reminder_channel___type__")
1611+
assert [input] = Floki.find(html, "#reminder_channel_my_type_field")
1612+
assert Floki.attribute(input, "name") == ["reminder[channel][my_type_field]"]
16111613
assert Floki.attribute(input, "type") == ["hidden"]
16121614
assert Floki.attribute(input, "value") == ["email"]
16131615

16141616
assert [input] = Floki.find(html, "#reminder_channel_number")
16151617
assert Floki.attribute(input, "type") == ["text"]
16161618
end
1619+
1620+
test "generates forms that can be rendered (custom type field)" do
1621+
reminder_module = get_module(Reminder, :polymorphic)
1622+
1623+
attrs = %{
1624+
date: ~U[2020-05-28 02:57:19Z],
1625+
text: "This is an Email reminder",
1626+
channel3: %{
1627+
my_type_field: "email"
1628+
}
1629+
}
1630+
1631+
changeset =
1632+
reminder_module
1633+
|> struct()
1634+
|> reminder_module.changeset(attrs)
1635+
1636+
html =
1637+
render_component(
1638+
&liveview_form/1,
1639+
%{changeset: changeset, field: :channel3}
1640+
)
1641+
|> Floki.parse_fragment!()
1642+
|> dbg()
1643+
1644+
assert [input] = Floki.find(html, "#reminder_channel3_my_type_field")
1645+
assert Floki.attribute(input, "name") == ["reminder[channel3][my_type_field]"]
1646+
assert Floki.attribute(input, "type") == ["hidden"]
1647+
assert Floki.attribute(input, "value") == ["email"]
1648+
end
1649+
1650+
test "generates forms that can be rendered (default type field)" do
1651+
reminder_module = get_module(Reminder, :polymorphic)
1652+
1653+
attrs = %{
1654+
date: ~U[2020-05-28 02:57:19Z],
1655+
text: "This is an Email reminder",
1656+
channel2: %{
1657+
__type__: "email",
1658+
address: "a",
1659+
valid: true,
1660+
confirmed: true
1661+
}
1662+
}
1663+
1664+
changeset =
1665+
reminder_module
1666+
|> struct()
1667+
|> reminder_module.changeset(attrs)
1668+
1669+
html =
1670+
render_component(
1671+
&liveview_form/1,
1672+
%{changeset: changeset, field: :channel2}
1673+
)
1674+
|> Floki.parse_fragment!()
1675+
1676+
assert [input] = Floki.find(html, "#reminder_channel2___type__")
1677+
assert Floki.attribute(input, "name") == ["reminder[channel2][__type__]"]
1678+
assert Floki.attribute(input, "type") == ["hidden"]
1679+
assert Floki.attribute(input, "value") == ["email"]
1680+
1681+
assert [input] = Floki.find(html, "#reminder_channel2_number")
1682+
assert Floki.attribute(input, "type") == ["text"]
1683+
end
16171684
end
16181685

16191686
test "inputs_for/4" do

0 commit comments

Comments
 (0)