import { cron } from './cron' import { api } from '../api' import { database } from '../database' import { Batch, EventLogType } from '../types' const TIME_INTERVAL = 1000 describe('Cron', () => { let spy: jest.SpyInstance beforeEach(async () => { // restore the spy created with spyOn jest.restoreAllMocks() spy = jest.spyOn(api, 'sendBatch') await database.reset() }) it('should send a batch of items to the API if there are items in the database', async () => { const batch: Batch = { source: 'test', event: 'testEvent', description: 'Test event', original_created_at: new Date().toISOString(), type: EventLogType.EVENT, } database.setItem(batch) await cron.sendBatch() expect(spy).toHaveBeenCalledWith([batch]) }) it('should not send a batch of items to the API if there are no items in the database', async () => { await cron.sendBatch() expect(spy).not.toHaveBeenCalled() }) it('should stop the interval when stop is called', () => { jest.useFakeTimers() cron.init(TIME_INTERVAL) cron.stop() expect(cron.getInterval()).toBeUndefined() }) it('should start the interval when init is called', () => { jest.useFakeTimers() cron.init(TIME_INTERVAL) expect(cron.getInterval()).toBeDefined() }) })