diff --git a/free-sample-product-service/src/controllers/cart.controller.test.ts b/free-sample-product-service/src/controllers/cart.controller.test.ts deleted file mode 100644 index 818ecbc..0000000 --- a/free-sample-product-service/src/controllers/cart.controller.test.ts +++ /dev/null @@ -1,133 +0,0 @@ -import CustomError from '../errors/custom.error'; -import { Resource } from '../interfaces/resource.interface'; -import { readConfiguration } from '../utils/config.utils'; -import { isProductAvailable } from '../api/products'; -import { update, cartController } from './cart.controller'; - -// Mocking dependencies -jest.mock('../utils/config.utils'); -jest.mock('../api/products'); -jest.mock('../errors/custom.error'); - -describe('update', () => { - let mockResource: Resource; - - beforeEach(() => { - mockResource = { - obj: { - lineItems: [], - totalPrice: { - currencyCode: 'USD', - centAmount: 1000, - }, - }, - }; - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - it('should add line item and recalculate', async () => { - // Mocking the configuration read from utils - (readConfiguration as jest.Mock).mockReturnValue({ - freeSampleSku: 'mockSampleSku', - minCartValue: 100, - freeSampleChannelKey: 'mockChannelKey', - freeLineItemKey: 'mockLineItemKey', - }); - - // Mocking isProductAvailable to return true - (isProductAvailable as jest.Mock).mockResolvedValue(true); - - // Executing the update function - const result = await update(mockResource); - - // Expectations - expect(result?.statusCode).toBe(200); - expect(result?.actions).toHaveLength(2); - expect(result?.actions[0].action).toBe('addLineItem'); - expect(result?.actions[1].action).toBe('recalculate'); - }); - - it('should remove line item and recalculate', async () => { - // Mocking the configuration read from utils - (readConfiguration as jest.Mock).mockReturnValue({ - freeSampleSku: 'mockSampleSku', - minCartValue: 100, - freeSampleChannelKey: 'mockChannelKey', - freeLineItemKey: 'mockLineItemKey', - }); - - // Mocking isProductAvailable to return false - (isProductAvailable as jest.Mock).mockResolvedValue(false); - - // Executing the update function - const result = await update(mockResource); - - // Expectations - expect(result?.statusCode).toBe(200); - expect(result?.actions).toHaveLength(2); - expect(result?.actions[0].action).toBe('removeLineItem'); - expect(result?.actions[1].action).toBe('recalculate'); - }); - - it('should throw CustomError on error', async () => { - const mockError = new Error('Mocked error'); - - // Mocking the error thrown by isProductAvailable - (isProductAvailable as jest.Mock).mockRejectedValue(mockError); - - // Executing the update function - await expect(update(mockResource)).rejects.toThrowError(CustomError); - - // Expectations - expect(CustomError).toHaveBeenCalledWith( - 400, - expect.stringContaining('Internal server error on CartController') - ); - }); -}); - -describe('cartController', () => { - let mockResource: Resource; - - beforeEach(() => { - mockResource = { - obj: { - lineItems: [], - totalPrice: { - currencyCode: 'USD', - centAmount: 1000, - }, - }, - }; - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - it('should handle Update action', async () => { - // Mocking the response from update function - const mockUpdateData = { statusCode: 200, actions: [{ action: 'mockAction' }] }; - (update as jest.Mock).mockResolvedValue(mockUpdateData); - - // Executing the cartController function - const result = await cartController('Update', mockResource); - - // Expectations - expect(result).toEqual(mockUpdateData); - }); - - it('should throw CustomError for unrecognized action', async () => { - // Executing the cartController function with an invalid action - await expect(cartController('InvalidAction', mockResource)).rejects.toThrowError(CustomError); - - // Expectations - expect(CustomError).toHaveBeenCalledWith( - 500, - expect.stringContaining('Internal Server Error - Resource not recognized') - ); - }); -}); diff --git a/new-category-cleanup-job-app/src/controllers/job.controller.test.ts b/new-category-cleanup-job-app/src/controllers/job.controller.test.ts index cde252d..8a8fa2c 100644 --- a/new-category-cleanup-job-app/src/controllers/job.controller.test.ts +++ b/new-category-cleanup-job-app/src/controllers/job.controller.test.ts @@ -8,8 +8,8 @@ import { getCategoryByKey } from '../api/categories'; // Mocking dependencies jest.mock('../utils/config.utils'); jest.mock('../utils/logger.utils'); -jest.mock('../products/fetch.products'); -jest.mock('../categories/categories'); +jest.mock('../api/fetch.products'); +jest.mock('../api/categories'); describe('Job Controller', () => { let req: Request; diff --git a/new-product-event-app/src/connector/pre-undeploy.ts b/new-product-event-app/src/connector/pre-undeploy.ts index 538d687..594e5f6 100644 --- a/new-product-event-app/src/connector/pre-undeploy.ts +++ b/new-product-event-app/src/connector/pre-undeploy.ts @@ -13,7 +13,7 @@ export async function run(): Promise { await preUndeploy(); } catch (error) { assertError(error); - process.stderr.write(`Post-undeploy failed: ${error.message}\n`); + process.stderr.write(`Pre-undeploy failed: ${error.message}`); process.exitCode = 1; } } diff --git a/new-product-event-app/src/controllers/event.controller.test.ts b/new-product-event-app/src/controllers/event.controller.test.ts deleted file mode 100644 index 9765b58..0000000 --- a/new-product-event-app/src/controllers/event.controller.test.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { Request, Response } from 'express'; -import { post } from './event.controller'; -import { getCategoryByKey } from '../api/categories'; -import { getProductById } from '../api/products'; -import { readConfiguration } from '../utils/config.utils'; -import CustomError from '../errors/custom.error'; - -// Mocking dependencies -jest.mock('../api/categories'); -jest.mock('../api/products'); -jest.mock('../utils/config.utils'); -jest.mock('../errors/custom.error'); - -describe('post', () => { - let mockRequest: Partial; - let mockResponse: Partial; - const mockProductId = 'mockProductId'; - const mockCategoryId = 'mockCategoryId'; - - beforeEach(() => { - mockRequest = {}; - mockResponse = { - status: jest.fn().mockReturnThis(), - send: jest.fn(), - }; - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - it('should handle missing request body', async () => { - // Executing the post function - await expect(post(mockRequest as Request, mockResponse as Response)).rejects.toThrowError(CustomError); - - // Expectations - expect(mockResponse.status).toHaveBeenCalledWith(400); - expect(mockResponse.send).toHaveBeenCalledWith('Bad request: No Pub/Sub message was received'); - }); - - it('should handle missing message', async () => { - mockRequest.body = {}; - - // Executing the post function - await expect(post(mockRequest as Request, mockResponse as Response)).rejects.toThrowError(CustomError); - - // Expectations - expect(mockResponse.status).toHaveBeenCalledWith(400); - expect(mockResponse.send).toHaveBeenCalledWith('Bad request: Wrong No Pub/Sub message format'); - }); - - it('should handle missing product id', async () => { - mockRequest.body = { message: { data: null } }; - - // Executing the post function - await expect(post(mockRequest as Request, mockResponse as Response)).rejects.toThrowError(CustomError); - - // Expectations - expect(mockResponse.status).toHaveBeenCalledWith(400); - expect(mockResponse.send).toHaveBeenCalledWith('Bad request: No product id in the Pub/Sub message'); - }); - - it('should handle successful execution', async () => { - const mockPubSubMessage = Buffer.from(JSON.stringify({ resource: { typeId: 'product', id: mockProductId } })).toString('base64'); - mockRequest.body = { message: { data: mockPubSubMessage } }; - - // Mocking the response from getCategoryByKey - (getCategoryByKey as jest.Mock).mockResolvedValue({ body: { id: mockCategoryId } }); - - // Mocking the response from getProductById - (getProductById as jest.Mock).mockResolvedValue({ body: { createdAt: new Date(), categories: [] } }); - - // Mocking the response from readConfiguration - (readConfiguration as jest.Mock).mockReturnValue({ categoryKey: 'mockCategoryKey' }); - - // Executing the post function - await post(mockRequest as Request, mockResponse as Response); - - // Expectations - expect(mockResponse.status).toHaveBeenCalledWith(204); - expect(mockResponse.send).toHaveBeenCalled(); - }); - - it('should handle error during execution', async () => { - const mockError = new Error('Mocked error'); - const mockPubSubMessage = Buffer.from(JSON.stringify({ resource: { typeId: 'product', id: mockProductId } })).toString('base64'); - mockRequest.body = { message: { data: mockPubSubMessage } }; - - // Mocking the response from getCategoryByKey - (getCategoryByKey as jest.Mock).mockResolvedValue({ body: { id: mockCategoryId } }); - - // Mocking the response from getProductById to throw an error - (getProductById as jest.Mock).mockRejectedValue(mockError); - - // Mocking the response from readConfiguration - (readConfiguration as jest.Mock).mockReturnValue({ categoryKey: 'mockCategoryKey' }); - - // Executing the post function - await expect(post(mockRequest as Request, mockResponse as Response)).rejects.toThrowError(CustomError); - - // Expectations - expect(mockResponse.status).toHaveBeenCalledWith(400); - expect(mockResponse.send).toHaveBeenCalledWith('Bad request: Error: Mocked error'); - }); -});