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', () => {
|
2021-10-15 12:45:41 +00:00
|
|
|
beforeAll(async() => {
|
2021-12-14 07:23:11 +00:00
|
|
|
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() => {
|
2022-12-15 07:31:47 +00:00
|
|
|
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';
|
|
|
|
const supplier = await models.Supplier.findOne({where: {id: 1}}, options);
|
|
|
|
|
|
|
|
await supplier.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() => {
|
2022-12-15 07:31:47 +00:00
|
|
|
const tx = await models.Supplier.beginTransaction({});
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
try {
|
|
|
|
let error;
|
|
|
|
const supplier = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
await supplier.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
|
|
|
});
|
2021-12-14 07:23:11 +00:00
|
|
|
|
2021-12-20 07:02:28 +00:00
|
|
|
it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => {
|
2022-12-15 07:31:47 +00:00
|
|
|
const tx = await models.Supplier.beginTransaction({});
|
|
|
|
const options = {transaction: tx};
|
2021-12-14 07:23:11 +00:00
|
|
|
|
2022-12-15 07:31:47 +00:00
|
|
|
try {
|
|
|
|
const supplier = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
await supplier.updateAttribute('isPayMethodChecked', true, options);
|
|
|
|
await supplier.updateAttribute('payMethodFk', 5, options);
|
2021-12-14 07:23:11 +00:00
|
|
|
|
2022-12-15 07:31:47 +00:00
|
|
|
const result = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
|
|
|
|
expect(result.isPayMethodChecked).toEqual(true);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-12-20 07:02:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => {
|
2022-12-15 07:31:47 +00:00
|
|
|
const tx = await models.Supplier.beginTransaction({});
|
|
|
|
const options = {transaction: tx};
|
2021-12-20 07:02:28 +00:00
|
|
|
|
2022-12-15 07:31:47 +00:00
|
|
|
try {
|
|
|
|
const supplier = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
await supplier.updateAttribute('isPayMethodChecked', true, options);
|
|
|
|
await supplier.updateAttribute('payMethodFk', 2, options);
|
2021-12-20 07:02:28 +00:00
|
|
|
|
2022-12-15 07:31:47 +00:00
|
|
|
const result = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
|
|
|
|
expect(result.isPayMethodChecked).toEqual(false);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-12-14 07:23:11 +00:00
|
|
|
});
|
2021-12-20 07:02:28 +00:00
|
|
|
|
|
|
|
it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => {
|
2022-12-15 07:31:47 +00:00
|
|
|
const tx = await models.Supplier.beginTransaction({});
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const supplier = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
await supplier.updateAttribute('payMethodFk', 2, options);
|
|
|
|
|
|
|
|
await supplier.updateAttribute('isPayMethodChecked', true, options);
|
|
|
|
await supplier.updateAttribute('payDay', 5, options);
|
|
|
|
const firstResult = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
|
|
|
|
await supplier.updateAttribute('isPayMethodChecked', true, options);
|
|
|
|
await supplier.updateAttribute('payDemFk', 1, options);
|
|
|
|
const secondResult = await models.Supplier.findOne({where: {id: 442}}, options);
|
|
|
|
|
|
|
|
expect(firstResult.isPayMethodChecked).toEqual(false);
|
|
|
|
expect(secondResult.isPayMethodChecked).toEqual(false);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2021-12-20 07:02:28 +00:00
|
|
|
});
|
2020-12-14 15:09:17 +00:00
|
|
|
});
|
|
|
|
});
|