Untitled

 avatar
unknown
plain_text
5 months ago
3.2 kB
11
Indexable
import { QuotesHandler } from './QuotesHandler';
import { LoggerInterface } from './LoggerInterface'; // Adjust based on your structure
import { LifeQuoteService } from './LifeQuoteService'; // Adjust based on your structure
import { Container } from 'typedi'; // Assuming you're using typedi for dependency injection
import { EventsConstant, Product } from './constants'; // Adjust based on your structure

jest.mock('./LoggerInterface');
jest.mock('./LifeQuoteService');
jest.mock('typedi', () => ({
    get: jest.fn(),
}));

describe('QuotesHandler', () => {
    let handler: QuotesHandler;
    let mockLogger: LoggerInterface;
    let mockLifeQuoteService: jest.Mocked<LifeQuoteService>;

    beforeEach(() => {
        mockLogger = new LoggerInterface() as jest.Mocked<LoggerInterface>;
        mockLifeQuoteService = new LifeQuoteService() as jest.Mocked<LifeQuoteService>;

        // Mock the Container.get to return the mocked LifeQuoteService
        (Container.get as jest.Mock).mockReturnValue(mockLifeQuoteService);

        handler = new QuotesHandler();
        handler['log'] = mockLogger; // Injecting the mocked logger
    });

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

    it('should process life insurance quotes successfully', async () => {
        const quotesData = { type: EventsConstant.LIFE_INSURANCE_OFFERS_CREATED };
        const expectedResult = { success: true };

        mockLifeQuoteService.processQuotesSuccess = jest.fn().mockResolvedValue(expectedResult);

        const result = await handler.processQuotes(quotesData);

        expect(mockLogger.info).toHaveBeenCalledWith(`Processing quotes ${quotesData.type}`);
        expect(mockLifeQuoteService.processQuotesSuccess).toHaveBeenCalledWith(quotesData);
        expect(result).toEqual(expectedResult);
    });

    it('should log info for unknown event types', async () => {
        const quotesData = { type: 'UNKNOWN_EVENT_TYPE' };

        const result = await handler.processQuotes(quotesData);

        expect(mockLogger.info).toHaveBeenCalledWith(`Processing quotes ${quotesData.type}`);
        expect(mockLogger.info).toHaveBeenCalledWith(`No handler for event type: ${quotesData.type}`);
        expect(result).toBeUndefined();
    });

    it('should log error when an exception occurs', async () => {
        const quotesData = { type: EventsConstant.LIFE_INSURANCE_OFFERS_CREATED };

        mockLifeQuoteService.processQuotesSuccess.mockImplementationOnce(() => {
            throw new Error('Processing error');
        });

        const result = await handler.processQuotes(quotesData);

        expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining('FAILURE_IN_PROCESSING_QUOTES'));
        expect(result).toBeUndefined();
    });

    it('should throw an error for unknown product type in getQuoteServiceObjectFromProduct', () => {
        const handlerWithInvalidProduct = new QuotesHandler();

        expect(() => {
            handlerWithInvalidProduct['getQuoteServiceObjectFromProduct']('INVALID_PRODUCT' as Product);
        }).toThrow('NO_QUOTE_HANDLER_FOUND_FOR_TYPE');
    });
});
Editor is loading...
Leave a Comment