56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
|
const models = require('vn-loopback/server/server').models;
|
||
|
const LoopBackContext = require('loopback-context');
|
||
|
|
||
|
describe('ClaimBeginning model()', () => {
|
||
|
const claimFk = 1;
|
||
|
const activeCtx = {
|
||
|
accessToken: {userId: 18},
|
||
|
headers: {origin: 'localhost:5000'},
|
||
|
__: () => {}
|
||
|
};
|
||
|
|
||
|
beforeEach(() => {
|
||
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||
|
active: activeCtx
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should change quantity claimed', async() => {
|
||
|
const tx = await models.ClaimBeginning.beginTransaction({});
|
||
|
|
||
|
let error;
|
||
|
try {
|
||
|
const options = {transaction: tx};
|
||
|
const claim = await models.ClaimBeginning.findOne({where: {claimFk}}, options);
|
||
|
const sale = await models.Sale.findById(claim.saleFk, {}, options);
|
||
|
await claim.updateAttribute('quantity', sale.quantity - 1, options);
|
||
|
|
||
|
await tx.rollback();
|
||
|
} catch (e) {
|
||
|
error = e;
|
||
|
await tx.rollback();
|
||
|
}
|
||
|
|
||
|
expect(error).toBeUndefined();
|
||
|
});
|
||
|
|
||
|
it('should throw error when quantity claimed is greater than quantity of the sale', async() => {
|
||
|
const tx = await models.ClaimBeginning.beginTransaction({});
|
||
|
|
||
|
let error;
|
||
|
try {
|
||
|
const options = {transaction: tx};
|
||
|
const claim = await models.ClaimBeginning.findOne({where: {claimFk}}, options);
|
||
|
const sale = await models.Sale.findById(claim.saleFk, {}, options);
|
||
|
await claim.updateAttribute('quantity', sale.quantity + 1, options);
|
||
|
|
||
|
await tx.rollback();
|
||
|
} catch (e) {
|
||
|
error = e;
|
||
|
await tx.rollback();
|
||
|
}
|
||
|
|
||
|
expect(error.toString()).toContain('The quantity claimed cannot be greater than the quantity of the line');
|
||
|
});
|
||
|
});
|