From 15395f3b68c06e90ea8fc531cdf1ae0fc1aa7915 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Sun, 25 Aug 2024 14:20:39 -0400 Subject: [PATCH 1/2] Renamed file and removed old commented out tests --- test/live_view_native/component_test.exs | 25 +++++++ test/live_view_native/components_test.exs | 80 ----------------------- 2 files changed, 25 insertions(+), 80 deletions(-) create mode 100644 test/live_view_native/component_test.exs delete mode 100644 test/live_view_native/components_test.exs diff --git a/test/live_view_native/component_test.exs b/test/live_view_native/component_test.exs new file mode 100644 index 00000000..9be68d2f --- /dev/null +++ b/test/live_view_native/component_test.exs @@ -0,0 +1,25 @@ +defmodule LiveViewNativeTest.ComponentTest do + use ExUnit.Case + import LiveViewNativeTest.Utils, only: [render: 1] + + alias Phoenix.LiveView.Socket + alias LiveViewNativeTest.{HomeLive} + + setup do + {:ok, socket: %Socket{private: %{connect_info: %{params: %{"target" => "classic"}}}}} + end + + describe "embed_templates" do + test "will compile templates into render/2 functions", %{socket: socket} do + assert render(HomeLive.home_live(%{foo: :bar, socket: socket}, %{})) =~ "Hello, GameBoy! bar" + + assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket}, %{})) =~ "Hello, Other GameBoy! bar" + end + + test "will compile templates into render/1 functions for use with `render_with`", %{socket: socket} do + assert render(HomeLive.home_live(%{foo: :bar, socket: socket})) =~ "Hello, GameBoy! bar" + + assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket})) =~ "Hello, Other GameBoy! bar" + end + end +end diff --git a/test/live_view_native/components_test.exs b/test/live_view_native/components_test.exs deleted file mode 100644 index ccf6a640..00000000 --- a/test/live_view_native/components_test.exs +++ /dev/null @@ -1,80 +0,0 @@ -defmodule LiveViewNativeTest.ComponentsTest do - use ExUnit.Case - import LiveViewNativeTest.Utils, only: [render: 1] - - alias Phoenix.LiveView.Socket - alias LiveViewNativeTest.{HomeLive} - - setup do - {:ok, socket: %Socket{private: %{connect_info: %{params: %{"target" => "classic"}}}}} - end - - describe "embed_templates" do - test "will compile templates into render/2 functions", %{socket: socket} do - assert render(HomeLive.home_live(%{foo: :bar, socket: socket}, %{})) =~ "Hello, GameBoy! bar" - - assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket}, %{})) =~ "Hello, Other GameBoy! bar" - end - - test "will compile templates into render/1 functions for use with `render_with`", %{socket: socket} do - assert render(HomeLive.home_live(%{foo: :bar, socket: socket})) =~ "Hello, GameBoy! bar" - - assert render(HomeLive.home_live_other(%{foo: :bar, socket: socket})) =~ "Hello, Other GameBoy! bar" - end - end - - # import Phoenix.LiveViewTest - # alias LiveViewNative.TestComponents - - # defp one(fragment, selector) do - # [element | _tail] = Floki.find(fragment, selector) - - # element - # end - - # test "test_component/1 renders as expected" do - # html_context = LiveViewNativePlatform.Kit.compile(%LiveViewNative.Platforms.HTML{}) - # test_context = LiveViewNativePlatform.Kit.compile(%LiveViewNative.TestPlatform{}) - - # html_result = - # render_component(&TestComponents.test_component/1, format: :html, native: html_context) - # |> Floki.parse_document!() - - # test_result = - # render_component(&TestComponents.test_component/1, - # format: :lvntest, - # native: test_context - # ) - # |> Floki.parse_document!() - - # local_component_result = one(test_result, "#local-component-test") - # remote_component_result = one(test_result, "#remote-component-test") - # imported_component_result = one(test_result, "#imported-component-test") - - # component_with_inner_block_result_content = - # one(test_result, "#component-with-inner-block-test #content") - - # component_with_inner_block_result_inner_block = - # one(test_result, "#component-with-inner-block-test #inner-block-test") - - # component_with_slot_result_content = - # one(test_result, "#component-with-slot-test #content") - - # component_with_slot_result_slot = - # one(test_result, "#component-with-slot-test #slot-test") - - # html_element_result = one(html_result, "#html-element-test") - - # assert Floki.text(local_component_result) == "Local Component Rendered" - # assert Floki.text(remote_component_result) == "Remote Component Rendered" - # assert Floki.text(imported_component_result) == "Imported Component Rendered" - - # assert Floki.text(component_with_inner_block_result_content) == - # "Component With Inner Block Rendered" - - # assert Floki.text(component_with_inner_block_result_inner_block) == "Inner Block Rendered" - # assert Floki.text(component_with_slot_result_content) == "Component With Slot Rendered" - # assert Floki.text(component_with_slot_result_slot) == "Slot Rendered" - # assert Floki.text(html_element_result) == "HTML Element Rendered" - # end -end From ce2c89ce51abad0d3262c043a14b70f82ca3aa75 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Fri, 30 Aug 2024 15:00:12 -0400 Subject: [PATCH 2/2] Interface normalizer Added private API for normalizing parts of the interface This is helpful for values that are strings but are better suited for version matching in another type --- lib/live_view_native/content_negotiator.ex | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/live_view_native/content_negotiator.ex b/lib/live_view_native/content_negotiator.ex index 14fe43a9..0cb3ce81 100644 --- a/lib/live_view_native/content_negotiator.ex +++ b/lib/live_view_native/content_negotiator.ex @@ -49,9 +49,11 @@ defmodule LiveViewNative.ContentNegotiator do |> assign_params(params) end - defp assign_params(%{assigns: %{:_format => _format}} = socket, %{"_interface" => interface} = params) do + defp assign_params(%{assigns: %{:_format => format}} = socket, %{"_interface" => interface} = params) do params = Map.delete(params, "_interface") + interface = normalize_interface(interface, format) + socket |> assign(:_interface, interface) |> assign_params(params) @@ -72,4 +74,39 @@ defmodule LiveViewNative.ContentNegotiator do layouts -> layouts[format] end end + + defp normalize_interface(interface, format) do + plugin = + format + |> LiveViewNative.fetch_plugin!() + |> Map.get(:__struct__) + + interface + |> normalize_os_version(plugin) + |> normalize_app_version(plugin) + end + + defp normalize_os_version(%{"os_version" => os_version} = interface, plugin) when not is_nil(os_version) do + if Module.defines?(plugin, {:normalize_os_version, 1}) do + os_version = plugin.normalize_os_version(os_version) + Map.put(interface, "os_version", os_version) + else + interface + end + end + + defp normalize_os_version(interface, _plugin), + do: interface + + defp normalize_app_version(%{"app_version" => app_version} = interface, plugin) when not is_nil(app_version) do + if Module.defines?(plugin, {:normalize_app_version, 1}) do + app_version = plugin.normalize_app_version(app_version) + Map.put(interface, "app_version", app_version) + else + interface + end + end + + defp normalize_app_version(interface, _plugin), + do: interface end