@@ -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 = % {
@@ -1607,13 +1607,78 @@ defmodule PolymorphicEmbedTest do
16071607 )
16081608 |> Floki . parse_fragment! ( )
16091609
1610- assert [ input ] = Floki . find ( html , "#reminder_channel___type__" )
1610+ assert [ input ] = Floki . find ( html , "#reminder_channel_my_type_field" )
1611+ assert Floki . attribute ( input , "name" ) == [ "reminder[channel][my_type_field]" ]
16111612 assert Floki . attribute ( input , "type" ) == [ "hidden" ]
16121613 assert Floki . attribute ( input , "value" ) == [ "email" ]
16131614
16141615 assert [ input ] = Floki . find ( html , "#reminder_channel_number" )
16151616 assert Floki . attribute ( input , "type" ) == [ "text" ]
16161617 end
1618+
1619+ test "generates forms that can be rendered (custom type field)" do
1620+ reminder_module = get_module ( Reminder , :polymorphic )
1621+
1622+ attrs = % {
1623+ date: ~U[ 2020-05-28 02:57:19Z] ,
1624+ text: "This is an Email reminder" ,
1625+ channel3: % {
1626+ my_type_field: "email"
1627+ }
1628+ }
1629+
1630+ changeset =
1631+ reminder_module
1632+ |> struct ( )
1633+ |> reminder_module . changeset ( attrs )
1634+
1635+ html =
1636+ render_component (
1637+ & liveview_form / 1 ,
1638+ % { changeset: changeset , field: :channel3 }
1639+ )
1640+ |> Floki . parse_fragment! ( )
1641+
1642+ assert [ input ] = Floki . find ( html , "#reminder_channel3_my_type_field" )
1643+ assert Floki . attribute ( input , "name" ) == [ "reminder[channel3][my_type_field]" ]
1644+ assert Floki . attribute ( input , "type" ) == [ "hidden" ]
1645+ assert Floki . attribute ( input , "value" ) == [ "email" ]
1646+ end
1647+
1648+ test "generates forms that can be rendered (default type field)" do
1649+ reminder_module = get_module ( Reminder , :polymorphic )
1650+
1651+ attrs = % {
1652+ date: ~U[ 2020-05-28 02:57:19Z] ,
1653+ text: "This is an Email reminder" ,
1654+ channel2: % {
1655+ __type__: "email" ,
1656+ address: "a" ,
1657+ valid: true ,
1658+ confirmed: true
1659+ }
1660+ }
1661+
1662+ changeset =
1663+ reminder_module
1664+ |> struct ( )
1665+ |> reminder_module . changeset ( attrs )
1666+
1667+ html =
1668+ render_component (
1669+ & liveview_form / 1 ,
1670+ % { changeset: changeset , field: :channel2 }
1671+ )
1672+ |> Floki . parse_fragment! ( )
1673+
1674+ assert [ input ] = Floki . find ( html , "#reminder_channel2___type__" )
1675+ assert Floki . attribute ( input , "name" ) == [ "reminder[channel2][__type__]" ]
1676+ assert Floki . attribute ( input , "type" ) == [ "hidden" ]
1677+ assert Floki . attribute ( input , "value" ) == [ "email" ]
1678+
1679+ assert [ input ] = Floki . find ( html , "#reminder_channel2_number" )
1680+ assert Floki . attribute ( input , "type" ) == [ "text" ]
1681+ end
16171682 end
16181683
16191684 test "inputs_for/4" do
@@ -1644,7 +1709,7 @@ defmodule PolymorphicEmbedTest do
16441709 expected_contents =
16451710 if ( polymorphic? ( generator ) ,
16461711 do:
1647- ~s( <input id="reminder_channel___type__ " name="reminder[channel][__type__ ]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">) ,
1712+ ~s( <input id="reminder_channel_my_type_field " name="reminder[channel][my_type_field ]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">) ,
16481713 else:
16491714 ~s( <input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
16501715 )
@@ -1665,7 +1730,7 @@ defmodule PolymorphicEmbedTest do
16651730 expected_contents =
16661731 if ( polymorphic? ( generator ) ,
16671732 do:
1668- ~s( <input id="reminder_channel___type__ " name="reminder[channel][__type__ ]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">) ,
1733+ ~s( <input id="reminder_channel_my_type_field " name="reminder[channel][my_type_field ]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">) ,
16691734 else:
16701735 ~s( <input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
16711736 )
@@ -1707,7 +1772,7 @@ defmodule PolymorphicEmbedTest do
17071772 expected_contents =
17081773 if ( polymorphic? ( generator ) ,
17091774 do:
1710- ~s( <input id="reminder_channel___type__ " name="reminder[channel][__type__ ]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">) ,
1775+ ~s( <input id="reminder_channel_my_type_field " name="reminder[channel][my_type_field ]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">) ,
17111776 else:
17121777 ~s( <input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
17131778 )
@@ -1728,7 +1793,7 @@ defmodule PolymorphicEmbedTest do
17281793 expected_contents =
17291794 if ( polymorphic? ( generator ) ,
17301795 do:
1731- ~s( <input id="reminder_channel___type__ " name="reminder[channel][__type__ ]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">) ,
1796+ ~s( <input id="reminder_channel_my_type_field " name="reminder[channel][my_type_field ]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">) ,
17321797 else:
17331798 ~s( <input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
17341799 )
0 commit comments