Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Oct 21, 2024
1 parent 1c17604 commit 9bfda32
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
57 changes: 57 additions & 0 deletions __tests__/functions/commit-safety-checks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {commitSafetyChecks} from '../../src/functions/commit-safety-checks'
import * as core from '@actions/core'

const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})

var data
var context
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(core, 'debug').mockImplementation(() => {})

context = {
payload: {
comment: {
created_at: '2024-10-15T12:00:00Z'
}
}
}

data = {
commit: {
author: {
date: '2024-10-15T11:00:00Z'
}
}
}
})

test('checks a commit and finds that it is safe (date)', async () => {
expect(await commitSafetyChecks(context, data)).toStrictEqual({
message: 'success',
status: true
})
expect(debugMock).toHaveBeenCalledWith(
'2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
)
})

test('checks a commit and finds that it is not safe (date)', async () => {
data.commit.author.date = '2024-10-15T12:00:01Z'

expect(await commitSafetyChecks(context, data)).toStrictEqual({
message:
'### ⚠️ Cannot proceed with deployment\n\nThe latest commit is not safe for deployment. It was authored after the trigger comment was created.',
status: false
})
expect(debugMock).toHaveBeenCalledWith(
'2024-10-15T12:00:00Z is older than 2024-10-15T12:00:01Z'
)
})

test('raises an error if the date format is invalid', async () => {
data.commit.author.date = '2024-10-15T12:00:uhoh'
await expect(commitSafetyChecks(context, data)).rejects.toThrow(
'Invalid date format. Please ensure the dates are valid UTC timestamps.'
)
})
15 changes: 9 additions & 6 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,15 @@ test('fails prechecks', async () => {
})

test('fails commitSafetyChecks', async () => {
jest.spyOn(commitSafetyChecks, 'commitSafetyChecks').mockImplementation(() => {
return {
status: false,
message: '### ⚠️ Cannot proceed with deployment... a scary commit was found',
}
})
jest
.spyOn(commitSafetyChecks, 'commitSafetyChecks')
.mockImplementation(() => {
return {
status: false,
message:
'### ⚠️ Cannot proceed with deployment... a scary commit was found'
}
})
jest.spyOn(actionStatus, 'actionStatus').mockImplementation(() => {
return undefined
})
Expand Down

0 comments on commit 9bfda32

Please sign in to comment.