Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/khaki-brooms-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@coldwired/turbo-stream': patch
'@coldwired/actions': patch
---

fix refresh action
3 changes: 3 additions & 0 deletions packages/actions/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ export class Actions {
private materializeActions(actions: Action[], element: Element): MaterializedAction[] {
this._debugMaterializeActions(actions, element);
return actions.map((action) => {
if (action.action == 'refresh') {
return { ...action, targets: [] };
}
const targets = getTargetElements(element, action.targets);
return { ...action, targets };
});
Expand Down
5 changes: 5 additions & 0 deletions packages/turbo-stream/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export function parseTurboStream(stream: Element): Action {
invariant(stream.tagName == 'TURBO-STREAM', '[turbo-stream] element must be a <turbo-stream>');

const action = parseActionName(stream);

if (action == 'refresh') {
return { action, targets: '' };
}

const delay = parseDelay(stream);
const pin = parsePin(stream);
const targets = parseTargets(stream);
Expand Down
18 changes: 17 additions & 1 deletion packages/turbo-stream/src/turbo-stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { Actions } from '@coldwired/actions';
import { parseHTMLDocument } from '@coldwired/utils';
Expand All @@ -15,6 +15,22 @@ describe('@coldwired/turbo-stream', () => {
//actions.disconnect();
});

it('should refresh', async () => {
let fetchCalled = false;
let path = '';
vi.stubGlobal(
'fetch',
vi.fn((url: string) => {
fetchCalled = true;
path = url;
return Promise.reject({ name: 'AbortError' });
}),
);
await renderTurboStream(actions, '<turbo-stream action="refresh"></turbo-stream>');
expect(fetchCalled).toBeTruthy();
expect(path).toBe(`${window.location.pathname}${window.location.search}`);
});

it('should append', async () => {
actions.morph(document, parseHTMLDocument('<div id="test1"><h1>Bonjour</h1></div>'));
const from = document.body.firstElementChild as HTMLDivElement;
Expand Down