From 1141e4d4c5dc6bde40610c2b5b3d1c2f99b8f341 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Tue, 16 Jan 2024 03:56:19 -0500 Subject: [PATCH] Fixed test suite --- config/test.exs | 2 +- lib/live_view_native/renderer.ex | 8 +-- lib/live_view_native/utils.ex | 6 +- mix.exs | 9 ++- .../integrations/inline_test.exs | 10 ++-- .../integrations/template_test.exs | 4 +- ...n_plug_test.exs => live_reloader_test.exs} | 17 ++---- test/live_view_native/live_session_test.exs | 59 ------------------- test/live_view_native_test.exs | 4 -- test/support/live/inline_live.ex | 4 +- 10 files changed, 26 insertions(+), 97 deletions(-) rename test/live_view_native/{session_plug_test.exs => live_reloader_test.exs} (51%) delete mode 100644 test/live_view_native/live_session_test.exs delete mode 100644 test/live_view_native_test.exs diff --git a/config/test.exs b/config/test.exs index 762086e2..bab206b3 100644 --- a/config/test.exs +++ b/config/test.exs @@ -27,4 +27,4 @@ config :live_view_native_test, %{path: "/template", module: LiveViewNativeTest.TemplateLive} ] -config :phoenix, :plug_init_mode, :runtime \ No newline at end of file +config :phoenix, :plug_init_mode, :runtime diff --git a/lib/live_view_native/renderer.ex b/lib/live_view_native/renderer.ex index bb2fe99e..6a342032 100644 --- a/lib/live_view_native/renderer.ex +++ b/lib/live_view_native/renderer.ex @@ -28,8 +28,8 @@ defmodule LiveViewNative.Renderer do else quote do def unquote(name)(var!(assigns)) do - target = LiveViewNative.Utils.get_target(var!(assigns)) - apply(__MODULE__, unquote(name), [var!(assigns), %{target: target}]) + interface = LiveViewNative.Utils.get_interface(var!(assigns)) + apply(__MODULE__, unquote(name), [var!(assigns), interface]) end end end @@ -123,7 +123,7 @@ defmodule LiveViewNative.Renderer do quote do @file unquote(template) @external_resource unquote(template) - def unquote(name)(var!(assigns), _) do + def unquote(name)(var!(assigns), _interface) do unquote(ast) end end @@ -132,7 +132,7 @@ defmodule LiveViewNative.Renderer do quote do @file unquote(template) @external_resource unquote(template) - def unquote(name)(var!(assigns), %{target: unquote(target)}) do + def unquote(name)(var!(assigns), %{"target" => unquote(target)}) do unquote(ast) end end diff --git a/lib/live_view_native/utils.ex b/lib/live_view_native/utils.ex index 05195a49..e8234a9b 100644 --- a/lib/live_view_native/utils.ex +++ b/lib/live_view_native/utils.ex @@ -5,9 +5,9 @@ defmodule LiveViewNative.Utils do def get_format(%Socket{private: %{connect_params: %{"_format" => format}}}), do: format def get_format(_socket), do: "html" - def get_target(%{socket: %Socket{private: %{connect_info: %{params: %{"target" => target}}}}}), do: target - def get_target(%{conn: %Plug.Conn{params: %{"target" => target}}}), do: target - def get_target(_socket), do: nil + def get_interface(%{socket: %Socket{private: %{connect_info: %{params: %{"_interface" => interface}}}}}), do: interface + def get_interface(%{conn: %Plug.Conn{params: %{"_interface" => interface}}}), do: interface + def get_interface(_socket), do: %{} def stringify_format(format) when is_binary(format), do: format def stringify_format(format) when is_atom(format), do: Atom.to_string(format) diff --git a/mix.exs b/mix.exs index 5d53666c..92551c54 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule LiveViewNative.MixProject do use Mix.Project - @version "0.2.0" + @version "0.3.0-alpha.1" def project do [ @@ -16,7 +16,6 @@ defmodule LiveViewNative.MixProject do aliases: aliases(), deps: deps(), test_coverage: [tool: ExCoveralls], - consolidate_protocols: Mix.env() != :test, docs: docs() ] end @@ -53,8 +52,8 @@ defmodule LiveViewNative.MixProject do {:makeup_eex, ">= 0.1.1", only: :dev, runtime: false}, {:owl, "~> 0.8", runtime: false}, {:deep_merge, "~> 1.0"}, - # {:live_view_native_test, path: "../live_view_native_test", only: :test} - {:live_view_native_test, github: "liveview-native/live_view_native_test", only: :test} + {:live_view_native_test, path: "../live_view_native_test", only: :test} + # {:live_view_native_test, github: "liveview-native/live_view_native_test", only: :test} ] end @@ -112,7 +111,7 @@ defmodule LiveViewNative.MixProject do # Hex package configuration defp package do %{ - maintainers: ["May Matyi"], + maintainers: ["Brian Cardarella"], licenses: ["MIT"], links: %{ "GitHub" => @source_url diff --git a/test/live_view_native/integrations/inline_test.exs b/test/live_view_native/integrations/inline_test.exs index fbf33054..1986fce4 100644 --- a/test/live_view_native/integrations/inline_test.exs +++ b/test/live_view_native/integrations/inline_test.exs @@ -12,35 +12,35 @@ defmodule LiveViewNative.InlineRenderTest do end test "can render the fallback html inline render", %{conn: conn} do - {:ok, lv, _html} = live(conn, "/inline") + {:ok, lv, _body} = live(conn, "/inline") assert lv |> element("#inline") |> render() =~ "original inline HTML works" end test "can render the gameboy format", %{conn: conn} do conn = put_req_header(conn, "accept", "text/gameboy") - {:ok, lv, _html} = live(conn, "/inline") + {:ok, lv, _body} = live(conn, "/inline") assert lv |> element("gameboy") |> render() =~ "Inline GameBoy Render 100" end test "can render the gameboy format with tv target", %{conn: conn} do conn = put_req_header(conn, "accept", "text/gameboy") - {:ok, lv, _html} = live(conn, "/inline?target=tv") + {:ok, lv, _body} = live(conn, "/inline?_interface[target]=tv") assert lv |> element("gameboytv") |> render() =~ "TV Target Inline GameBoy Render 100" end test "can render the switch format", %{conn: conn} do conn = put_req_header(conn, "accept", "text/switch") - {:ok, lv, _html} = live(conn, "/inline") + {:ok, lv, _body} = live(conn, "/inline") assert lv |> element("switch") |> render() =~ "Inline Switch Render 100" end test "can render the switch format with tv target", %{conn: conn} do conn = put_req_header(conn, "accept", "text/switch") - {:ok, lv, _html} = live(conn, "/inline?target=tv") + {:ok, lv, _body} = live(conn, "/inline?_interface[target]=tv") assert lv |> element("switchtv") |> render() =~ "TV Target Inline Switch Render 100" end diff --git a/test/live_view_native/integrations/template_test.exs b/test/live_view_native/integrations/template_test.exs index 20ca8da0..d19c2f68 100644 --- a/test/live_view_native/integrations/template_test.exs +++ b/test/live_view_native/integrations/template_test.exs @@ -26,7 +26,7 @@ defmodule LiveViewNative.TemplateRenderTest do test "can render the gameboy format with tv target", %{conn: conn} do conn = put_req_header(conn, "accept", "text/gameboy") - {:ok, lv, _html} = live(conn, "/template?target=tv") + {:ok, lv, _html} = live(conn, "/template?_interface[target]=tv") assert lv |> element("gameboytv") |> render() =~ "TV Target Template GameBoy Render 200" end @@ -40,7 +40,7 @@ defmodule LiveViewNative.TemplateRenderTest do test "can render the switch format with tv target", %{conn: conn} do conn = put_req_header(conn, "accept", "text/switch") - {:ok, lv, _html} = live(conn, "/template?target=tv") + {:ok, lv, _html} = live(conn, "/template?_interface[target]=tv") assert lv |> element("switchtv") |> render() =~ "TV Target Template Switch Render 200" end diff --git a/test/live_view_native/session_plug_test.exs b/test/live_view_native/live_reloader_test.exs similarity index 51% rename from test/live_view_native/session_plug_test.exs rename to test/live_view_native/live_reloader_test.exs index 98d249f6..6fb78143 100644 --- a/test/live_view_native/session_plug_test.exs +++ b/test/live_view_native/live_reloader_test.exs @@ -1,4 +1,4 @@ -defmodule LiveViewNative.SessionPlugTest do +defmodule LiveViewNative.LiveReloaderTest do use ExUnit.Case import Plug.Test @@ -6,24 +6,17 @@ defmodule LiveViewNative.SessionPlugTest do defp conn(path) do conn(:get, path) - |> Plug.Conn.put_private(:phoenix_endpoint, MyApp.Endpoint) + |> Plug.Conn.put_private(:phoenix_endpoint, LiveViewNativeTest.Endpoint) end describe "live reloader" do test "injects live_reload for LVN requests if configured and injects at the end of the body" do - opts = LiveViewNative.SessionPlug.init([]) + opts = LiveViewNative.LiveReloader.init([]) - # define so it exists - :root_layout_native - conn = conn("/") - |> Map.put(:params, %{"_lvn" => %{"format" => "native"}}) - |> put_private(:phoenix_format, "html") - |> put_private(:phoenix_root_layout, %{ - "html" => {TestLayout, :root_layout} - }) - |> LiveViewNative.SessionPlug.call(opts) + |> Map.put(:query_string, "_format=gameboy") + |> LiveViewNative.LiveReloader.call(opts) |> send_resp(200, "Hello, Elixir") assert to_string(conn.resp_body) == diff --git a/test/live_view_native/live_session_test.exs b/test/live_view_native/live_session_test.exs deleted file mode 100644 index d054e3eb..00000000 --- a/test/live_view_native/live_session_test.exs +++ /dev/null @@ -1,59 +0,0 @@ -defmodule LiveViewNative.LiveSessionTest do - use ExUnit.Case - - # alias Phoenix.LiveView.Socket - # alias LiveViewNative.LiveSession - - # describe "on_mount/4" do - # test "assigns a @native assign with the correct platform configuration" do - # socket = %Socket{ - # private: %{ - # connect_params: %{ - # "_lvn" => %{ - # "app_version" => "0.2", - # "app_build" => "1", - # "bundle_id" => "com.TestSuite.LVN", - # "format" => "lvntest", - # "os" => "fakeos", - # "os_version" => "1.0", - # "target" => "testsuite" - # } - # } - # }, - # transport_pid: self() - # } - - # {:cont, updated_socket} = LiveSession.on_mount(:live_view_native, %{}, %{}, socket) - - # assert updated_socket.assigns - # assert updated_socket.assigns.native - # assert updated_socket.assigns.app_version == "0.2" - # assert updated_socket.assigns.app_build == "1" - # assert updated_socket.assigns.bundle_id == "com.TestSuite.LVN" - # assert updated_socket.assigns.format == :lvntest - # assert updated_socket.assigns.os == "fakeos" - # assert updated_socket.assigns.os_version == "1.0" - # assert updated_socket.assigns.target == :testsuite - # assert updated_socket.assigns.native.__struct__ == LiveViewNativePlatform.Env - # assert updated_socket.assigns.native.platform_id == :lvntest - - # assert updated_socket.assigns.native.platform_config == %LiveViewNative.TestPlatform{ - # testing_notes: "everything is ok" - # } - - # assert updated_socket.assigns.native.template_extension == ".test.heex" - # end - - # test "does nothing if _lvn connect param is not passed" do - # socket = %Socket{ - # private: %{connect_params: %{}}, - # transport_pid: self() - # } - - # {:cont, updated_socket} = LiveSession.on_mount(:live_view_native, %{}, %{}, socket) - - # assert updated_socket.assigns - # refute Map.has_key?(updated_socket.assigns, :native) - # end - # end -end diff --git a/test/live_view_native_test.exs b/test/live_view_native_test.exs deleted file mode 100644 index 3d0aff38..00000000 --- a/test/live_view_native_test.exs +++ /dev/null @@ -1,4 +0,0 @@ -defmodule LiveViewNativeTest do - use ExUnit.Case - -end diff --git a/test/support/live/inline_live.ex b/test/support/live/inline_live.ex index 980a8bb0..39858788 100644 --- a/test/support/live/inline_live.ex +++ b/test/support/live/inline_live.ex @@ -4,7 +4,7 @@ defmodule LiveViewNativeTest.InlineLive.GameBoy do as: :render, layout: {LiveViewNativeTest.GameBoyLayouts, :app} - def render(assigns, %{target: "tv"}) do + def render(assigns, %{"target" => "tv"}) do ~LVN""" TV Target Inline GameBoy Render <%= @count %> """ @@ -23,7 +23,7 @@ defmodule LiveViewNativeTest.InlineLive.Switch do as: :render, layout: {LiveViewNativeTest.SwitchLayouts, :app} - def render(assigns, %{target: "tv"}) do + def render(assigns, %{"target" => "tv"}) do ~LVN""" TV Target Inline Switch Render <%= @count %> """