diff --git a/asset-transfer-basic/rest-api-typescript/src/jobs.spec.ts b/asset-transfer-basic/rest-api-typescript/src/jobs.spec.ts index 93e83f8b..1d8b7fe2 100644 --- a/asset-transfer-basic/rest-api-typescript/src/jobs.spec.ts +++ b/asset-transfer-basic/rest-api-typescript/src/jobs.spec.ts @@ -4,29 +4,54 @@ import { Job, Queue } from 'bullmq'; import { + addSubmitTransactionJob, getJobCounts, getJobSummary, processSubmitTransactionJob, JobNotFoundError, + updateJobData, } from './jobs'; import { Contract, Transaction } from 'fabric-network'; import { mock, MockProxy } from 'jest-mock-extended'; import { Application } from 'express'; -describe('initJobQueue', () => { - it.todo('write tests'); -}); - -describe('initJobQueueWorker', () => { - it.todo('write tests'); -}); - -describe('initJobQueueScheduler', () => { - it.todo('write tests'); -}); - describe('addSubmitTransactionJob', () => { - it.todo('write tests'); + let mockJob: MockProxy; + let mockQueue: MockProxy; + + beforeEach(() => { + mockJob = mock(); + mockQueue = mock(); + mockQueue.add.mockResolvedValue(mockJob); + }); + + it('returns the new job ID', async () => { + mockJob.id = 'mockJobId'; + + const jobid = await addSubmitTransactionJob( + mockQueue, + 'mockMspId', + 'txn', + 'arg1', + 'arg2' + ); + + expect(jobid).toBe('mockJobId'); + }); + + it('throws an error if there is no job ID', async () => { + mockJob.id = undefined; + + await expect(async () => { + await addSubmitTransactionJob( + mockQueue, + 'mockMspId', + 'txn', + 'arg1', + 'arg2' + ); + }).rejects.toThrowError('Submit transaction job ID not available'); + }); }); describe('getJobSummary', () => { @@ -133,8 +158,40 @@ describe('getJobSummary', () => { }); }); -describe('updateSubmitTransactionJobStateData', () => { - it.todo('write tests'); +describe('updateJobData', () => { + let mockJob: MockProxy; + + beforeEach(() => { + mockJob = mock(); + mockJob.data = { + transactionIds: ['txn1'], + }; + }); + + it('stores the serialized state in the job data if a transaction is specified', async () => { + const mockSavedState = Buffer.from('MOCK SAVED STATE'); + const mockTransaction = mock(); + mockTransaction.getTransactionId.mockReturnValue('txn2'); + mockTransaction.serialize.mockReturnValue(mockSavedState); + + await updateJobData(mockJob, mockTransaction); + + expect(mockJob.update).toBeCalledTimes(1); + expect(mockJob.update).toBeCalledWith({ + transactionIds: ['txn1', 'txn2'], + transactionState: mockSavedState, + }); + }); + + it('removes the serialized state from the job data if a transaction is not specified', async () => { + await updateJobData(mockJob, undefined); + + expect(mockJob.update).toBeCalledTimes(1); + expect(mockJob.update).toBeCalledWith({ + transactionIds: ['txn1'], + transactionState: undefined, + }); + }); }); describe('getJobCounts', () => { diff --git a/asset-transfer-basic/rest-api-typescript/src/jobs.ts b/asset-transfer-basic/rest-api-typescript/src/jobs.ts index c46af0e5..64307982 100644 --- a/asset-transfer-basic/rest-api-typescript/src/jobs.ts +++ b/asset-transfer-basic/rest-api-typescript/src/jobs.ts @@ -53,7 +53,7 @@ const connection: ConnectionOptions = { }; /* - * Set up the queue for submit jobs + * Set up the queue for submit jobs */ export const initJobQueue = (): Queue => { const submitQueue = new Queue(config.JOB_QUEUE_NAME, { @@ -276,7 +276,7 @@ export const updateJobData = async ( /* * Gets a job summary - * + * * This function is used for the jobs REST endpoint */ export const getJobSummary = async (