Skip to content

Commit

Permalink
actions: check result.data is not undefined instead of truthy (#11559)
Browse files Browse the repository at this point in the history
* actions: check result.data is not undefined instead of truthy

* add changeset

* Update .changeset/tasty-rockets-jog.md

---------

Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
bryanwood and bluwy authored Jul 30, 2024
1 parent e3f29d4 commit 1953dbb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-rockets-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allows actions to return falsy values without an error
2 changes: 1 addition & 1 deletion packages/astro/src/actions/runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const POST: APIRoute = async (context) => {
);
}
return new Response(JSON.stringify(result.data), {
status: result.data ? 200 : 204,
status: result.data !== undefined ? 200 : 204,
headers: {
'Content-Type': 'application/json',
},
Expand Down
28 changes: 28 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,33 @@ describe('Astro Actions', () => {
assert.equal($('[data-url]').text(), '/subscribe');
assert.equal($('[data-channel]').text(), 'bholmesdev');
});

it('Returns content when the value is 0', async () => {
const req = new Request('http://example.com/_actions/zero', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': '0',
},
});
const res = await app.render(req);
assert.equal(res.status, 200);
const value = await res.json();
assert.equal(value, 0);
});

it('Returns content when the value is false', async () => {
const req = new Request('http://example.com/_actions/false', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': '0',
},
});
const res = await app.render(req);
assert.equal(res.status, 200);
const value = await res.json();
assert.equal(value, false);
});
});
});
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/actions/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ export const server = {
return;
}
}),
zero: defineAction({
handler: async () => {
return 0;
}
}),
false: defineAction({
handler: async () => {
return false;
}
})
};

0 comments on commit 1953dbb

Please sign in to comment.