Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Víctor Guzmán committed Nov 30, 2022
1 parent 546de0c commit 828abf0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/getReviewers.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
94 changes: 94 additions & 0 deletions test/submitTimesheet.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
})

0 comments on commit 828abf0

Please sign in to comment.