Skip to content

Commit

Permalink
Fixed available_plugins bug
Browse files Browse the repository at this point in the history
Also added unit tests for `LiveViewNative`
  • Loading branch information
bcardarella committed Mar 7, 2024
1 parent 2514609 commit 9b48ed7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
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

0 comments on commit 9b48ed7

Please sign in to comment.