diff --git a/processor/tests/api/error.api.spec.ts b/processor/tests/api/error.api.spec.ts new file mode 100644 index 0000000..60d6b5b --- /dev/null +++ b/processor/tests/api/error.api.spec.ts @@ -0,0 +1,39 @@ +import { Response } from 'express'; +import { CTError } from '../../src/types/commercetools.types'; +import { apiError } from '../../src/api/error.api'; + +describe('apiError', () => { + let response: Response; + let statusMock: jest.Mock; + let jsonMock: jest.Mock; + + beforeEach(() => { + statusMock = jest.fn().mockReturnThis(); + jsonMock = jest.fn(); + response = { + status: statusMock, + json: jsonMock, + } as unknown as Response; + }); + + it('should send an error response with 400 status code and the errors array', () => { + const errors: CTError[] = [ + { code: 'InvalidInput', message: 'Invalid input provided' } as unknown as CTError, + { code: 'NotFound', message: 'Resource not found' } as unknown as CTError, + ]; + + apiError(response, errors); + + expect(statusMock).toHaveBeenCalledWith(400); + expect(jsonMock).toHaveBeenCalledWith({ errors }); + }); + + it('should send an error response with 400 status code and an empty errors array', () => { + const errors: CTError[] = []; + + apiError(response, errors); + + expect(statusMock).toHaveBeenCalledWith(400); + expect(jsonMock).toHaveBeenCalledWith({ errors }); + }); +}); diff --git a/processor/tests/api/success.api.spec.ts b/processor/tests/api/success.api.spec.ts new file mode 100644 index 0000000..610f069 --- /dev/null +++ b/processor/tests/api/success.api.spec.ts @@ -0,0 +1,37 @@ +import { UpdateAction } from '@commercetools/sdk-client-v2'; +import { Response } from 'express'; +import { apiSuccess } from '../../src/api/success.api'; + +describe('test success.api', () => { + let response: Response; + let statusMock: jest.Mock; + let jsonMock: jest.Mock; + + beforeEach(() => { + statusMock = jest.fn().mockReturnThis(); + jsonMock = jest.fn(); + response = { + status: statusMock, + json: jsonMock, + } as unknown as Response; + }); + + it('should send a success response with status code and update actions', () => { + const statusCode = 200; + const updateActions: UpdateAction[] = [{ action: 'testAction' }]; + + apiSuccess(statusCode, response, updateActions); + + expect(statusMock).toHaveBeenCalledWith(statusCode); + expect(jsonMock).toHaveBeenCalledWith({ actions: updateActions }); + }); + + it('should send a success response with status code and no update actions', () => { + const statusCode = 200; + + apiSuccess(statusCode, response); + + expect(statusMock).toHaveBeenCalledWith(statusCode); + expect(jsonMock).toHaveBeenCalledWith({}); + }); +});