From ea1380d9781b987abd556ef9d0714fc691e90090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Guzm=C3=A1n?= Date: Mon, 28 Nov 2022 19:06:08 +0100 Subject: [PATCH] Add unit tests --- test/getReviewers.spec.ts | 42 ++++++++++++++++ test/submitTimesheet.spec.ts | 94 ++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 test/getReviewers.spec.ts create mode 100644 test/submitTimesheet.spec.ts diff --git a/test/getReviewers.spec.ts b/test/getReviewers.spec.ts new file mode 100644 index 0000000..b0b3d14 --- /dev/null +++ b/test/getReviewers.spec.ts @@ -0,0 +1,42 @@ +// @ts-ignore TS6059 +// Only for test purpose, isn't compiled to js sources +import { mockCurrentDate } from './mocks/currentDate' + +import api from '../src/api/api' +import timesheets from '../src/timesheets/timesheets' +import authenticator from '../src/config/authenticator' + +jest.mock('../src/config/configStore', () => jest.requireActual('./mocks/configStore')) + +afterEach(() => { jest.clearAllMocks() }) + +authenticator.saveCredentials({ + accountId: 'fakeAccountId', + tempoToken: 'fakeToken' +}) + +describe('gets reviewers', () => { + const getReviewersMock = jest.fn() + .mockReturnValue([ + { + accountId: "123456", + displayName: "First Reviewer" + }, + { + accountId: "456789", + displayName: "Second Reviewer" + } + ]) + api.getReviewers = getReviewersMock + api.getUserSchedule = jest.fn() + + mockCurrentDate(new Date('2020-02-28T12:00:00.000+01:00')) + + describe('gets reviewers', () => { + test('getReviewers', async () => { + await timesheets.getReviewers() + + expect(getReviewersMock).toHaveBeenCalled() + }) + }) +}) diff --git a/test/submitTimesheet.spec.ts b/test/submitTimesheet.spec.ts new file mode 100644 index 0000000..87949f0 --- /dev/null +++ b/test/submitTimesheet.spec.ts @@ -0,0 +1,94 @@ +// @ts-ignore TS6059 +// Only for test purpose, isn't compiled to js sources +import { mockCurrentDate } from './mocks/currentDate' + +import api from '../src/api/api' +import timesheets from '../src/timesheets/timesheets' +import authenticator from '../src/config/authenticator' +import aliases from '../src/config/aliases' + +jest.mock('../src/config/configStore', () => jest.requireActual('./mocks/configStore')) + +afterEach(() => { jest.clearAllMocks() }) + +authenticator.saveCredentials({ + accountId: 'fakeAccountId', + tempoToken: 'fakeToken' +}) + +describe('adds a worklog', () => { + const submitTimesheetMock = jest.fn() + .mockReturnValue({ reviewer: { + accountId: "123456", + displayName: "First Reviewer" + } }) + api.submitTimesheet = submitTimesheetMock + api.getUserSchedule = jest.fn() + + mockCurrentDate(new Date('2020-02-28T12:00:00.000+01:00')) + + describe('empty inputs', () => { + test('default to current week', async () => { + await timesheets.submitTimesheet({ + reviewerAccountId: "123456", + comment: "", + from: null, + to: null + }) + + expect(submitTimesheetMock).toHaveBeenCalledWith({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2020-02-24T12:00:00.000+01:00'), + to: new Date('2020-03-01T12:00:00.000+01:00') + }) + }), + test('to defaults to next sunday', async () => { + await timesheets.submitTimesheet({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2020-02-28T12:00:00.000+01:00'), + to: null + }) + + expect(submitTimesheetMock).toHaveBeenCalledWith({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2020-02-28T12:00:00.000+01:00'), + to: new Date('2020-03-01T12:00:00.000+01:00') + }) + }), + test('from defaults to a week before to', async () => { + await timesheets.submitTimesheet({ + reviewerAccountId: "123456", + comment: "", + from: null, + to: new Date('2022-11-26T12:00:00.000+01:00') + }) + + expect(submitTimesheetMock).toHaveBeenCalledWith({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2022-11-21T12:00:00.000+01:00'), + to: new Date('2022-11-26T12:00:00.000+01:00') + }) + }) + }) + describe('filled inputs', () => { + test('respect user inputs', async () => { + await timesheets.submitTimesheet({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2020-02-27T12:00:00.000+01:00'), + to: new Date('2020-02-29T12:00:00.000+01:00') + }) + + expect(submitTimesheetMock).toHaveBeenCalledWith({ + reviewerAccountId: "123456", + comment: "", + from: new Date('2020-02-27T12:00:00.000+01:00'), + to: new Date('2020-02-29T12:00:00.000+01:00') + }) + }) + }) +}) \ No newline at end of file