Skip to content

Commit 6bf4176

Browse files
authored
fix refresh action (#53)
2 parents 9a66393 + 5aa7d97 commit 6bf4176

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

.changeset/khaki-brooms-notice.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@coldwired/turbo-stream': patch
3+
'@coldwired/actions': patch
4+
---
5+
6+
fix refresh action

packages/actions/src/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ export class Actions {
253253
private materializeActions(actions: Action[], element: Element): MaterializedAction[] {
254254
this._debugMaterializeActions(actions, element);
255255
return actions.map((action) => {
256+
if (action.action == 'refresh') {
257+
return { ...action, targets: [] };
258+
}
256259
const targets = getTargetElements(element, action.targets);
257260
return { ...action, targets };
258261
});

packages/turbo-stream/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export function parseTurboStream(stream: Element): Action {
1414
invariant(stream.tagName == 'TURBO-STREAM', '[turbo-stream] element must be a <turbo-stream>');
1515

1616
const action = parseActionName(stream);
17+
18+
if (action == 'refresh') {
19+
return { action, targets: '' };
20+
}
21+
1722
const delay = parseDelay(stream);
1823
const pin = parsePin(stream);
1924
const targets = parseTargets(stream);

packages/turbo-stream/src/turbo-stream.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeEach } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
22

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

18+
it('should refresh', async () => {
19+
let fetchCalled = false;
20+
let path = '';
21+
vi.stubGlobal(
22+
'fetch',
23+
vi.fn((url: string) => {
24+
fetchCalled = true;
25+
path = url;
26+
return Promise.reject({ name: 'AbortError' });
27+
}),
28+
);
29+
await renderTurboStream(actions, '<turbo-stream action="refresh"></turbo-stream>');
30+
expect(fetchCalled).toBeTruthy();
31+
expect(path).toBe(`${window.location.pathname}${window.location.search}`);
32+
});
33+
1834
it('should append', async () => {
1935
actions.morph(document, parseHTMLDocument('<div id="test1"><h1>Bonjour</h1></div>'));
2036
const from = document.body.firstElementChild as HTMLDivElement;

0 commit comments

Comments
 (0)