salix/loopback/common/methods/vn-model/specs/rewriteDbError.spec.js

45 lines
1.3 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('Model rewriteDbError()', () => {
beforeAll(async() => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should extend rewriteDbError properties to any model passed', () => {
const exampleModel = models.ItemTag;
expect(exampleModel.rewriteDbError).toBeDefined();
});
it('should handle a duplicated warehouse error', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const itemTag = {itemFk: 1, tagFk: 56, value: 'Ranged weapon', priority: 1};
await models.ItemTag.create(itemTag, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`The tag or priority can't be repeated for an item`);
});
});