-
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.
#17 Use @ljosberinn Lambda Test Util for API tests
- Loading branch information
Showing
10 changed files
with
2,107 additions
and
796 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import nc from 'next-connect'; | ||
import { testLambda } from '../../utils/lambdaTestUtil'; | ||
import postDocument from '../../utils/dbUtil'; | ||
import { expectJSONBodyMiddleware } from '../../middlewares/expectJSONBody'; | ||
import { | ||
RequestInitMethod, | ||
} from '../../utils/requestMethods'; | ||
|
||
afterEach(jest.clearAllMocks); | ||
|
||
const url = '/api/document'; | ||
const middleware = expectJSONBodyMiddleware; | ||
const docRoute = nc().post(postDocument); | ||
|
||
test('should be a function', () => { | ||
expect(docRoute).toBeInstanceOf(Function); | ||
}); | ||
|
||
test('results in 404 on get', async () => { | ||
const method: RequestInitMethod = 'get'; | ||
const response = await testLambda(docRoute, { | ||
method, | ||
middleware, | ||
url, | ||
}); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
|
||
test('results in 400 on empty post', async () => { | ||
const method: RequestInitMethod = 'post'; | ||
const response = await testLambda(docRoute, { | ||
method, | ||
middleware, | ||
url, | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
}); | ||
|
||
test('results in 400 on post with missing title', async () => { | ||
const method: RequestInitMethod = 'post'; | ||
const response = await testLambda(docRoute, { | ||
method, | ||
middleware, | ||
url, | ||
body: { | ||
'notes':'test', | ||
}, | ||
}); | ||
|
||
expect(response.status).toBe(400); | ||
}); |
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,29 @@ | ||
import { NextApiResponse, NextApiRequest } from 'next'; | ||
import { Middleware } from 'next-connect'; | ||
|
||
/** | ||
* Middleware accepting exclusively valid JSON as req.body, if existing | ||
* This utility courtesy of Gerrit Alex (@ljosberinn) | ||
* From @ljosberinn/personal-react-boilerplate on GitHub | ||
* Released under MIT License, 2020 | ||
*/ | ||
|
||
export const expectJSONBodyMiddleware: Middleware<NextApiRequest, NextApiResponse> = (req, res, next) => { | ||
if (req.body.length > 0) { | ||
try { | ||
const body = JSON.parse(req.body); | ||
|
||
if (!(body instanceof Object)) { | ||
return res.status(400).end(); | ||
} | ||
|
||
req.body = body; | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
return res.status(400).end(); | ||
} | ||
} | ||
|
||
next(); | ||
}; |
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
Oops, something went wrong.