Add test to illustrate how to call actions locally (#276)
* Add test to illustrate how to call actions locally Add the code sample from the capire section [Unbound Actions / Functions](https://cap.cloud.sap/docs/node.js/services#-unbound-actions--functions) as a test in the code sample repository. Signed-off-by: Pierre Fritsch <PierreFritsch@users.noreply.github.com> * Wrap the action calls into a transaction Signed-off-by: Pierre Fritsch <PierreFritsch@users.noreply.github.com> * Add assertions Signed-off-by: Pierre Fritsch <PierreFritsch@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> * Update test/consuming-actions.test.js --------- Signed-off-by: Pierre Fritsch <PierreFritsch@users.noreply.github.com> Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com>
This commit is contained in:
53
test/consuming-actions.test.js
Normal file
53
test/consuming-actions.test.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const cds = require("@sap/cds");
|
||||||
|
const { expect } = cds.test(
|
||||||
|
"serve",
|
||||||
|
"CatalogService",
|
||||||
|
"--from",
|
||||||
|
"@capire/bookshop,@capire/common",
|
||||||
|
"--in-memory"
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("Consuming actions locally", () => {
|
||||||
|
let cats, CatalogService, Books, stockBefore;
|
||||||
|
const BOOK_ID = 251;
|
||||||
|
const QUANTITY = 1;
|
||||||
|
|
||||||
|
before("bootstrap the database", async () => {
|
||||||
|
CatalogService = cds.services.CatalogService;
|
||||||
|
expect(CatalogService).not.to.be.undefined;
|
||||||
|
|
||||||
|
Books = CatalogService.entities.Books;
|
||||||
|
expect(Books).not.to.be.undefined;
|
||||||
|
|
||||||
|
cats = await cds.connect.to("CatalogService");
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
// Read the stock before the action is called
|
||||||
|
stockBefore = (await cats.get(Books, BOOK_ID)).stock;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls unbound actions - basic variant using srv.send", async () => {
|
||||||
|
// Use a managed transaction to create a continuation with an authenticated user
|
||||||
|
const res1 = await cats.tx({ user: "alice" }, () => {
|
||||||
|
return cats.send("submitOrder", { book: BOOK_ID, quantity: QUANTITY });
|
||||||
|
});
|
||||||
|
expect(res1.stock).to.eql(stockBefore - QUANTITY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls unbound actions - named args variant", async () => {
|
||||||
|
// Use a managed transaction to create a continuation with an authenticated user
|
||||||
|
const res2 = await cats.tx({ user: "alice" }, () => {
|
||||||
|
return cats.submitOrder({ book: BOOK_ID, quantity: QUANTITY });
|
||||||
|
});
|
||||||
|
expect(res2.stock).to.eql(stockBefore - QUANTITY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls unbound actions - positional args variant", async () => {
|
||||||
|
// Use a managed transaction to create a continuation with an authenticated user
|
||||||
|
const res3 = await cats.tx({ user: "alice" }, () => {
|
||||||
|
return cats.submitOrder(BOOK_ID, QUANTITY);
|
||||||
|
});
|
||||||
|
expect(res3.stock).to.eql(stockBefore - QUANTITY);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user