Skip to content

Commit b2be836

Browse files
committed
[tests] Basic unit test coverage for Torch.Component
[related to issue #393]
1 parent 0c4df0a commit b2be836

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

test/torch/component_test.exs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
defmodule Torch.ComponentTest do
2+
use ExUnit.Case, async: true
3+
use Phoenix.Component
4+
5+
import Phoenix.LiveViewTest
6+
alias Torch.Component
7+
8+
describe "torch_input/1" do
9+
test "renders a text input with default attributes" do
10+
html =
11+
render_component(&Component.torch_input/1, %{
12+
type: "text",
13+
name: "username",
14+
value: "test_user"
15+
})
16+
17+
assert html =~ ~s(<input type="text" name="username" id="username" value="test_user")
18+
end
19+
20+
test "renders a checkbox input with checked attribute" do
21+
html =
22+
render_component(&Component.torch_input/1, %{
23+
type: "checkbox",
24+
name: "accept_terms",
25+
value: true
26+
})
27+
28+
assert html =~ ~s(<input type="checkbox" name="accept_terms" id="accept_terms" value="true" checked)
29+
end
30+
31+
test "renders a select input with options and prompt" do
32+
html =
33+
render_component(&Component.torch_input/1, %{
34+
type: "select",
35+
name: "country",
36+
options: [{"USA", "us"}, {"Canada", "ca"}],
37+
prompt: "Select a country",
38+
value: "us"
39+
})
40+
41+
assert html =~ ~s(<option value="">Select a country</option>)
42+
assert html =~ ~s(<option selected value="us">USA</option>)
43+
assert html =~ ~s(<option value="ca">Canada</option>)
44+
end
45+
46+
test "renders a textarea input with value" do
47+
html =
48+
render_component(&Component.torch_input/1, %{
49+
type: "textarea",
50+
name: "bio",
51+
value: "This is a test bio."
52+
})
53+
54+
assert html =~ ~s(<textarea id="bio" name="bio" class="">\nThis is a test bio.</textarea>)
55+
end
56+
57+
test "renders errors for invalid input" do
58+
html =
59+
render_component(&Component.torch_input/1, %{
60+
type: "text",
61+
name: "username",
62+
value: "",
63+
errors: ["can't be blank"]
64+
})
65+
66+
assert html =~ ~s(<span class="invalid-feedback">can&#39;t be blank</span>)
67+
end
68+
end
69+
70+
describe "torch_label/1" do
71+
def torch_label(assigns), do: Component.torch_label(assigns)
72+
73+
test "renders a label with text" do
74+
html = render_component(fn assigns ->
75+
~H"""
76+
<.torch_label for="username">Username</.torch_label>
77+
"""
78+
end)
79+
80+
assert html =~ ~s(<label for="username">\n Username\n</label>)
81+
end
82+
end
83+
84+
describe "torch_error/1" do
85+
def torch_error(assigns), do: Component.torch_error(assigns)
86+
87+
test "renders an error message" do
88+
html = render_component(fn assigns ->
89+
~H"""
90+
<.torch_error for="username">This is an error.</.torch_error>
91+
"""
92+
end)
93+
94+
assert html =~ ~s(<span class="invalid-feedback">This is an error.</span>)
95+
end
96+
end
97+
98+
describe "flash_messages/1" do
99+
test "renders flash messages" do
100+
html =
101+
render_component(&Component.flash_messages/1, %{
102+
flash: %{"info" => "Information message", "error" => "Error message"}
103+
})
104+
105+
assert html =~ ~s(<p class="torch-flash info">Information message)
106+
assert html =~ ~s(<p class="torch-flash error">Error message)
107+
end
108+
end
109+
110+
describe "torch_flash/1" do
111+
test "renders a single flash message" do
112+
html =
113+
render_component(&Component.torch_flash/1, %{
114+
flash_type: "info",
115+
message: "This is a flash message."
116+
})
117+
118+
assert html =~ ~s(<p class="torch-flash info">This is a flash message.)
119+
end
120+
end
121+
122+
describe "translate_error/1" do
123+
test "translates an error message with count" do
124+
msg = Component.translate_error({"%{count} files", count: 2})
125+
assert msg == "2 files"
126+
end
127+
128+
test "translates a simple error message" do
129+
msg = Component.translate_error({"is invalid", []})
130+
assert msg == "is invalid"
131+
end
132+
end
133+
134+
describe "translate_errors/2" do
135+
test "translates errors for a specific field" do
136+
errors = [username: {"can't be blank", []}, email: {"is invalid", []}]
137+
translated = Component.translate_errors(errors, :username)
138+
assert translated == ["can't be blank"]
139+
end
140+
end
141+
end

0 commit comments

Comments
 (0)