From b87930fb36adbc8d17696871e7bb00c360ab3ba6 Mon Sep 17 00:00:00 2001 From: rayangler <27821750+rayangler@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:46:58 -0500 Subject: [PATCH] Testing --- tests/unit/job/stagingJobHandler.test.ts | 58 ++++++++++++++++++++++-- tests/utils/jobHandlerTestHelper.ts | 6 ++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/tests/unit/job/stagingJobHandler.test.ts b/tests/unit/job/stagingJobHandler.test.ts index f0955bae1..0b576d497 100644 --- a/tests/unit/job/stagingJobHandler.test.ts +++ b/tests/unit/job/stagingJobHandler.test.ts @@ -1,13 +1,22 @@ +import axios from 'axios'; import { TestDataProvider } from '../../data/data'; import { JobHandlerTestHelper } from '../../utils/jobHandlerTestHelper'; import { getStagingJobDef } from '../../data/jobDef'; +import { JobStatus } from '../../../src/entities/job'; describe('StagingJobHandler Tests', () => { let jobHandlerTestHelper: JobHandlerTestHelper; + let spyPost; beforeEach(() => { jobHandlerTestHelper = new JobHandlerTestHelper(); jobHandlerTestHelper.init('staging'); + spyPost = jest.spyOn(axios, 'post'); + }); + + afterEach(() => { + process.env.GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED = 'false'; + spyPost.mockClear(); }); test('Construct Production Handler', () => { @@ -82,9 +91,50 @@ describe('StagingJobHandler Tests', () => { expect(jobHandlerTestHelper.jobRepo.insertJob).toBeCalledTimes(0); }); - test('Staging deploy with Gatsby Cloud site does not result in job completion', async () => { - jobHandlerTestHelper.setStageForDeploySuccess(false, undefined, { hasGatsbySiteId: true }); - await jobHandlerTestHelper.jobHandler.execute(); - expect(jobHandlerTestHelper.jobRepo.updateWithStatus).toBeCalledTimes(0); + describe('Gatsby Cloud build hooks', () => { + beforeEach(() => { + process.env.GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED = 'true'; + }); + + test('Staging with Gatsby Cloud site does not result in immediate job completion', async () => { + jobHandlerTestHelper.setStageForDeploySuccess(false, undefined, { hasGatsbySiteId: true }); + await jobHandlerTestHelper.jobHandler.execute(); + // Post-build webhook is expected to update the status + expect(jobHandlerTestHelper.jobRepo.updateWithStatus).toBeCalledTimes(0); + }); + + test('Gatsby Cloud build hook fail results in job failure', async () => { + jobHandlerTestHelper.setStageForDeploySuccess(false, undefined, { hasGatsbySiteId: true }); + spyPost.mockImplementationOnce(() => Promise.reject({})); + await jobHandlerTestHelper.jobHandler.execute(); + expect(jobHandlerTestHelper.jobRepo.updateWithErrorStatus).toBeCalledTimes(1); + }); + }); + + describe('Netlify build hooks', () => { + beforeEach(() => { + process.env.GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED = 'true'; + }); + + test('Staging with Netlify does not result in immediate job completion with Gatsby Cloud', async () => { + jobHandlerTestHelper.setStageForDeploySuccess(false, undefined, { + hasGatsbySiteId: true, + hasNetlifyBuildHook: true, + }); + await jobHandlerTestHelper.jobHandler.execute(); + // Post-build webhook is expected to update the status + expect(jobHandlerTestHelper.jobRepo.updateWithStatus).toBeCalledTimes(0); + }); + + test('Netlify build hook error does not interfere with job execution', async () => { + jobHandlerTestHelper.setStageForDeploySuccess(false, undefined, { hasNetlifyBuildHook: true }); + spyPost.mockImplementationOnce(() => Promise.reject({})); + await jobHandlerTestHelper.jobHandler.execute(); + expect(jobHandlerTestHelper.jobRepo.updateWithStatus).toBeCalledWith( + expect.anything(), + undefined, + JobStatus.completed + ); + }); }); }); diff --git a/tests/utils/jobHandlerTestHelper.ts b/tests/utils/jobHandlerTestHelper.ts index ffcf6af5d..a6968ee4d 100644 --- a/tests/utils/jobHandlerTestHelper.ts +++ b/tests/utils/jobHandlerTestHelper.ts @@ -20,6 +20,7 @@ import { getBuildJobDef, getManifestJobDef, getStagingJobDef } from '../data/job type MockReturnValueOnce = { status: string; output: string; error: string | null }; type SetupOptions = { hasGatsbySiteId?: boolean; + hasNetlifyBuildHook?: boolean; }; export class JobHandlerTestHelper { @@ -88,10 +89,13 @@ export class JobHandlerTestHelper { this.setupForSuccess(); const publishOutput = TestDataProvider.getPublishOutputWithPurgedUrls(prodDeploy); - const { hasGatsbySiteId } = setupOptions; + const { hasGatsbySiteId, hasNetlifyBuildHook } = setupOptions; if (hasGatsbySiteId) { this.repoEntitlementsRepo.getGatsbySiteIdByGithubUsername.mockResolvedValue('gatsby_site_id'); } + if (hasNetlifyBuildHook) { + this.repoEntitlementsRepo.getNetlifyBuildHookByGithubUsername.mockResolvedValue('netlify_build_hook'); + } if (returnValue) { this.jobCommandExecutor.execute.mockResolvedValue(returnValue);