Files
cloud-cap-samples/test/messaging.test.js
Daniel Hutzel 7b92463394 Spring cleaning, part 2 (#813)
* Remove OData v2 show case: Genres TreeView
* Refactored bookstore and fiori services: consolidate routing and remove deprecated files
* Add Jest configuration file with silent mode enabled
* Refactor test files: streamline imports and remove redundant code
* Change price field type to Price for better clarity and precision
* Example for separation of concerns: access-control.cds
2025-03-21 09:48:11 +01:00

75 lines
2.8 KiB
JavaScript

const cds = require('@sap/cds')
const { expect } = cds.test.in(__dirname,'..')
describe('cap/samples - Messaging', ()=>{
const _model = '@capire/reviews'
const Reviews = 'sap.capire.reviews.Reviews'
beforeAll(()=>{
cds.User.default = cds.User.Privileged // hard core monkey patch
})
it ('should bootstrap sqlite in-memory db', async()=>{
const db = await cds.deploy (_model) .to ('sqlite::memory:')
await db.delete(Reviews)
expect (db.model) .not.undefined
})
let srv
it ('should serve ReviewsService', async()=>{
srv = await cds.serve('ReviewsService') .from (_model)
expect (srv.name) .to.match (/ReviewsService/)
})
let N=0, received=[], M=0
it ('should add messaging event handlers', ()=>{
srv.on('reviewed', (msg)=> received.push(msg))
})
it ('should add more messaging event handlers', ()=>{
srv.on('reviewed', ()=> ++M)
})
it ('should add review', async ()=>{
const review = { subject: "201", title: "Captivating", rating: ++N }
cds._debug = 1
const response = await srv.create ('Reviews') .entries (review)
expect (response) .to.containSubset (review)
})
it ('should add more reviews', ()=> Promise.all ([
// REVISIT: mass operation should trigger one message per entry
// srv.create('Reviews').entries(
// { ID: 111 + (++N), subject: "201", title: "Captivating", rating: N },
// { ID: 111 + (++N), subject: "201", title: "Captivating", rating: N },
// { ID: 111 + (++N), subject: "201", title: "Captivating", rating: N },
// { ID: 111 + (++N), subject: "201", title: "Captivating", rating: N },
// ),
srv.create ('Reviews') .entries (
{ ID: String(111 + (++N)), subject: "201", title: "Captivating", rating: N }
),
srv.create ('Reviews') .entries (
{ ID: String(111 + (++N)), subject: "201", title: "Captivating", rating: N }
),
srv.create ('Reviews') .entries (
{ ID: String(111 + (++N)), subject: "201", title: "Captivating", rating: N }
),
srv.create ('Reviews') .entries (
{ ID: String(111 + (++N)), subject: "201", title: "Captivating", rating: N }
),
]))
it ('should have received all messages', async()=> {
await new Promise((done)=>setImmediate(done))
expect(M).equals(N)
expect(received.length).equals(N)
expect(received.map(m=>m.data)).to.deep.equal([
{ count: 1, subject: '201', rating: 1 },
{ count: 2, subject: '201', rating: 1.5 },
{ count: 3, subject: '201', rating: 2 },
{ count: 4, subject: '201', rating: 2.5 },
{ count: 5, subject: '201', rating: 3 },
])
})
})