Skip to content

Commit

Permalink
Added tests for live_vue component rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Valian committed Nov 30, 2024
1 parent c505052 commit 18d6905
Showing 1 changed file with 102 additions and 5 deletions.
107 changes: 102 additions & 5 deletions test/live_vue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,111 @@ defmodule LiveVueTest do
import Phoenix.Component
import Phoenix.LiveViewTest

alias Phoenix.LiveView.JS
alias LiveVue.Test

doctest LiveVue

def test_component(assigns) do
~H'<.vue name="John" surname="Doe" vue-component="MyComponent" />'
describe "basic component rendering" do
def simple_component(assigns) do
~H"""
<.vue name="John" surname="Doe" v-component="MyComponent" />
"""
end

test "renders component with correct props" do

Check failure on line 20 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test basic component rendering renders component with correct props (LiveVueTest)

Check failure on line 20 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test basic component rendering renders component with correct props (LiveVueTest)
html = render_component(&simple_component/1)
vue = Test.get_vue(html)

assert vue.component == "MyComponent"
assert vue.props == %{"name" => "John", "surname" => "Doe"}
end

test "generates consistent ID" do

Check failure on line 28 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test basic component rendering generates consistent ID (LiveVueTest)

Check failure on line 28 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test basic component rendering generates consistent ID (LiveVueTest)
html = render_component(&simple_component/1)
vue = Test.get_vue(html)

assert vue.id =~ ~r/MyComponent-\d+/
end
end

describe "multiple components" do
def multi_component(assigns) do
~H"""
<div>
<.vue name="John" v-component="UserProfile" />
<.vue name="Jane" v-component="UserCard" />
</div>
"""
end

test "finds first component by default" do

Check failure on line 46 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test multiple components finds first component by default (LiveVueTest)

Check failure on line 46 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test multiple components finds first component by default (LiveVueTest)
html = render_component(&multi_component/1)
vue = Test.get_vue(html)

assert vue.component == "UserProfile"
assert vue.props == %{"name" => "John"}
end

test "finds specific component by name" do

Check failure on line 54 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test multiple components finds specific component by name (LiveVueTest)

Check failure on line 54 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test multiple components finds specific component by name (LiveVueTest)
html = render_component(&multi_component/1)
vue = Test.get_vue(html, name: "UserCard")

assert vue.component == "UserCard"
assert vue.props == %{"name" => "Jane"}
end
end

@tag :skip
test "Render the html correctly" do
assert render_component(&test_component/1) =~ "<h1>Hello John Doe</h1>"
describe "event handlers" do
def component_with_events(assigns) do
~H"""
<.vue
name="John"
v-component="MyComponent"
v-on:click={JS.push("click", value: %{"abc" => "def"})}
v-on:submit={JS.push("submit")}
/>
"""
end

test "renders event handlers correctly" do

Check failure on line 75 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test event handlers renders event handlers correctly (LiveVueTest)

Check failure on line 75 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test event handlers renders event handlers correctly (LiveVueTest)
html = render_component(&component_with_events/1)
vue = Test.get_vue(html)

assert vue.handlers == %{
"click" => JS.push("click", value: %{"abc" => "def"}),
"submit" => JS.push("submit")
}
end
end

describe "styling" do
def styled_component(assigns) do
~H"""
<.vue name="John" v-component="MyComponent" class="bg-blue-500 rounded" />
"""
end

test "applies CSS classes" do

Check failure on line 93 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test styling applies CSS classes (LiveVueTest)

Check failure on line 93 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test styling applies CSS classes (LiveVueTest)
html = render_component(&styled_component/1)
vue = Test.get_vue(html)

assert vue.class == "bg-blue-500 rounded"
end
end

describe "SSR behavior" do
def ssr_component(assigns) do
~H"""
<.vue name="John" v-component="MyComponent" v-ssr={false} />
"""
end

test "respects SSR flag" do

Check failure on line 108 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.15 / OTP 25.3

test SSR behavior respects SSR flag (LiveVueTest)

Check failure on line 108 in test/live_vue_test.exs

View workflow job for this annotation

GitHub Actions / Build and test 1.17 / OTP 27.1

test SSR behavior respects SSR flag (LiveVueTest)
html = render_component(&ssr_component/1)
vue = Test.get_vue(html)

assert vue.ssr == false
end
end
end

0 comments on commit 18d6905

Please sign in to comment.