const models = require('vn-loopback/server/server').models; describe('invoiceIn', () => { let options; let tx; const invoiceId = 1; const supplierId = 791; const currencyId = 1; const companyId = 442; beforeEach(async() => { tx = await models.InvoiceIn.beginTransaction({}); options = {transaction: tx}; }); afterEach(async() => { await tx.rollback(); }); it('should allow insert for new instance', async() => { const newInvoice = { supplierFk: supplierId, issued: Date.vnNew(), operated: Date.vnNew(), currencyFk: currencyId, companyFk: companyId, isBooked: false }; const createdInvoice = await models.InvoiceIn.create(newInvoice, options); expect(createdInvoice).toBeDefined(); expect(createdInvoice.id).toBeDefined(); }); it('should throw an error if trying to update a booked invoice', async() => { const invoice = await models.InvoiceIn.findById(invoiceId, null, options); await invoice.updateAttribute('isBooked', true, options); let error; try { await invoice.updateAttribute('supplierFk', supplierId, options); } catch (err) { error = err; } expect(error.message).toBe('InvoiceIn is already booked'); }); it('should throw an error if trying to delete a booked invoice', async() => { const invoice = await models.InvoiceIn.findById(invoiceId, null, options); await invoice.updateAttribute('isBooked', true, options); let error; try { await models.InvoiceIn.deleteById(invoiceId, options); } catch (err) { error = err; } expect(error.message).toBe('InvoiceIn is already booked'); }); });