Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler committed Mar 1, 2024
1 parent 222eba8 commit b87930f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
58 changes: 54 additions & 4 deletions tests/unit/job/stagingJobHandler.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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
);
});
});
});
6 changes: 5 additions & 1 deletion tests/utils/jobHandlerTestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b87930f

Please sign in to comment.