Skip to content

Commit

Permalink
Merge pull request #275 from github/test-fixes
Browse files Browse the repository at this point in the history
Test Fixes / Improvements
  • Loading branch information
GrantBirki authored Jul 6, 2024
2 parents 2d8b8bc + 6b82fa4 commit 5ef8752
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 45 deletions.
8 changes: 8 additions & 0 deletions __tests__/functions/identical-commit-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ beforeEach(() => {
}),
listDeployments: jest.fn().mockReturnValue({
data: [
{
sha: 'deadbeef',
id: 123395608,
created_at: '2023-02-01T21:30:40Z',
payload: {
type: 'some-other-type'
}
},
{
sha: 'beefdead',
id: 785395609,
Expand Down
15 changes: 14 additions & 1 deletion __tests__/functions/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const validStates = {
deployment_id: '456',
environment: 'production',
token: 'test-token',
approved_reviews_count: '1'
approved_reviews_count: '1',
environment_url: 'https://example.com'
}

const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(() => {})
Expand Down Expand Up @@ -59,6 +60,18 @@ test('successfully runs post() Action logic', async () => {
expect(await post()).toBeUndefined()
})

test('successfully runs post() Action logic when environment_url is not defined', async () => {
const noEnvironmentUrl = {
environment_url: null
}

jest.spyOn(core, 'getState').mockImplementation(name => {
return noEnvironmentUrl[name]
})

expect(await post()).toBeUndefined()
})

test('exits due to an invalid Actions context', async () => {
jest.spyOn(contextCheck, 'contextCheck').mockImplementation(() => {
return false
Expand Down
54 changes: 27 additions & 27 deletions __tests__/functions/prechecks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ beforeEach(() => {
reviewDecision: 'APPROVED',
mergeStateStatus: 'CLEAN',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -161,7 +161,7 @@ test('runs prechecks and finds that the IssueOps command is valid for a branch d
reviewDecision: 'APPROVED',
mergeStateStatus: 'CLEAN',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -318,7 +318,7 @@ test('runs prechecks and finds CI checks pass but reviews are not defined', asyn
pullRequest: {
reviewDecision: null,
reviews: {
total: 0
totalCount: 0
},
commits: {
nodes: [
Expand Down Expand Up @@ -356,7 +356,7 @@ test('runs prechecks and finds CI is passing and the PR has not been reviewed BU
pullRequest: {
reviewDecision: 'REVIEW_REQUIRED',
reviews: {
total: 0
totalCount: 0
},
commits: {
nodes: [
Expand Down Expand Up @@ -393,7 +393,7 @@ test('runs prechecks and finds that the IssueOps command is valid for a branch d
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -443,7 +443,7 @@ test('runs prechecks and finds that the IssueOps command is on a PR from a forke
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 4
totalCount: 4
},
commits: {
nodes: [
Expand Down Expand Up @@ -492,7 +492,7 @@ test('runs prechecks and finds CI is pending and the PR has not been reviewed BU
pullRequest: {
reviewDecision: 'REVIEW_REQUIRED',
reviews: {
total: 0
totalCount: 0
},
commits: {
nodes: [
Expand Down Expand Up @@ -527,7 +527,7 @@ test('runs prechecks and finds CI checks are pending, the PR has not been review
pullRequest: {
reviewDecision: 'REVIEW_REQUIRED',
reviews: {
total: 0
totalCount: 0
},
commits: {
nodes: [
Expand Down Expand Up @@ -559,7 +559,7 @@ test('runs prechecks and finds CI is pending and reviewers have not been defined
pullRequest: {
reviewDecision: null,
reviews: {
total: 0
totalCount: 0
},
commits: {
nodes: [
Expand Down Expand Up @@ -591,7 +591,7 @@ test('runs prechecks and finds CI checked have not been defined, the PR has not
pullRequest: {
reviewDecision: 'REVIEW_REQUIRED',
reviews: {
total: 0
totalCount: 0
}
}
}
Expand All @@ -614,7 +614,7 @@ test('runs prechecks and deploys to the stable branch', async () => {
pullRequest: {
reviewDecision: null,
reviews: {
total: 0
totalCount: 0
}
}
}
Expand All @@ -640,7 +640,7 @@ test('runs prechecks and finds the PR has been approved but CI checks are pendin
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -701,7 +701,7 @@ test('runs prechecks and finds the PR is approved but CI is failing', async () =
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -733,7 +733,7 @@ test('runs prechecks and finds the PR is approved but CI is failing', async () =
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -828,7 +828,7 @@ test('runs prechecks and finds the PR is approved and CI checks have NOT been de
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
}
}
}
Expand All @@ -848,7 +848,7 @@ test('runs prechecks and finds the PR is behind the stable branch and a noop dep
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -896,7 +896,7 @@ test('runs prechecks and finds the PR is un-mergable and a noop deploy', async (
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'DIRTY',
commits: {
Expand Down Expand Up @@ -933,7 +933,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and it fails t
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -981,7 +981,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and it hits an
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -1024,7 +1024,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and update_bra
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ test('runs prechecks and finds the PR is a DRAFT PR and a noop deploy', async ()
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BLOCKED',
commits: {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ test('runs prechecks and finds the PR is a DRAFT PR and from an allowed environm
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'CLEAN',
commits: {
Expand Down Expand Up @@ -1177,7 +1177,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and the commit
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -1214,7 +1214,7 @@ test('runs prechecks and finds the PR is BEHIND and a full deploy and update_bra
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -1254,7 +1254,7 @@ test('runs prechecks and finds the PR is behind the stable branch and a full dep
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
mergeStateStatus: 'BEHIND',
commits: {
Expand Down Expand Up @@ -1500,7 +1500,7 @@ test('runs prechecks and finds that no CI checks exist but reviews are defined a
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down Expand Up @@ -1571,7 +1571,7 @@ test('runs prechecks and finds that skip_ci is set and the PR has been approved'
pullRequest: {
reviewDecision: 'APPROVED',
reviews: {
total: 1
totalCount: 1
},
commits: {
nodes: [
Expand Down
44 changes: 42 additions & 2 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ beforeEach(() => {
process.env.INPUT_GITHUB_TOKEN = 'faketoken'
process.env.INPUT_TRIGGER = '.deploy'
process.env.INPUT_REACTION = 'eyes'
process.env.INPUT_UPDATE_BRANCH = 'warn'
process.env.INPUT_ENVIRONMENT = 'production'
process.env.INPUT_ENVIRONMENT_TARGETS = 'production,development,staging'
process.env.INPUT_ENVIRONMENT_URLS = ''
Expand Down Expand Up @@ -114,7 +115,8 @@ beforeEach(() => {
ref: 'test-ref',
status: true,
message: '✔️ PR is approved and all CI checks passed - OK',
noopMode: false
noopMode: false,
sha: null
}
})
})
Expand All @@ -139,7 +141,8 @@ test('successfully runs the action', async () => {
expect(debugMock).toHaveBeenCalledWith('production_environment: true')
})

test('successfully runs the action on a deployment to development', async () => {
test('successfully runs the action on a deployment to development and with branch updates disabled', async () => {
process.env.INPUT_UPDATE_BRANCH = 'disabled'
github.context.payload.comment.body = '.deploy to development'

expect(await run()).toBe('success')
Expand Down Expand Up @@ -595,6 +598,43 @@ test('runs with a naked command when naked commands are NOT allowed', async () =
expect(saveStateMock).toHaveBeenCalledWith('bypass', 'true')
})

test('successfully runs the action on a deployment to an exact sha in development with params', async () => {
process.env.INPUT_ALLOW_SHA_DEPLOYMENTS = 'true'
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
return {
ref: 'test-ref',
status: true,
message: '✔️ PR is approved and all CI checks passed - OK',
noopMode: false,
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
}
})

github.context.payload.comment.body =
'.deploy 82c238c277ca3df56fe9418a5913d9188eafe3bc development | something1 something2 something3'

expect(await run()).toBe('success')
expect(setOutputMock).toHaveBeenCalledWith('deployment_id', 123)
expect(setOutputMock).toHaveBeenCalledWith(
'comment_body',
'.deploy 82c238c277ca3df56fe9418a5913d9188eafe3bc development | something1 something2 something3'
)
expect(setOutputMock).toHaveBeenCalledWith('triggered', 'true')
expect(setOutputMock).toHaveBeenCalledWith('comment_id', 123)
expect(setOutputMock).toHaveBeenCalledWith('ref', 'test-ref')
expect(setOutputMock).toHaveBeenCalledWith('noop', false)
expect(setOutputMock).toHaveBeenCalledWith('continue', 'true')
expect(saveStateMock).toHaveBeenCalledWith('isPost', 'true')
expect(saveStateMock).toHaveBeenCalledWith('actionsToken', 'faketoken')
expect(saveStateMock).toHaveBeenCalledWith('environment', 'development')
expect(saveStateMock).toHaveBeenCalledWith('comment_id', 123)
expect(saveStateMock).toHaveBeenCalledWith('ref', 'test-ref')
expect(saveStateMock).toHaveBeenCalledWith('noop', false)
expect(setOutputMock).toHaveBeenCalledWith('type', 'deploy')
expect(saveStateMock).toHaveBeenCalledWith('deployment_id', 123)
expect(debugMock).toHaveBeenCalledWith('production_environment: false')
})

test('successfully runs the action after trimming the body', async () => {
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
return {
Expand Down
Loading

0 comments on commit 5ef8752

Please sign in to comment.