diff --git a/lib/live_view_native.ex b/lib/live_view_native.ex index 9a0ee2d..1e1f410 100644 --- a/lib/live_view_native.ex +++ b/lib/live_view_native.ex @@ -115,7 +115,16 @@ defmodule LiveViewNative do end @doc""" - Returns a list of all available LiveView Nativ plugins + Fetches a plugin based upon the format name + + Follows the same return types as `Map.fetch!/2` + """ + def fetch_plugin!(format) do + Map.fetch!(plugins(), stringify_format(format)) + end + + @doc""" + Returns a list of all available LiveView Native plugins Only the plugins that have been registered in your application config will be returned in the list @@ -144,13 +153,8 @@ defmodule LiveViewNative do The format list is derived from the plugins returned by `LiveViewNative.plugins/0` """ def available_formats() do - case Application.fetch_env(:live_view_native, :plugins) do - {:ok, plugins} -> - Enum.map(plugins, &(&1.format)) - :error -> - IO.warn("No LiveView Native plugins registered") - - [] - end + plugins() + |> Map.values() + |> Enum.map(&(&1.format)) end end diff --git a/test/live_view_native_test.exs b/test/live_view_native_test.exs new file mode 100644 index 0000000..02118ba --- /dev/null +++ b/test/live_view_native_test.exs @@ -0,0 +1,41 @@ +defmodule LiveViewNative.Test do + use ExUnit.Case + + alias LiveViewNativeTest.{GameBoy, Switch} + + test "fetch_plugin/1" do + {:ok, plugin} = LiveViewNative.fetch_plugin(:gameboy) + assert plugin.format == :gameboy + + {:ok, plugin} = LiveViewNative.fetch_plugin(:switch) + assert plugin.format == :switch + + assert LiveViewNative.fetch_plugin(:other) == :error + end + + test "fetch_plugin!/1" do + plugin = LiveViewNative.fetch_plugin!(:gameboy) + assert plugin.format == :gameboy + + plugin = LiveViewNative.fetch_plugin!(:switch) + assert plugin.format == :switch + + assert_raise KeyError, fn -> + LiveViewNative.fetch_plugin!(:other) + end + end + + test "plugins/0" do + plugins = LiveViewNative.plugins() + + assert %GameBoy{} = plugins["gameboy"] + assert %Switch{} = plugins["switch"] + end + + test "available_formats/0" do + formats = LiveViewNative.available_formats() + + assert Enum.member?(formats, :gameboy) + assert Enum.member?(formats, :switch) + end +end