Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed available_plugins bug #140

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/live_view_native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
41 changes: 41 additions & 0 deletions test/live_view_native_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading