2022-04-12 05:56:03 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
describe('loopback model address', () => {
|
|
|
|
let createdAddressId;
|
2021-06-23 11:24:23 +00:00
|
|
|
const clientId = 1101;
|
2018-09-06 08:42:43 +00:00
|
|
|
|
2022-04-12 05:56:03 +00:00
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-11 17:38:10 +00:00
|
|
|
beforeAll(() => {
|
2022-04-12 05:56:03 +00:00
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-15 12:45:41 +00:00
|
|
|
afterAll(async() => {
|
2022-04-12 05:56:03 +00:00
|
|
|
const client = await models.Client.findById(clientId);
|
2018-08-21 12:59:04 +00:00
|
|
|
|
2022-04-12 05:56:03 +00:00
|
|
|
await models.Address.destroyById(createdAddressId);
|
2018-08-21 12:59:04 +00:00
|
|
|
await client.updateAttribute('isEqualizated', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('observe()', () => {
|
2018-09-06 08:42:43 +00:00
|
|
|
it('should throw an error when deactivating a consignee if its the default address', async() => {
|
2018-08-21 12:59:04 +00:00
|
|
|
let error;
|
2022-04-12 05:56:03 +00:00
|
|
|
const address = await models.Address.findById(1);
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
await address.updateAttribute('isActive', false)
|
|
|
|
.catch(e => {
|
|
|
|
error = e;
|
|
|
|
|
|
|
|
expect(error.message).toEqual('The default consignee can not be unchecked');
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set isEqualizated to true of a given Client to trigger any new address to have it', async() => {
|
2022-04-12 05:56:03 +00:00
|
|
|
const client = await models.Client.findById(clientId);
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
expect(client.isEqualizated).toBeFalsy();
|
|
|
|
|
|
|
|
await client.updateAttribute('isEqualizated', true);
|
|
|
|
|
2022-04-12 05:56:03 +00:00
|
|
|
const newAddress = await models.Address.create({
|
2020-01-28 08:03:20 +00:00
|
|
|
clientFk: clientId,
|
2018-08-21 12:59:04 +00:00
|
|
|
agencyModeFk: 5,
|
|
|
|
city: 'here',
|
|
|
|
isActive: true,
|
|
|
|
mobile: '555555555',
|
|
|
|
nickname: 'Test address',
|
|
|
|
phone: '555555555',
|
2019-09-19 07:17:21 +00:00
|
|
|
postalCode: '46000',
|
2018-08-21 12:59:04 +00:00
|
|
|
provinceFk: 1,
|
2020-01-28 08:03:20 +00:00
|
|
|
street: 'Test address',
|
|
|
|
incotermsFk: 'FAS',
|
|
|
|
customsAgentFk: 1
|
2018-08-21 12:59:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(newAddress.isEqualizated).toBeTruthy();
|
|
|
|
|
|
|
|
createdAddressId = newAddress.id;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|