const models = require('vn-loopback/server/server').models; describe('state isEditable()', () => { it('should return false if the state is not editable by a specific role', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const salesPersonRole = 18; const onDeliveryState = 13; const ctx = {req: {accessToken: {userId: salesPersonRole}}}; const result = await models.State.isEditable(ctx, onDeliveryState, options); expect(result).toBe(false); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should return true if the state is editable by a specific role', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const salesPersonRole = 18; const asignedState = 20; const ctx = {req: {accessToken: {userId: salesPersonRole}}}; const result = await models.State.isEditable(ctx, asignedState, options); expect(result).toBe(true); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should return true again if the state is editable by a specific role', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const employeeRole = 1; const fixingState = 1; const ctx = {req: {accessToken: {userId: employeeRole}}}; const result = await models.State.isEditable(ctx, fixingState, options); expect(result).toBe(true); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should return false if the state is not editable for the given role', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const employeeRole = 1; const asignedState = 13; const ctx = {req: {accessToken: {userId: employeeRole}}}; const result = await models.State.isEditable(ctx, asignedState, options); expect(result).toBe(false); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should return true if the state is editable for the given role', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const productionRole = 49; const onDeliveryState = 13; const ctx = {req: {accessToken: {userId: productionRole}}}; const result = await models.State.isEditable(ctx, onDeliveryState, options); expect(result).toBe(true); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should return true if the ticket is editable, the role is salesPerson and the ticket state is printed', async() => { const tx = await models.State.beginTransaction({}); try { const options = {transaction: tx}; const salesPersonRole = 18; const printedState = 4; const okState = 3; const ctx = {req: {accessToken: {userId: salesPersonRole}}}; const canEditCurrent = await models.State.isEditable(ctx, printedState, options); const canAsignNew = await models.State.isEditable(ctx, okState, options); const result = canEditCurrent && canAsignNew; expect(result).toBe(true); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });