Added model unit test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-03-24 12:45:26 +01:00
parent b3d333368b
commit 32805dbafb
2 changed files with 56 additions and 58 deletions

View File

@ -227,9 +227,10 @@ module.exports = Self => {
await Self.app.models.ClientCredit.create(newCredit);
}
ctx.hookState.oldInstance = Object.assign({}, orgData.__data);
ctx.hookState.newInstance = Object.assign({}, finalState);
if (orgData)
ctx.hookState.oldInstance = Object.assign({}, orgData.__data);
if (finalState)
ctx.hookState.newInstance = Object.assign({}, finalState);
});
Self.observe('after save', async ctx => {
@ -282,11 +283,11 @@ module.exports = Self => {
const workerIdAfter = newInstance.salesPersonFk;
const assignmentChanged = workerIdBefore != workerIdAfter;
if (assignmentChanged)
await notifyAssignment(instance, workerIdBefore, workerIdAfter);
await Self.notifyAssignment(instance, workerIdBefore, workerIdAfter);
});
// Send notification on client worker assignment
async function notifyAssignment(client, previousWorkerId, currentWorkerId) {
Self.notifyAssignment = async function notifyAssignment(client, previousWorkerId, currentWorkerId) {
const loopBackContext = LoopBackContext.getCurrentContext();
const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http.req;
@ -295,20 +296,22 @@ module.exports = Self => {
const origin = headers.origin;
const models = Self.app.models;
let previousWorkerName = $t('None');
let currentWorkerName = $t('None');
let previousWorker = {name: $t('None')};
let currentWorker = {name: $t('None')};
if (previousWorkerId) {
const worker = await models.Worker.findById(previousWorkerId, {
include: {relation: 'user'}
});
previousWorkerName = worker && worker.user().nickname;
previousWorker.user = worker && worker.user().name;
previousWorker.name = worker && worker.user().nickname;
}
if (currentWorkerId) {
const worker = await models.Worker.findById(currentWorkerId, {
include: {relation: 'user'}
});
currentWorkerName = worker && worker.user().nickname;
currentWorker.user = worker && worker.user().name;
currentWorker.name = worker && worker.user().nickname;
}
const fullUrl = `${origin}/#!/client/${client.id}/basic-data`;
@ -316,17 +319,16 @@ module.exports = Self => {
clientId: client.id,
clientName: client.name,
url: fullUrl,
previousWorkerName: previousWorkerName,
currentWorkerName: currentWorkerName
previousWorkerName: previousWorker.name,
currentWorkerName: currentWorker.name
});
if (previousWorkerName) // change to worker userName
await models.Chat.send(httpCtx, '@joan', message);
if (previousWorkerId)
await models.Chat.send(httpCtx, `@${previousWorker.user}`, message);
if (currentWorkerName)
await models.Chat.send(httpCtx, '@joan', message);
}
if (currentWorkerId)
await models.Chat.send(httpCtx, `@${currentWorker.user}`, message);
};
async function validateCreditChange(ctx, finalState) {
let models = Self.app.models;

View File

@ -1,58 +1,54 @@
const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('loopback model address', () => {
let createdAddressId;
const clientId = 101;
describe('Client Model', () => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'},
[`__`]: value => {
return value;
}
}
}
};
const ctx = {req: activeCtx};
const chatModel = app.models.Chat;
const client = {id: 101, name: 'Bruce Banner'};
const previousWorkerId = 106; // DavidCharlesHaller
const currentWorkerId = 107; // HankPym
afterAll(async done => {
let client = await app.models.Client.findById(clientId);
await app.models.Address.destroyById(createdAddressId);
await client.updateAttribute('isEqualizated', false);
done();
beforeEach(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
describe('observe()', () => {
it('should throw an error when deactivating a consignee if its the default address', async() => {
let error;
let address = await app.models.Address.findById(1);
describe('notifyAssignment()', () => {
it('should call to the Chat send() method for both workers', async() => {
spyOn(chatModel, 'send').and.callThrough();
await address.updateAttribute('isActive', false)
.catch(e => {
error = e;
await app.models.Client.notifyAssignment(client, previousWorkerId, currentWorkerId);
expect(error.message).toEqual('The default consignee can not be unchecked');
});
expect(error).toBeDefined();
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
});
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(clientId);
it('should call to the Chat send() method for the previous worker', async() => {
spyOn(chatModel, 'send').and.callThrough();
expect(client.isEqualizated).toBeFalsy();
await app.models.Client.notifyAssignment(client, null, currentWorkerId);
await client.updateAttribute('isEqualizated', true);
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
});
let newAddress = await app.models.Address.create({
clientFk: clientId,
agencyModeFk: 5,
city: 'here',
isActive: true,
mobile: '555555555',
nickname: 'Test address',
phone: '555555555',
postalCode: '46000',
provinceFk: 1,
street: 'Test address',
incotermsFk: 'FAS',
customsAgentFk: 1
});
it('should call to the Chat send() method for the current worker', async() => {
spyOn(chatModel, 'send').and.callThrough();
expect(newAddress.isEqualizated).toBeTruthy();
await app.models.Client.notifyAssignment(client, previousWorkerId, null);
createdAddressId = newAddress.id;
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
});
});
});