Skip to content

Commit

Permalink
[TM-1273] Implement tests for the delayed job controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Oct 23, 2024
1 parent 7bad1b7 commit 5c82c6e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
37 changes: 37 additions & 0 deletions apps/job-service/src/jobs/delayed-jobs.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DelayedJobsController } from './delayed-jobs.controller';
import { Test, TestingModule } from '@nestjs/testing';
import { NotFoundException } from '@nestjs/common';
import { DelayedJobFactory } from '@terramatch-microservices/database/factories';
import { Resource } from '@terramatch-microservices/common/util';

describe('DelayedJobsController', () => {
let controller: DelayedJobsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DelayedJobsController]
}).compile();

controller = module.get<DelayedJobsController>(DelayedJobsController);
});

afterEach(() => {
jest.restoreAllMocks();
})

it('should throw not found if the delayed job does not exist', async () => {
await expect(controller.findOne('asdf')).rejects
.toThrow(NotFoundException);
});

it('should return the job definition when the delayed job does exist', async () => {
const { uuid, statusCode, payload } = await DelayedJobFactory.create();
const result = await controller.findOne(uuid);
const resource = result.data as Resource;
expect(resource.type).toBe('delayedJobs');
expect(resource.id).toBe(uuid);
expect(resource.attributes.statusCode).toBe(statusCode);
// TODO This will need an update when `payload` has been converted to a jsonb field
expect(resource.attributes.payload).toBe(payload);
});
})
10 changes: 10 additions & 0 deletions libs/database/src/lib/factories/delayed-job.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FactoryGirl } from 'factory-girl-ts';
import { DelayedJob } from '../entities';

export const DelayedJobFactory = FactoryGirl.define(DelayedJob, async () => ({
uuid: crypto.randomUUID(),
status: 'succeeded',
statusCode: 200,
// TODO: this will need an update when `payload` has been converted to a jsonb field
payload: '{ "data": "test" }'
}));
1 change: 1 addition & 0 deletions libs/database/src/lib/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './delayed-job.factory';
export * from './organisation.factory';
export * from './user.factory';

0 comments on commit 5c82c6e

Please sign in to comment.