Skip to content

Commit 9cd1e6b

Browse files
committed
tests: Update test coverage
1 parent 5eb4dc1 commit 9cd1e6b

File tree

13 files changed

+2312
-27
lines changed

13 files changed

+2312
-27
lines changed

__tests__/functions/deployment.test.js

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,253 @@ test('returns false if the deployment is not found', async () => {
314314

315315
expect(octokit.graphql).toHaveBeenCalled()
316316
})
317+
318+
test('returns null when no deployments are found with task parameter', async () => {
319+
octokit = createMockGraphQLOctokit({
320+
repository: {
321+
deployments: {
322+
nodes: []
323+
}
324+
}
325+
})
326+
327+
expect(
328+
await latestActiveDeployment(octokit, context, environment, 'backend')
329+
).toBeNull()
330+
331+
expect(octokit.graphql).toHaveBeenCalled()
332+
expect(core.debug).toHaveBeenCalledWith(
333+
'no deployments found for production with task backend'
334+
)
335+
})
336+
337+
test('returns active deployment with matching task on first page', async () => {
338+
const mockDataWithTask = {
339+
repository: {
340+
deployments: {
341+
nodes: [
342+
{
343+
createdAt: '2024-09-19T20:18:18Z',
344+
environment: 'production',
345+
updatedAt: '2024-09-19T20:18:21Z',
346+
id: 'DE_kwDOID9x8M5sC6QZ',
347+
payload: '{"type":"branch-deploy"}',
348+
state: 'ACTIVE',
349+
task: 'backend',
350+
creator: {
351+
login: 'github-actions'
352+
},
353+
ref: {
354+
name: 'main'
355+
},
356+
commit: {
357+
oid: '315cec138fc9d7dac8a47c6bba4217d3965ede3b'
358+
}
359+
},
360+
{
361+
createdAt: '2024-09-19T20:18:10Z',
362+
environment: 'production',
363+
updatedAt: '2024-09-19T20:18:15Z',
364+
id: 'DE_kwDOID9x8M5sC6QY',
365+
payload: '{"type":"branch-deploy"}',
366+
state: 'ACTIVE',
367+
task: 'frontend',
368+
creator: {
369+
login: 'github-actions'
370+
},
371+
ref: {
372+
name: 'main'
373+
},
374+
commit: {
375+
oid: 'abc123'
376+
}
377+
}
378+
],
379+
pageInfo: {
380+
endCursor: null,
381+
hasNextPage: false
382+
}
383+
}
384+
}
385+
}
386+
387+
octokit = createMockGraphQLOctokit(mockDataWithTask)
388+
389+
const result = await latestActiveDeployment(
390+
octokit,
391+
context,
392+
environment,
393+
'backend'
394+
)
395+
396+
expect(result).toStrictEqual({
397+
createdAt: '2024-09-19T20:18:18Z',
398+
environment: 'production',
399+
updatedAt: '2024-09-19T20:18:21Z',
400+
id: 'DE_kwDOID9x8M5sC6QZ',
401+
payload: '{"type":"branch-deploy"}',
402+
state: 'ACTIVE',
403+
task: 'backend',
404+
creator: {
405+
login: 'github-actions'
406+
},
407+
ref: {
408+
name: 'main'
409+
},
410+
commit: {
411+
oid: '315cec138fc9d7dac8a47c6bba4217d3965ede3b'
412+
}
413+
})
414+
415+
expect(octokit.graphql).toHaveBeenCalledTimes(1)
416+
expect(core.debug).toHaveBeenCalledWith(
417+
'found active deployment for production with task backend in page 1'
418+
)
419+
})
420+
421+
test('returns active deployment with matching task during pagination', async () => {
422+
octokit.graphql = jest
423+
.fn()
424+
.mockReturnValueOnce({
425+
repository: {
426+
deployments: {
427+
nodes: [
428+
{
429+
state: 'ACTIVE',
430+
task: 'frontend'
431+
},
432+
{
433+
state: 'INACTIVE',
434+
task: 'backend'
435+
}
436+
],
437+
pageInfo: {
438+
endCursor: 'cursor1',
439+
hasNextPage: true
440+
}
441+
}
442+
}
443+
})
444+
.mockReturnValueOnce({
445+
repository: {
446+
deployments: {
447+
nodes: [
448+
{
449+
state: 'INACTIVE',
450+
task: 'backend'
451+
},
452+
{
453+
state: 'ACTIVE',
454+
task: 'backend'
455+
}
456+
],
457+
pageInfo: {
458+
endCursor: 'cursor2',
459+
hasNextPage: false
460+
}
461+
}
462+
}
463+
})
464+
465+
const result = await latestActiveDeployment(
466+
octokit,
467+
context,
468+
environment,
469+
'backend'
470+
)
471+
472+
expect(result).toStrictEqual({
473+
state: 'ACTIVE',
474+
task: 'backend'
475+
})
476+
477+
expect(octokit.graphql).toHaveBeenCalledTimes(2)
478+
expect(core.debug).toHaveBeenCalledWith(
479+
'found active deployment for production with task backend in page 2'
480+
)
481+
})
482+
483+
test('returns null when no active deployment found after pagination with task filter', async () => {
484+
octokit.graphql = jest
485+
.fn()
486+
.mockReturnValueOnce({
487+
repository: {
488+
deployments: {
489+
nodes: [
490+
{
491+
state: 'ACTIVE',
492+
task: 'frontend'
493+
},
494+
{
495+
state: 'INACTIVE',
496+
task: 'backend'
497+
}
498+
],
499+
pageInfo: {
500+
endCursor: 'cursor1',
501+
hasNextPage: true
502+
}
503+
}
504+
}
505+
})
506+
.mockReturnValueOnce({
507+
repository: {
508+
deployments: {
509+
nodes: [
510+
{
511+
state: 'INACTIVE',
512+
task: 'backend'
513+
},
514+
{
515+
state: 'ACTIVE',
516+
task: 'frontend'
517+
}
518+
],
519+
pageInfo: {
520+
endCursor: 'cursor2',
521+
hasNextPage: true
522+
}
523+
}
524+
}
525+
})
526+
.mockReturnValueOnce({
527+
repository: {
528+
deployments: {
529+
nodes: [
530+
{
531+
state: 'PENDING',
532+
task: 'backend'
533+
},
534+
{
535+
state: 'ACTIVE',
536+
task: 'frontend'
537+
}
538+
],
539+
pageInfo: {
540+
endCursor: null,
541+
hasNextPage: false
542+
}
543+
}
544+
}
545+
})
546+
547+
const result = await latestActiveDeployment(
548+
octokit,
549+
context,
550+
environment,
551+
'backend'
552+
)
553+
554+
expect(result).toBeNull()
555+
556+
expect(octokit.graphql).toHaveBeenCalledTimes(3)
557+
expect(core.debug).toHaveBeenCalledWith(
558+
'no active deployment found for production with task backend in page 2'
559+
)
560+
expect(core.debug).toHaveBeenCalledWith(
561+
'no active deployment found for production with task backend in page 3'
562+
)
563+
expect(core.debug).toHaveBeenCalledWith(
564+
'no active deployment found for production with task backend after 3 pages'
565+
)
566+
})

