salix/back/methods/collection/spec/setSaleQuantity.spec.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

const models = require('vn-loopback/server/server').models;
2023-03-23 09:05:02 +00:00
const LoopBackContext = require('loopback-context');
describe('setSaleQuantity()', () => {
2023-03-23 09:05:02 +00:00
beforeAll(async() => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should change quantity sale', async() => {
2022-11-25 10:50:24 +00:00
const tx = await models.Ticket.beginTransaction({});
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
if (sqlStatement.includes('catalog_calcFromItem')) {
sqlStatement = `CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY
SELECT 100 as available;`;
params = null;
}
return models.Ticket.rawSql(sqlStatement, params, options);
});
2022-11-25 10:50:24 +00:00
try {
const options = {transaction: tx};
2022-11-25 10:50:24 +00:00
const saleId = 30;
const newQuantity = 10;
2022-11-25 10:50:24 +00:00
const originalSale = await models.Sale.findById(saleId, null, options);
await models.Collection.setSaleQuantity(saleId, newQuantity, options);
const updateSale = await models.Sale.findById(saleId, null, options);
2023-03-08 11:10:08 +00:00
expect(updateSale.quantity).not.toEqual(originalSale.quantity);
2022-11-25 10:50:24 +00:00
expect(updateSale.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});