Skip to content
Draft
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
139 changes: 139 additions & 0 deletions test/torch/component_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,109 @@

assert html =~ ~s(<span class="invalid-feedback">can&#39;t be blank</span>)
end

test "renders a string input type" do

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 26.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 27.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 27.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 25.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 25.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 25.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 25.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 26.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 26.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)

Check failure on line 81 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 26.x)

test torch_input/1 renders a string input type (Torch.ComponentTest)
html =
render_component(fn assigns ->
~H"""
<.torch_input type="string" name="username" value="test_user" label="Username" />
"""
end)

assert html =~ ~s(<input type="text" name="username" id="username" value="test_user")
assert html =~ ~s(<label for="username">\n Username\n</label>)
end

test "renders a number input type" do

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 26.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 27.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 27.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 25.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 25.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 25.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 25.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 26.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 26.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)

Check failure on line 93 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 26.x)

test torch_input/1 renders a number input type (Torch.ComponentTest)
html =
render_component(fn assigns ->
~H"""
<.torch_input type="number" name="age" value="25" label="Age" />
"""
end)

assert html =~ ~s(<input type="number" name="age" id="age" value="25")
assert html =~ ~s(<label for="age">\n Age\n</label>)
end

test "renders a date input type" do

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 26.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 27.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 27.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 25.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 25.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 25.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 25.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 26.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 26.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)

Check failure on line 105 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 26.x)

test torch_input/1 renders a date input type (Torch.ComponentTest)
html =
render_component(fn assigns ->
~H"""
<.torch_input type="date" name="birthday" value="2023-01-01" label="Birthday" />
"""
end)

assert html =~ ~s(<input type="date" name="birthday" id="birthday" value="2023-01-01")
assert html =~ ~s(<label for="birthday">\n Birthday\n</label>)
end

test "renders with custom id" do
html =
render_component(fn assigns ->
~H"""
<.torch_input type="text" name="username" id="custom-id" value="test_user" />
"""
end)

assert html =~ ~s(<input type="text" name="username" id="custom-id" value="test_user")
end

test "renders with form field" do

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 26.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 27.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 27.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 25.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 25.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 25.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 25.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 26.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 26.x)

test torch_input/1 renders with form field (Torch.ComponentTest)

Check failure on line 128 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 26.x)

test torch_input/1 renders with form field (Torch.ComponentTest)
form_data = %{
"user" => %{
"name" => "John Doe",
"email" => "[email protected]"
}
}

form = Phoenix.Component.to_form(form_data, as: :user)
field = Phoenix.HTML.FormField.to_form_field(form, :name)

html =
render_component(
fn assigns ->
~H"""
<.torch_input field={@field} type="text" label="Name" />
"""
end,
%{
field: field
}
)

assert html =~ ~s(<input type="text" name="user[name]" id="user_name" value="John Doe")
assert html =~ ~s(<label for="user_name">\n Name\n</label>)
end

test "renders with form field and multiple flag" do

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 26.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 27.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 27.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 25.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 25.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.17.x | OTP 25.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 25.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | OTP 26.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.18.x | OTP 26.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)

Check failure on line 155 in test/torch/component_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.16.x | OTP 26.x)

test torch_input/1 renders with form field and multiple flag (Torch.ComponentTest)
form_data = %{
"user" => %{
"roles" => ["admin", "editor"]
}
}

form = Phoenix.Component.to_form(form_data, as: :user)
field = Phoenix.HTML.FormField.to_form_field(form, :roles)

html =
render_component(
fn assigns ->
~H"""
<.torch_input field={@field} type="select" multiple={true} options={@options} label="Roles" />
"""
end,
%{
field: field,
options: [{"Admin", "admin"}, {"Editor", "editor"}, {"Viewer", "viewer"}]
}
)

assert html =~ ~s(<select id="user_roles" name="user[roles][]" class="" multiple)
assert html =~ ~s(<option selected value="admin">Admin</option>)
assert html =~ ~s(<option selected value="editor">Editor</option>)
assert html =~ ~s(<option value="viewer">Viewer</option>)
end
end

describe "torch_label/1" do
Expand Down Expand Up @@ -122,6 +225,24 @@
assert html =~ ~s(<p class="torch-flash info">Information message)
assert html =~ ~s(<p class="torch-flash error">Error message)
end

test "renders empty flash messages" do
html =
render_component(
fn assigns ->
~H"""
<.flash_messages flash={@flash} />
"""
end,
%{
flash: %{}
}
)

assert html =~ ~s(<section id="torch-flash-messages">)
assert html =~ ~s(<div class="torch-container">)
refute html =~ ~s(<p class="torch-flash)
end
end

describe "torch_flash/1" do
Expand All @@ -135,6 +256,18 @@

assert html =~ ~s(<p class="torch-flash info">This is a flash message.)
end

test "renders a flash message with close button" do
html =
render_component(fn assigns ->
~H"""
<.torch_flash flash_type="error" message="Error occurred." />
"""
end)

assert html =~ ~s(<p class="torch-flash error">Error occurred.)
assert html =~ ~s(<button class="torch-flash-close">x</button>)
end
end

describe "translate_error/1" do
Expand All @@ -155,5 +288,11 @@
translated = Component.translate_errors(errors, :username)
assert translated == ["can't be blank"]
end

test "returns empty list when field has no errors" do
errors = [username: {"can't be blank", []}, email: {"is invalid", []}]
translated = Component.translate_errors(errors, :password)
assert translated == []
end
end
end
68 changes: 68 additions & 0 deletions test/torch/config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Torch.ConfigTest do
use ExUnit.Case, async: true

describe "otp_app/0" do
test "returns the configured otp_app" do
# Save the original value
original_value = Application.get_env(:torch, :otp_app)

# Set a test value
Application.put_env(:torch, :otp_app, :test_app)
assert Torch.Config.otp_app() == :test_app

# Restore the original value
if original_value do
Application.put_env(:torch, :otp_app, original_value)
else
Application.delete_env(:torch, :otp_app)
end
end

test "returns nil when otp_app is not configured" do
# Save the original value
original_value = Application.get_env(:torch, :otp_app)

# Delete the config
Application.delete_env(:torch, :otp_app)
assert Torch.Config.otp_app() == nil

# Restore the original value
if original_value do
Application.put_env(:torch, :otp_app, original_value)
end
end
end

describe "i18n_backend/0" do
test "returns the configured i18n_backend" do
# Save the original value
original_value = Application.get_env(:torch, :i18n_backend)

# Set a test value
test_backend = Module.concat(["Test", "I18n", "Backend"])
Application.put_env(:torch, :i18n_backend, test_backend)
assert Torch.Config.i18n_backend() == test_backend

# Restore the original value
if original_value do
Application.put_env(:torch, :i18n_backend, original_value)
else
Application.delete_env(:torch, :i18n_backend)
end
end

test "returns default backend when i18n_backend is not configured" do
# Save the original value
original_value = Application.get_env(:torch, :i18n_backend)

# Delete the config
Application.delete_env(:torch, :i18n_backend)
assert Torch.Config.i18n_backend() == Torch.I18n.Backend

# Restore the original value
if original_value do
Application.put_env(:torch, :i18n_backend, original_value)
end
end
end
end
Loading