salix/modules/supplier/back/models/specs/supplier.spec.js

147 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-11-10 14:11:42 +00:00
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
2020-12-14 15:09:17 +00:00
2021-04-01 16:38:40 +00:00
describe('loopback model Supplier', () => {
2022-12-15 08:52:34 +00:00
let supplierOne;
let supplierTwo;
beforeAll(async() => {
2022-12-15 08:52:34 +00:00
supplierOne = await models.Supplier.findById(1);
supplierTwo = await models.Supplier.findById(442);
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
2020-12-14 15:09:17 +00:00
});
describe('payMethodFk', () => {
it('should throw an error when attempting to set an invalid payMethod id in the supplier', async() => {
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
let error;
const expectedError = 'You can not select this payment method without a registered bankery account';
2022-12-15 08:52:34 +00:00
await supplierOne.updateAttribute('payMethodFk', 8, options)
.catch(e => {
error = e;
expect(error.message).toContain(expectedError);
});
expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2020-12-14 15:09:17 +00:00
});
it('should not throw if the payMethod id is valid', async() => {
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
let error;
2022-12-15 08:52:34 +00:00
await supplierTwo.updateAttribute('payMethodFk', 4, options)
.catch(e => {
error = e;
});
expect(error).not.toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2020-12-14 15:09:17 +00:00
});
it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => {
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
2022-12-15 08:52:34 +00:00
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payMethodFk', 5, options);
2022-12-16 11:50:09 +00:00
const result = await models.Supplier.findById(442, null, options);
expect(result.isPayMethodChecked).toEqual(true);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => {
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
2022-12-15 08:52:34 +00:00
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payMethodFk', 2, options);
2022-12-16 11:50:09 +00:00
const result = await models.Supplier.findById(442, null, options);
expect(result.isPayMethodChecked).toEqual(false);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => {
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
2022-12-15 08:52:34 +00:00
await supplierTwo.updateAttribute('payMethodFk', 2, options);
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payDay', 5, options);
2022-12-16 11:50:09 +00:00
const firstResult = await models.Supplier.findById(442, null, options);
2022-12-15 08:52:34 +00:00
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payDemFk', 1, options);
2022-12-16 11:50:09 +00:00
const secondResult = await models.Supplier.findById(442, null, options);
expect(firstResult.isPayMethodChecked).toEqual(false);
expect(secondResult.isPayMethodChecked).toEqual(false);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
2023-09-18 08:12:32 +00:00
2023-09-25 06:23:59 +00:00
it('should update the account attribute when a new supplier is created', async() => {
2023-09-18 08:12:32 +00:00
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
try {
2024-02-20 12:04:45 +00:00
const newSupplier = {
name: 'ALFRED PENNYWORTH', nif: '87805752D', city: 'Gotham'
};
const supplierCreated = await models.Supplier.create(newSupplier, options);
const fetchedSupplier = await models.Supplier.findById(supplierCreated.id, null, options);
2023-09-18 08:12:32 +00:00
2024-02-20 12:04:45 +00:00
expect(Number(fetchedSupplier.account)).toEqual(4100000000 + supplierCreated.id);
2023-09-18 08:12:32 +00:00
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});
2020-12-14 15:09:17 +00:00
});