Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to illustrate how to call actions locally #276

Merged
merged 13 commits into from
Jul 29, 2024
53 changes: 53 additions & 0 deletions test/consuming-actions.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});