-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
037d56b
commit 1b2cbce
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); |