const app = require('vn-loopback/server/server'); describe('chat sendCheckingPresence()', () => { const departmentId = 23; const workerId = 107; let timeEntry; afterAll(async done => { const department = await app.models.Department.findById(departmentId); await department.updateAttribute('chatName', null); await app.models.WorkerTimeControl.destroyById(timeEntry.id); done(); }); it(`should call to send() method with the worker username when no department channel is specified and then return a "Fake notification sent" as response`, async() => { const ctx = {req: {accessToken: {userId: 1}}}; const chatModel = app.models.Chat; spyOn(chatModel, 'send').and.callThrough(); const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); expect(response.statusCode).toEqual(200); expect(response.message).toEqual('Fake notification sent'); expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something'); }); it(`should call to send() method with the worker department channel if is specified and then return a "Fake notification sent" as response`, async() => { const ctx = {req: {accessToken: {userId: 1}}}; const chatModel = app.models.Chat; spyOn(chatModel, 'send').and.callThrough(); const department = await app.models.Department.findById(departmentId); await department.updateAttribute('chatName', 'cooler'); const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); expect(response.statusCode).toEqual(200); expect(response.message).toEqual('Fake notification sent'); expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', 'I changed something'); }); it(`should call to send() method with the worker username when the worker is working`, async() => { const ctx = {req: {accessToken: {userId: 1}}}; const chatModel = app.models.Chat; spyOn(chatModel, 'send').and.callThrough(); const today = new Date(); today.setHours(6, 0); timeEntry = await app.models.WorkerTimeControl.create({ userFk: workerId, timed: today, manual: false, direction: 'in' }); const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); expect(response.statusCode).toEqual(200); expect(response.message).toEqual('Fake notification sent'); expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something'); }); });