__tests__/functions/environment-targets.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,3 +1145,94 @@ test('checks the comment body on a lock info request and uses the development en
11451145
'found environment target for lock request: development'
11461146
)
11471147
})
1148+
1149+
test('checks the comment body and parses --task flag', async () => {
1150+
expect(
1151+
await environmentTargets(
1152+
environment,
1153+
'.deploy to development --task frontend',
1154+
trigger,
1155+
noop_trigger,
1156+
stable_branch
1157+
)
1158+
).toStrictEqual({
1159+
environment: 'development',
1160+
environmentUrl: null,
1161+
environmentObj: {
1162+
target: 'development',
1163+
noop: false,
1164+
stable_branch_used: false,
1165+
params: null,
1166+
parsed_params: null,
1167+
sha: null,
1168+
task: 'frontend'
1169+
}
1170+
})
1171+
expect(infoMock).toHaveBeenCalledWith(
1172+
`📋 detected task in command: ${COLORS.highlight}frontend${COLORS.reset}`
1173+
)
1174+
expect(debugMock).toHaveBeenCalledWith(
1175+
"found environment target for branch deploy (with 'to'): development"
1176+
)
1177+
})
1178+
1179+
test('checks the comment body and parses --task flag with params', async () => {
1180+
expect(
1181+
await environmentTargets(
1182+
environment,
1183+
'.deploy to development --task backend | something1 something2',
1184+
trigger,
1185+
noop_trigger,
1186+
stable_branch
1187+
)
1188+
).toStrictEqual({
1189+
environment: 'development',
1190+
environmentUrl: null,
1191+
environmentObj: {
1192+
target: 'development',
1193+
noop: false,
1194+
stable_branch_used: false,
1195+
params: 'something1 something2',
1196+
parsed_params: {_: ['something1', 'something2']},
1197+
sha: null,
1198+
task: 'backend'
1199+
}
1200+
})
1201+
expect(infoMock).toHaveBeenCalledWith(
1202+
`📋 detected task in command: ${COLORS.highlight}backend${COLORS.reset}`
1203+
)
1204+
expect(infoMock).toHaveBeenCalledWith(
1205+
`🧮 detected parameters in command: ${COLORS.highlight}something1 something2`
1206+
)
1207+
})
1208+
1209+
test('checks the comment body with malformed --task flag (no value) and environment check fails', async () => {
1210+
expect(
1211+
await environmentTargets(
1212+
environment,
1213+
'.deploy production --task',
1214+
trigger,
1215+
noop_trigger,
1216+
stable_branch
1217+
)
1218+
).toStrictEqual({
1219+
environment: false,
1220+
environmentUrl: null,
1221+
environmentObj: {
1222+
target: false,
1223+
noop: null,
1224+
stable_branch_used: null,
1225+
params: null,
1226+
parsed_params: null,
1227+
sha: null,
1228+
task: null
1229+
}
1230+
})
1231+
expect(warningMock).toHaveBeenCalledWith(
1232+
expect.stringContaining('No matching environment target found')
1233+
)
1234+
expect(infoMock).not.toHaveBeenCalledWith(
1235+
expect.stringContaining('📋 detected task in command:')
1236+
)
1237+
expect(saveStateMock).toHaveBeenCalledWith('bypass', 'true')
1238+
})

0 commit comments

Comments
 (0)