2019-01-24 08:08:28 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
describe('loopback model address', () => {
|
|
|
|
let createdAddressId;
|
2018-09-06 08:42:43 +00:00
|
|
|
|
2018-08-21 12:59:04 +00:00
|
|
|
afterAll(async() => {
|
2018-09-06 08:42:43 +00:00
|
|
|
let address = await app.models.Address.findById(1);
|
2018-08-21 12:59:04 +00:00
|
|
|
let client = await app.models.Client.findById(101);
|
|
|
|
|
|
|
|
await address.updateAttribute('isDefaultAddress', true);
|
|
|
|
await app.models.Address.destroyById(createdAddressId);
|
|
|
|
|
|
|
|
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;
|
2018-09-06 08:42:43 +00:00
|
|
|
let address = await app.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 isDefaultAddress to false of all the addresses for a given client', async() => {
|
2018-09-06 08:42:43 +00:00
|
|
|
let previousDefaultAddress = await app.models.Address.findById(1);
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
expect(previousDefaultAddress.isDefaultAddress).toBeTruthy();
|
|
|
|
|
|
|
|
let address = await app.models.Address.findById(121);
|
|
|
|
|
|
|
|
expect(address.isDefaultAddress).toBeFalsy();
|
|
|
|
|
|
|
|
let defaultAddress = await address.updateAttribute('isDefaultAddress', true);
|
|
|
|
|
|
|
|
expect(defaultAddress.isDefaultAddress).toBeTruthy();
|
|
|
|
|
2018-09-06 08:42:43 +00:00
|
|
|
let noLongerDefaultAddress = await app.models.Address.findById(1);
|
2018-08-21 12:59:04 +00:00
|
|
|
|
|
|
|
expect(noLongerDefaultAddress.isDefaultAddress).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set isEqualizated to true of a given Client to trigger any new address to have it', async() => {
|
|
|
|
let client = await app.models.Client.findById(101);
|
|
|
|
|
|
|
|
expect(client.isEqualizated).toBeFalsy();
|
|
|
|
|
|
|
|
await client.updateAttribute('isEqualizated', true);
|
|
|
|
|
|
|
|
let newAddress = await app.models.Address.create({
|
|
|
|
clientFk: 101,
|
|
|
|
agencyModeFk: 5,
|
|
|
|
city: 'here',
|
|
|
|
isActive: true,
|
|
|
|
isDefaultAddress: false,
|
|
|
|
mobile: '555555555',
|
|
|
|
nickname: 'Test address',
|
|
|
|
phone: '555555555',
|
|
|
|
postalCode: '00000',
|
|
|
|
provinceFk: 1,
|
|
|
|
street: 'Test address'
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(newAddress.isEqualizated).toBeTruthy();
|
|
|
|
|
|
|
|
createdAddressId = newAddress.id;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|