salix/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js

93 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-12-21 11:50:28 +00:00
const app = require(`${serviceRoot}/server/server`);
2018-10-25 09:50:18 +00:00
describe('ticket changeState()', () => {
let ticket;
2019-01-22 09:04:42 +00:00
beforeAll(async() => {
2018-10-25 09:50:18 +00:00
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
originalTicket.id = null;
ticket = await app.models.Ticket.create(originalTicket);
});
2019-01-22 09:04:42 +00:00
afterAll(async() => {
2018-10-25 09:50:18 +00:00
await app.models.Ticket.destroyById(ticket.id);
});
2019-01-22 09:04:42 +00:00
it('should throw an error if the ticket is not editable and the user isnt production', async() => {
let ctx = {req: {accessToken: {userId: 18}}};
let params = {ticketFk: 2, stateFk: 3};
let error;
try {
await app.models.TicketTracking.changeState(ctx, params);
} catch (e) {
error = e;
}
expect(error).toEqual(new Error(`You don't have enough privileges to change the state of this ticket`));
});
2019-01-22 09:04:42 +00:00
it('should throw an error if the state is assigned and theres not worker in params', async() => {
let ctx = {req: {accessToken: {userId: 18}}};
let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
let params = {ticketFk: 11, stateFk: assignedState.id};
let error;
try {
await app.models.TicketTracking.changeState(ctx, params);
} catch (e) {
error = e;
}
expect(error.message).toEqual('La instancia `TicketTracking` no es válida. Detalles: `workerFk` Worker cannot be blank (value: undefined).');
});
it('should throw an error if a worker thats not production tries to change the state to one thats not assigned', async() => {
let ctx = {req: {accessToken: {userId: 110}}};
let params = {ticketFk: 11, stateFk: 3};
let error;
try {
await app.models.TicketTracking.changeState(ctx, params);
} catch (e) {
error = e;
}
expect(error.message).toEqual(`You don't have enough privileges to change the state of this ticket`);
});
it('should be able to create a ticket tracking line for a not editable ticket if the user has the production role', async() => {
let ctx = {req: {accessToken: {userId: 49}}};
let params = {ticketFk: ticket.id, stateFk: 3};
let res = await app.models.TicketTracking.changeState(ctx, params);
expect(res.__data.ticketFk).toBe(params.ticketFk);
expect(res.__data.stateFk).toBe(params.stateFk);
2019-01-22 09:04:42 +00:00
expect(res.__data.workerFk).toBe(49);
expect(res.__data.id).toBeDefined();
});
2019-01-22 09:04:42 +00:00
it('return an array with the created ticket tracking line', async() => {
let ctx = {req: {accessToken: {userId: 49}}};
2018-10-25 09:50:18 +00:00
let params = {ticketFk: ticket.id, stateFk: 3};
let res = await app.models.TicketTracking.changeState(ctx, params);
expect(res.__data.ticketFk).toBe(params.ticketFk);
expect(res.__data.stateFk).toBe(params.stateFk);
2019-01-22 09:04:42 +00:00
expect(res.__data.workerFk).toBe(49);
expect(res.__data.id).toBeDefined();
});
it('return an array with the created ticket tracking line when the user is salesperson, uses the state assigned and thes a workerFk given', async() => {
let ctx = {req: {accessToken: {userId: 18}}};
let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
let params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1};
let res = await app.models.TicketTracking.changeState(ctx, params);
expect(res.__data.ticketFk).toBe(params.ticketFk);
expect(res.__data.stateFk).toBe(params.stateFk);
expect(res.__data.workerFk).toBe(params.workerFk);
expect(res.__data.workerFk).toBe(1);
expect(res.__data.id).toBeDefined();
});
});