Skip to content

Commit 5ef8752

Browse files
authored
Merge pull request #275 from github/test-fixes
Test Fixes / Improvements
2 parents 2d8b8bc + 6b82fa4 commit 5ef8752

File tree

8 files changed

+116
-45
lines changed

8 files changed

+116
-45
lines changed

__tests__/functions/identical-commit-check.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ beforeEach(() => {
5757
}),
5858
listDeployments: jest.fn().mockReturnValue({
5959
data: [
60+
{
61+
sha: 'deadbeef',
62+
id: 123395608,
63+
created_at: '2023-02-01T21:30:40Z',
64+
payload: {
65+
type: 'some-other-type'
66+
}
67+
},
6068
{
6169
sha: 'beefdead',
6270
id: 785395609,

__tests__/functions/post.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const validStates = {
2424
deployment_id: '456',
2525
environment: 'production',
2626
token: 'test-token',
27-
approved_reviews_count: '1'
27+
approved_reviews_count: '1',
28+
environment_url: 'https://example.com'
2829
}
2930

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

63+
test('successfully runs post() Action logic when environment_url is not defined', async () => {
64+
const noEnvironmentUrl = {
65+
environment_url: null
66+
}
67+
68+
jest.spyOn(core, 'getState').mockImplementation(name => {
69+
return noEnvironmentUrl[name]
70+
})
71+
72+
expect(await post()).toBeUndefined()
73+
})
74+
6275
test('exits due to an invalid Actions context', async () => {
6376
jest.spyOn(contextCheck, 'contextCheck').mockImplementation(() => {
6477
return false

__tests__/functions/prechecks.test.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ beforeEach(() => {
8282
reviewDecision: 'APPROVED',
8383
mergeStateStatus: 'CLEAN',
8484
reviews: {
85-
total: 1
85+
totalCount: 1
8686
},
8787
commits: {
8888
nodes: [
@@ -161,7 +161,7 @@ test('runs prechecks and finds that the IssueOps command is valid for a branch d
161161
reviewDecision: 'APPROVED',
162162
mergeStateStatus: 'CLEAN',
163163
reviews: {
164-
total: 1
164+
totalCount: 1
165165
},
166166
commits: {
167167
nodes: [
@@ -318,7 +318,7 @@ test('runs prechecks and finds CI checks pass but reviews are not defined', asyn
318318
pullRequest: {
319319
reviewDecision: null,
320320
reviews: {
321-
total: 0
321+
totalCount: 0
322322
},
323323
commits: {
324324
nodes: [
@@ -356,7 +356,7 @@ test('runs prechecks and finds CI is passing and the PR has not been reviewed BU
356356
pullRequest: {
357357
reviewDecision: 'REVIEW_REQUIRED',
358358
reviews: {
359-
total: 0
359+
totalCount: 0
360360
},
361361
commits: {
362362
nodes: [
@@ -393,7 +393,7 @@ test('runs prechecks and finds that the IssueOps command is valid for a branch d
393393
pullRequest: {
394394
reviewDecision: 'APPROVED',
395395
reviews: {
396-
total: 1
396+
totalCount: 1
397397
},
398398
commits: {
399399
nodes: [
@@ -443,7 +443,7 @@ test('runs prechecks and finds that the IssueOps command is on a PR from a forke
443443
pullRequest: {
444444
reviewDecision: 'APPROVED',
445445
reviews: {
446-
total: 4
446+
totalCount: 4
447447
},
448448
commits: {
449449
nodes: [
@@ -492,7 +492,7 @@ test('runs prechecks and finds CI is pending and the PR has not been reviewed BU
492492
pullRequest: {
493493
reviewDecision: 'REVIEW_REQUIRED',
494494
reviews: {
495-
total: 0
495+
totalCount: 0
496496
},
497497
commits: {
498498
nodes: [
@@ -527,7 +527,7 @@ test('runs prechecks and finds CI checks are pending, the PR has not been review
527527
pullRequest: {
528528
reviewDecision: 'REVIEW_REQUIRED',
529529
reviews: {
530-
total: 0
530+
totalCount: 0
531531
},
532532
commits: {
533533
nodes: [
@@ -559,7 +559,7 @@ test('runs prechecks and finds CI is pending and reviewers have not been defined
559559
pullRequest: {
560560
reviewDecision: null,
561561
reviews: {
562-
total: 0
562+
totalCount: 0
563563
},
564564
commits: {
565565
nodes: [
@@ -591,7 +591,7 @@ test('runs prechecks and finds CI checked have not been defined, the PR has not
591591
pullRequest: {
592592
reviewDecision: 'REVIEW_REQUIRED',
593593
reviews: {
594-
total: 0
594+
totalCount: 0
595595
}
596596
}
597597
}
@@ -614,7 +614,7 @@ test('runs prechecks and deploys to the stable branch', async () => {
614614
pullRequest: {
615615
reviewDecision: null,
616616
reviews: {
617-
total: 0
617+
totalCount: 0
618618
}
619619
}
620620
}
@@ -640,7 +640,7 @@ test('runs prechecks and finds the PR has been approved but CI checks are pendin
640640
pullRequest: {
641641
reviewDecision: 'APPROVED',
642642
reviews: {
643-
total: 1
643+
totalCount: 1
644644
},
645645
commits: {
646646
nodes: [
@@ -701,7 +701,7 @@ test('runs prechecks and finds the PR is approved but CI is failing', async () =
701701
pullRequest: {
702702
reviewDecision: 'APPROVED',
703703
reviews: {
704-
total: 1
704+
totalCount: 1
705705
},
706706
commits: {
707707
nodes: [
@@ -733,7 +733,7 @@ test('runs prechecks and finds the PR is approved but CI is failing', async () =
733733
pullRequest: {
734734
reviewDecision: 'APPROVED',
735735
reviews: {
736-
total: 1
736+
totalCount: 1
737737
},
738738
commits: {
739739
nodes: [
@@ -828,7 +828,7 @@ test('runs prechecks and finds the PR is approved and CI checks have NOT been de
828828
pullRequest: {
829829
reviewDecision: 'APPROVED',
830830
reviews: {
831-
total: 1
831+
totalCount: 1
832832
}
833833
}
834834
}
@@ -848,7 +848,7 @@ test('runs prechecks and finds the PR is behind the stable branch and a noop dep
848848
pullRequest: {
849849
reviewDecision: 'APPROVED',
850850
reviews: {
851-
total: 1
851+
totalCount: 1
852852
},
853853
mergeStateStatus: 'BEHIND',
854854
commits: {
@@ -896,7 +896,7 @@ test('runs prechecks and finds the PR is un-mergable and a noop deploy', async (
896896
pullRequest: {
897897
reviewDecision: 'APPROVED',
898898
reviews: {
899-
total: 1
899+
totalCount: 1
900900
},
901901
mergeStateStatus: 'DIRTY',
902902
commits: {
@@ -933,7 +933,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and it fails t
933933
pullRequest: {
934934
reviewDecision: 'APPROVED',
935935
reviews: {
936-
total: 1
936+
totalCount: 1
937937
},
938938
mergeStateStatus: 'BEHIND',
939939
commits: {
@@ -981,7 +981,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and it hits an
981981
pullRequest: {
982982
reviewDecision: 'APPROVED',
983983
reviews: {
984-
total: 1
984+
totalCount: 1
985985
},
986986
mergeStateStatus: 'BEHIND',
987987
commits: {
@@ -1024,7 +1024,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and update_bra
10241024
pullRequest: {
10251025
reviewDecision: 'APPROVED',
10261026
reviews: {
1027-
total: 1
1027+
totalCount: 1
10281028
},
10291029
mergeStateStatus: 'BEHIND',
10301030
commits: {
@@ -1065,7 +1065,7 @@ test('runs prechecks and finds the PR is a DRAFT PR and a noop deploy', async ()
10651065
pullRequest: {
10661066
reviewDecision: 'APPROVED',
10671067
reviews: {
1068-
total: 1
1068+
totalCount: 1
10691069
},
10701070
mergeStateStatus: 'BLOCKED',
10711071
commits: {
@@ -1124,7 +1124,7 @@ test('runs prechecks and finds the PR is a DRAFT PR and from an allowed environm
11241124
pullRequest: {
11251125
reviewDecision: 'APPROVED',
11261126
reviews: {
1127-
total: 1
1127+
totalCount: 1
11281128
},
11291129
mergeStateStatus: 'CLEAN',
11301130
commits: {
@@ -1177,7 +1177,7 @@ test('runs prechecks and finds the PR is BEHIND and a noop deploy and the commit
11771177
pullRequest: {
11781178
reviewDecision: 'APPROVED',
11791179
reviews: {
1180-
total: 1
1180+
totalCount: 1
11811181
},
11821182
mergeStateStatus: 'BEHIND',
11831183
commits: {
@@ -1214,7 +1214,7 @@ test('runs prechecks and finds the PR is BEHIND and a full deploy and update_bra
12141214
pullRequest: {
12151215
reviewDecision: 'APPROVED',
12161216
reviews: {
1217-
total: 1
1217+
totalCount: 1
12181218
},
12191219
mergeStateStatus: 'BEHIND',
12201220
commits: {
@@ -1254,7 +1254,7 @@ test('runs prechecks and finds the PR is behind the stable branch and a full dep
12541254
pullRequest: {
12551255
reviewDecision: 'APPROVED',
12561256
reviews: {
1257-
total: 1
1257+
totalCount: 1
12581258
},
12591259
mergeStateStatus: 'BEHIND',
12601260
commits: {
@@ -1500,7 +1500,7 @@ test('runs prechecks and finds that no CI checks exist but reviews are defined a
15001500
pullRequest: {
15011501
reviewDecision: 'APPROVED',
15021502
reviews: {
1503-
total: 1
1503+
totalCount: 1
15041504
},
15051505
commits: {
15061506
nodes: [
@@ -1571,7 +1571,7 @@ test('runs prechecks and finds that skip_ci is set and the PR has been approved'
15711571
pullRequest: {
15721572
reviewDecision: 'APPROVED',
15731573
reviews: {
1574-
total: 1
1574+
totalCount: 1
15751575
},
15761576
commits: {
15771577
nodes: [

__tests__/main.test.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ beforeEach(() => {
3636
process.env.INPUT_GITHUB_TOKEN = 'faketoken'
3737
process.env.INPUT_TRIGGER = '.deploy'
3838
process.env.INPUT_REACTION = 'eyes'
39+
process.env.INPUT_UPDATE_BRANCH = 'warn'
3940
process.env.INPUT_ENVIRONMENT = 'production'
4041
process.env.INPUT_ENVIRONMENT_TARGETS = 'production,development,staging'
4142
process.env.INPUT_ENVIRONMENT_URLS = ''
@@ -114,7 +115,8 @@ beforeEach(() => {
114115
ref: 'test-ref',
115116
status: true,
116117
message: '✔️ PR is approved and all CI checks passed - OK',
117-
noopMode: false
118+
noopMode: false,
119+
sha: null
118120
}
119121
})
120122
})
@@ -139,7 +141,8 @@ test('successfully runs the action', async () => {
139141
expect(debugMock).toHaveBeenCalledWith('production_environment: true')
140142
})
141143

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

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

601+
test('successfully runs the action on a deployment to an exact sha in development with params', async () => {
602+
process.env.INPUT_ALLOW_SHA_DEPLOYMENTS = 'true'
603+
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
604+
return {
605+
ref: 'test-ref',
606+
status: true,
607+
message: '✔️ PR is approved and all CI checks passed - OK',
608+
noopMode: false,
609+
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
610+
}
611+
})
612+
613+
github.context.payload.comment.body =
614+
'.deploy 82c238c277ca3df56fe9418a5913d9188eafe3bc development | something1 something2 something3'
615+
616+
expect(await run()).toBe('success')
617+
expect(setOutputMock).toHaveBeenCalledWith('deployment_id', 123)
618+
expect(setOutputMock).toHaveBeenCalledWith(
619+
'comment_body',
620+
'.deploy 82c238c277ca3df56fe9418a5913d9188eafe3bc development | something1 something2 something3'
621+
)
622+
expect(setOutputMock).toHaveBeenCalledWith('triggered', 'true')
623+
expect(setOutputMock).toHaveBeenCalledWith('comment_id', 123)
624+
expect(setOutputMock).toHaveBeenCalledWith('ref', 'test-ref')
625+
expect(setOutputMock).toHaveBeenCalledWith('noop', false)
626+
expect(setOutputMock).toHaveBeenCalledWith('continue', 'true')
627+
expect(saveStateMock).toHaveBeenCalledWith('isPost', 'true')
628+
expect(saveStateMock).toHaveBeenCalledWith('actionsToken', 'faketoken')
629+
expect(saveStateMock).toHaveBeenCalledWith('environment', 'development')
630+
expect(saveStateMock).toHaveBeenCalledWith('comment_id', 123)
631+
expect(saveStateMock).toHaveBeenCalledWith('ref', 'test-ref')
632+
expect(saveStateMock).toHaveBeenCalledWith('noop', false)
633+
expect(setOutputMock).toHaveBeenCalledWith('type', 'deploy')
634+
expect(saveStateMock).toHaveBeenCalledWith('deployment_id', 123)
635+
expect(debugMock).toHaveBeenCalledWith('production_environment: false')
636+
})
637+
598638
test('successfully runs the action after trimming the body', async () => {
599639
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
600640
return {

0 commit comments

Comments
 (0)