Skip to content

Commit

Permalink
Test Live::View#prepend and #append.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 4, 2024
1 parent be07a61 commit 0d48cf4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
3 changes: 3 additions & 0 deletions test/live/.website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
</head>
<body>
<div class="live" id="test" data-class="TestTag"></div>
<ul class="test">
<li class="middle">Middle</li>
</ul>
<script type="module">
import {Live} from 'live';
// Use the Live module here
Expand Down
60 changes: 51 additions & 9 deletions test/live/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@

require 'live/view'

class TestResolver < Live::Resolver
def initialize(...)
super

@bound = {}
end

attr :bound

def call(id, data)
super.tap do |element|
@bound[id] = element
end
end
end

class TestTag < Live::View
# Used for binding the tag instance to the client-side element via the resolver.
def self.name
"TestTag"
end

def bind(...)
super

@clock ||= Async do
while true
self.update!
sleep 1
end
end
self.update!
end

def render(builder)
Expand Down Expand Up @@ -51,7 +63,7 @@ def content_type(path)
end
end

let(:resolver) {Live::Resolver.allow(TestTag)}
let(:resolver) {TestResolver.allow(TestTag)}

def app
::Protocol::HTTP::Middleware.for do |request|
Expand All @@ -70,14 +82,44 @@ def app
end

it "can load website" do
session.implicit_wait_timeout = 10_000

navigate_to("/index.html")

expect(session.document_title).to be == "Live Test"

expect(find_element(css: "#test p")).to have_attributes(
text: be =~ /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/
)

expect(resolver.bound).not.to be(:empty?)
end

it "can prepend content" do
navigate_to("/index.html")

# Wait for the page to load.
find_element(css: "#test p")

tag = resolver.bound.values.first

tag.prepend("ul.test", '<li class="prepended">Prepended</li>')

# find_element(css: "ul.test li.prepended")

expect(find_element(css: "ul.test").text).to be == "Prepended\nMiddle"
end

it "can append content" do
navigate_to("/index.html")

# Wait for the page to load.
find_element(css: "#test p")

tag = resolver.bound.values.first

tag.append("ul.test", '<li class="appended">Appended</li>')

# find_element(css: "ul.test li.appended")

expect(find_element(css: "ul.test").text).to be == "Middle\nAppended"
end
end

0 comments on commit 0d48cf4

Please sign in to comment.