mirror of
https://github.com/withastro/astro.git
synced 2025-03-24 23:21:57 -05:00
actions: check result.data is not undefined instead of truthy (#11559)
* actions: check result.data is not undefined instead of truthy * add changeset * Update .changeset/tasty-rockets-jog.md --------- Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
parent
e3f29d416a
commit
1953dbbd41
4 changed files with 44 additions and 1 deletions
5
.changeset/tasty-rockets-jog.md
Normal file
5
.changeset/tasty-rockets-jog.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allows actions to return falsy values without an error
|
|
@ -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',
|
||||
},
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -64,4 +64,14 @@ export const server = {
|
|||
return;
|
||||
}
|
||||
}),
|
||||
zero: defineAction({
|
||||
handler: async () => {
|
||||
return 0;
|
||||
}
|
||||
}),
|
||||
false: defineAction({
|
||||
handler: async () => {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue