Skip to content

Commit

Permalink
test error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Oct 8, 2023
1 parent 7614bd4 commit 873bebb
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@
"problemMatcher": "$mixTestFailure"
},
{
"label": "run testbed test at cursor",
"label": "mix test focused",
"type": "shell",
"command": "mix",
"args": [
"test",
"../${relativeFile}:${lineNumber}",
"${relativeFile}:${lineNumber}",
"--color",
"--trace"
],
"options": {
"cwd": "${workspaceRoot}/testbed",
"cwd": "${workspaceRoot}",
"requireFiles": [
"test/**/test_helper.exs",
"test/**/*_test.exs"
Expand Down
2 changes: 1 addition & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ import './todo-list-element.ts';
// import './react-todo';
import './join-params';
import './patch-thing';
import './connect-error';
import './display-error';
14 changes: 11 additions & 3 deletions assets/js/connect-error.ts → assets/js/display-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ import { liveState, liveStateConfig, LiveStateError } from 'phx-live-state';
* @slot - This element has a slot
* @csspart button - The button
*/
@customElement('connect-error')
@liveState({topic: 'garbage', url: 'ws://localhost:4001/socket', events: {receive: ['livestate-error']}})
@customElement('display-error')
@liveState({events: {receive: ['livestate-error']}})
export class ConnectErrorElement extends LitElement {

@liveStateConfig('topic')
@property()
topic: string = '';

@property()
@liveStateConfig('url')
url: string = '';

constructor() {
super();
this.addEventListener('livestate-error', (e: CustomEvent) => {
this.errorDescription = e.detail.kind;
this.errorDescription = e.detail.message;
})
}

Expand Down
11 changes: 11 additions & 0 deletions lib/higher_order.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule HigherOrder do
def higher_order_function(callback) do
callback.("foo")
end
end

defmodule Callback do
def callback_function(arg) do
arg <> " I was called!"
end
end
8 changes: 8 additions & 0 deletions lib/livestate_testbed_web/channels/init_error_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule LivestateTestbedWeb.InitErrorChannel do
use LiveState.Channel, web_module: LivestateTestbedWeb

def init(_channel, _params, _socket) do
{:error, "You suck"}
end

end
1 change: 1 addition & 0 deletions lib/livestate_testbed_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule LivestateTestbedWeb.UserSocket do

channel "todo:*", LivestateTestbedWeb.TodoChannel
channel "join_params", LivestateTestbedWeb.JoinParamsChannel
channel "init_error", LivestateTestbedWeb.InitErrorChannel
channel "patchy", LivestateTestbedWeb.PatchChannel

# Socket params are passed from the client and can
Expand Down
3 changes: 2 additions & 1 deletion lib/livestate_testbed_web/templates/page/errors.html.heex
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<connect-error url={@url}></connect-error>
<display-error id="connect" topic="garbage" url={@url}></display-error>
<display-error id="init" topic="init_error" url={@url}></display-error>
14 changes: 11 additions & 3 deletions test/livestate_testbed_web/features/error_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ defmodule LivestateTestbedWeb.Features.ErrorTest do

import Wallaby.Query

feature "join params", %{session: session} do
feature "connect error", %{session: session} do
session
|> visit("/errors")
|> find(css("connect-error"))
|> find(css("display-error#connect"))
|> shadow_root()
|> assert_has(css("div", text: "error"))
|> assert_has(css("div", text: "unmatched topic"))
end

feature "init error", %{session: session} do
session
|> visit("/errors")
|> find(css("display-error#init"))
|> shadow_root()
|> assert_has(css("div", text: "You suck"))
end
end

0 comments on commit 873bebb

Please sign in to comment.