Skip to content

Commit

Permalink
test: add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tung-Huynh-Shopmacher committed Jul 29, 2024
1 parent 037d56b commit 1b2cbce
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions processor/tests/api/error.api.spec.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});
37 changes: 37 additions & 0 deletions processor/tests/api/success.api.spec.ts
Original file line number Diff line number Diff line change
@@ -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({});
});
});

0 comments on commit 1b2cbce

Please sign in to comment.