Skip to content

Commit

Permalink
Fix websocket polling trigger (#1903)
Browse files Browse the repository at this point in the history
fix websocket polling trigger
  • Loading branch information
Renerick authored Oct 26, 2023
1 parent 0718daa commit a679244
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ext/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f

socketWrapper.send(body, elt);

if (api.shouldCancel(evt, elt)) {
if (evt && api.shouldCancel(evt, elt)) {
evt.preventDefault();
}
});
Expand Down
12 changes: 12 additions & 0 deletions test/ext/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ describe("web-sockets extension", function () {
this.messages.length.should.equal(1);
})

it('sends data to the server with polling trigger', function () {
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><div hx-trigger="every 1s" ws-send id="d1">div1</div></div>');
this.tickMock();
this.clock.tick(2000);

byId("d1").click();

this.tickMock();

this.messages.length.should.equal(2);
})

it('sends expected headers to the server', function () {
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><button hx-trigger="click" hx-target="#target" ws-send id="d1" name="d1-name">div1</button><output id="target"></output></div>');
this.tickMock();
Expand Down

0 comments on commit a679244

Please sign in to comment.