2019-01-24 08:08:28 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2023-04-12 10:33:14 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-10-22 07:29:58 +00:00
|
|
|
|
2018-10-22 07:30:21 +00:00
|
|
|
describe('Model crud()', () => {
|
2023-04-12 10:33:14 +00:00
|
|
|
beforeAll(async() => {
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
2018-10-22 07:29:58 +00:00
|
|
|
let insertId;
|
2020-10-06 08:15:27 +00:00
|
|
|
const barcodeModel = app.models.ItemBarcode;
|
2018-10-22 07:29:58 +00:00
|
|
|
|
|
|
|
it('should inherit crud method from VnModel', () => {
|
2020-10-06 08:15:27 +00:00
|
|
|
expect(barcodeModel.crud).toBeDefined();
|
2018-10-22 07:29:58 +00:00
|
|
|
});
|
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
it('should create, edit and delete an instance', async() => {
|
|
|
|
let barcodeData = {code: '500', itemFk: '1'};
|
|
|
|
let creates = [barcodeData];
|
2018-10-22 07:29:58 +00:00
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
await barcodeModel.crud(null, null, creates);
|
|
|
|
let instance = await barcodeModel.findOne({where: barcodeData});
|
2018-10-22 07:29:58 +00:00
|
|
|
insertId = instance.id;
|
|
|
|
|
|
|
|
expect(instance).not.toEqual(null);
|
|
|
|
expect(instance.code).toEqual('500');
|
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
let updates = [{
|
|
|
|
where: {id: insertId},
|
|
|
|
data: {code: '501', itemFk: 1}
|
|
|
|
}];
|
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
await barcodeModel.crud(null, updates);
|
|
|
|
let editedInstance = await barcodeModel.findById(insertId);
|
2018-10-22 07:29:58 +00:00
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
expect(editedInstance.code).toEqual('501');
|
2018-10-22 07:29:58 +00:00
|
|
|
|
2018-11-02 14:44:27 +00:00
|
|
|
let deletes = [insertId];
|
2020-10-06 08:15:27 +00:00
|
|
|
await barcodeModel.crud(deletes);
|
|
|
|
let deletedInstance = await barcodeModel.findById(insertId);
|
2018-10-22 07:29:58 +00:00
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
expect(deletedInstance).toEqual(null);
|
2018-10-22 07:29:58 +00:00
|
|
|
});
|
|
|
|
});
|