Skip to content

Commit

Permalink
Add real integration tests. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored May 4, 2024
1 parent 92d75f3 commit be07a61
Show file tree
Hide file tree
Showing 32 changed files with 5,393 additions and 20 deletions.
3 changes: 3 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@

gem "bake-test"
gem "bake-test-external"

gem "sus-fixtures-async-http"
gem "sus-fixtures-async-webdriver"
end
File renamed without changes.
18 changes: 13 additions & 5 deletions lib/live/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ def initialize(id, **data)

# Generate a JavaScript string which forwards the specified event to the server.
# @parameter details [Hash] The details associated with the forwarded event.
def forward(details = nil)
def forward_event(details = nil)
if details
"live.forward(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
"live.forwardEvent(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
else
"live.forward(#{JSON.dump(@id)}, event)"
"live.forwardEvent(#{JSON.dump(@id)}, event)"
end
end

def forward_form_event(details = nil)
if details
"live.forwardFormEvent(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
else
"live.forwardFormEvent(#{JSON.dump(@id)}, event)"
end
end

Expand All @@ -49,9 +57,9 @@ def handle(event)
# Enqueue a remote procedure call to the currently bound page.
# @parameter method [Symbol] The name of the remote functio to invoke.
# @parameter arguments [Array]
def rpc(method, arguments)
def rpc(*arguments)
# This update might not be sent right away. Therefore, mutable arguments may be serialized to JSON at a later time (or never). This could be a race condition:
@page.updates.enqueue([method, arguments])
@page.updates.enqueue(arguments)
end
end
end
12 changes: 6 additions & 6 deletions lib/live/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def handle(id, event)
if element = @elements[id]
return element.handle(event)
else
Console.logger.warn(self, "Could not handle event:", event, details)
Console.warn(self, "Could not handle event:", event, details)
end

return nil
Expand All @@ -68,12 +68,12 @@ def process_message(message)
if element = self.resolve(id, data)
self.bind(element)
else
Console.logger.warn(self, "Could not resolve element:", message)
Console.warn(self, "Could not resolve element:", message)
end
elsif id = message[:id]
self.handle(id, message[:event])
else
Console.logger.warn(self, "Unhandled message:", message)
Console.warn(self, "Unhandled message:", message)
end
end

Expand All @@ -82,20 +82,20 @@ def process_message(message)
def run(connection)
queue_task = Async do
while update = @updates.dequeue
Console.logger.debug(self, "Sending update:", update)
Console.debug(self, "Sending update:", update)

connection.write(::Protocol::WebSocket::JSONMessage.generate(update))
connection.flush if @updates.empty?
end
end

while message = connection.read
Console.logger.debug(self, "Reading message:", message)
Console.debug(self, "Reading message:", message)

if json_message = ::Protocol::WebSocket::JSONMessage.wrap(message)
process_message(json_message.parse)
else
Console.logger.warn(self, "Unhandled message:", message)
Console.warn(self, "Unhandled message:", message)
end
end
ensure
Expand Down
37 changes: 28 additions & 9 deletions lib/live/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,40 @@
module Live
# Represents a single division of content on the page an provides helpers for rendering the content.
class View < Element
# Update the content of the client-side element by rendering this view.
def update!(**options)
rpc(:update, @id, self.to_html, options)
end

# Replace the content of the client-side element by rendering this view.
def replace!
rpc(:replace, [@id, self.to_html])
# @parameter selector [String] The CSS selector to replace.
# @parameter node [String] The HTML to replace.
def replace(selector, node, **options)
rpc(:replace, selector, node.to_s, options)
end

# Prepend to the content of the client-side element by appending the specified element.
# @parameter selector [String] The CSS selector to prepend to.
# @parameter node [String] The HTML to prepend.
def prepend(selector, node, **options)
rpc(:prepend, selector, node.to_s, options)
end

# Append to the content of the client-side element by appending the specified element.
# @parameter node [Live::Element] The element to append.
def append!(element)
rpc(:append, [@id, element.to_html])
# @parameter selector [String] The CSS selector to append to.
# @parameter node [String] The HTML to prepend.
def append(selector, node, **options)
rpc(:append, selector, node.to_s, options)
end

# Prepend to the content of the client-side element by appending the specified element.
# @parameter node [Live::Element] The element to prepend.
def prepend!(element)
rpc(:prepend, [@id, element.to_html])
# Remove the specified element from the client-side element.
# @parameter selector [String] The CSS selector to remove.
def remove(selector, **options)
rpc(:remove, selector, options)
end

def dispatch_event(selector, type, **options)
rpc(:dispatch_event, selector, event, options)
end

# Render the element.
Expand Down
22 changes: 22 additions & 0 deletions test/live/.website/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Live Test</title>
<script type="importmap">
{
"imports": {
"live": "/node_modules/@socketry/live/Live.js",
"morphdom": "/node_modules/morphdom/dist/morphdom-esm.js"
}
}
</script>
</head>
<body>
<div class="live" id="test" data-class="TestTag"></div>
<script type="module">
import {Live} from 'live';
// Use the Live module here
let live = Live.start();
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions test/live/.website/node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit be07a61

Please sign in to comment.