-
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.
feat(envited.ascs.digital): Abstract out return messages (#18)
- Loading branch information
Showing
8 changed files
with
106 additions
and
5 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { ERRORS } from './errors' | ||
export { RESPONSES } from './response' |
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,18 @@ | ||
export const RESPONSES = { | ||
badRequest: { | ||
status: 403, | ||
statusText: 'Bad request', | ||
}, | ||
internalServerError: { | ||
status: 500, | ||
statusText: 'Internal server error', | ||
}, | ||
notFound: { | ||
status: 404, | ||
statusText: 'Not found', | ||
}, | ||
noContent: { | ||
status: 204, | ||
statusText: 'No content', | ||
}, | ||
} |
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 @@ | ||
export { ok, badRequest, noContent, notFound, internalServerError } from './utils' |
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,70 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { RESPONSES } from '../constants'; | ||
import * as SUT from './utils' | ||
|
||
describe('common/utils', () => { | ||
const mockFetch = Promise.resolve({ json: () => Promise.resolve('') }); | ||
global.fetch = jest.fn().mockImplementation(() => mockFetch); | ||
|
||
describe('ok', () => { | ||
it.each([ | ||
['Message', 'Message'], | ||
[[], []], | ||
])('should return as expected', async (message, expected) => { | ||
// when ... we want to get the result | ||
// then ... it returns the result as expected | ||
const response = SUT.ok(message) | ||
const result = await response.json() | ||
|
||
expect(result).toEqual(expected) | ||
expect(response.status).toEqual(200) | ||
}) | ||
}) | ||
|
||
describe('badRequest', () => { | ||
it('should return a bad request reponse as expected', async () => { | ||
// when ... we submit a bad request | ||
// then ... it returns a bad request as expected | ||
const response = SUT.badRequest() | ||
|
||
expect(response.status).toEqual(403) | ||
expect(response.statusText).toEqual(RESPONSES.badRequest.statusText) | ||
}) | ||
}) | ||
|
||
describe('internalServerError', () => { | ||
it('should return an internal server error as expected', async () => { | ||
// when ... we encounter a irrecoverable error during the request | ||
// then ... it returns an internal server error response as expected | ||
const response = SUT.internalServerError() | ||
|
||
expect(response.status).toEqual(500) | ||
expect(response.statusText).toEqual(RESPONSES.internalServerError.statusText) | ||
}) | ||
}) | ||
|
||
describe('notFound', () => { | ||
it('should return a not found as expected', async () => { | ||
// when ... we cannot find the requested resource | ||
// then ... it returns a not found response as expected | ||
const response = SUT.notFound() | ||
|
||
expect(response.status).toEqual(404) | ||
expect(response.statusText).toEqual(RESPONSES.notFound.statusText) | ||
}) | ||
}) | ||
|
||
describe('noContent', () => { | ||
it('should return a no content as expected', async () => { | ||
// when ... we request a resource that returns an empty response | ||
// then ... it returns a no content response as expected | ||
const response = SUT.noContent() | ||
|
||
expect(response.status).toEqual(204) | ||
expect(response.statusText).toEqual(RESPONSES.noContent.statusText) | ||
}) | ||
}) | ||
}) |
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,11 @@ | ||
import { RESPONSES } from '../constants' | ||
|
||
export const ok = (data: any) => Response.json(data) | ||
|
||
export const badRequest = () => Response.json(null, { status: RESPONSES.badRequest.status, statusText: RESPONSES.badRequest.statusText }) | ||
|
||
export const internalServerError = () => Response.json(null, { status: RESPONSES.internalServerError.status, statusText: RESPONSES.internalServerError.statusText }) | ||
|
||
export const notFound = () => Response.json(null, { status: RESPONSES.notFound.status, statusText: RESPONSES.notFound.statusText}) | ||
|
||
export const noContent = () => new Response(null, { status: RESPONSES.noContent.status, statusText: RESPONSES.noContent.statusText }) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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