From e2902640e3eeb4c53f6391541f33ad051dc923ce Mon Sep 17 00:00:00 2001 From: Pierre Fritsch Date: Mon, 29 Jul 2024 20:38:54 +0200 Subject: [PATCH] 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 * Wrap the action calls into a transaction Signed-off-by: Pierre Fritsch * Add assertions Signed-off-by: Pierre Fritsch * 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 Co-authored-by: sjvans <30337871+sjvans@users.noreply.github.com> --- test/consuming-actions.test.js | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test/consuming-actions.test.js diff --git a/test/consuming-actions.test.js b/test/consuming-actions.test.js new file mode 100644 index 00000000..133eb09d --- /dev/null +++ b/test/consuming-actions.test.js @@ -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); + }); +});