-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Router route
method (#19)
#33
Conversation
1d859ac
to
2f6b64c
Compare
1 similar comment
src/interfaces.ts
Outdated
export interface RouteProcessorAppender<T> { | ||
|
||
/** | ||
* @param handlers the processors to mount to this route's path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
* // Runs for all HTTP verbs | ||
* }) | ||
* .get(function(req, res, next) { | ||
* res.json(...); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment
tests/integration-tests.test.ts
Outdated
assert.calledOnce(putSpy); | ||
assert.notCalled(getSpy); | ||
assert.notCalled(postSpy); | ||
postSpy.resetHistory(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong one
tests/integration-tests.test.ts
Outdated
_.each(handlers, (handler) => { assert.calledOnce(handler); }); | ||
|
||
// Check that the "all" handler was called for each method | ||
expect(allHandler.callCount).to.strictlyEqual(methods.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could refactor these checks to be inside the testOutcome
each ... otherwise we're not really testing that the right handler was called on each outcome.
2f6b64c
to
1174414
Compare
I pushed these changes. diff --git a/src/interfaces.ts b/src/interfaces.ts
index 86b76d1..50da097 100644
--- a/src/interfaces.ts
+++ b/src/interfaces.ts
@@ -187,6 +187,7 @@ export interface IRouter {
* // Runs for all HTTP verbs
* })
* .get(function(req, res, next) {
+ * // Handle GETs to /hello
* res.json(...);
* })
* .post(function(req, res, next) {
@@ -247,7 +248,7 @@ export interface IRoute {
export interface RouteProcessorAppender<T> {
/**
- * @param handlers the processors to mount to this route's path
- */
+ * @param handlers the processors to mount to this route's path
+ */
(...handlers: ProcessorOrProcessors[]): T;
}
diff --git a/tests/integration-tests.test.ts b/tests/integration-tests.test.ts
index a2d806a..11efd89 100644
--- a/tests/integration-tests.test.ts
+++ b/tests/integration-tests.test.ts
@@ -623,12 +623,11 @@ describe('integration tests', () => {
assert.calledOnce(putSpy);
assert.notCalled(getSpy);
assert.notCalled(postSpy);
- postSpy.resetHistory();
+ putSpy.resetHistory();
});
it('registers route handlers properly', () => {
- let handlers: SinonSpy[] = [],
- methods: (keyof IRoute & keyof IRouter)[],
+ let methods: (keyof IRoute & keyof IRouter)[],
allHandler: SinonSpy,
route: IRoute;
@@ -643,34 +642,46 @@ describe('integration tests', () => {
// Register a handler for each method
- _.each(methods, (method) => {
- let handler = spy((_req: Request, resp: Response) => { resp.send('Test'); });
+ const handlers = _.reduce(methods, (memo, method) => {
+ let handler = spy((_req: Request, resp: Response) => { resp.send(`Test ${method}`); });
// Save the handler spy for testing later
- handlers.push(handler);
+ memo[method] = handler;
+ // add the handler to our route
route[method](handler);
- });
+
+ return memo;
+ }, {} as { [k: string]: SinonSpy });
app.use((_req: Request, resp: Response) => {
resp.send('not found');
});
// Run once for each method type
- _.each(methods, (method) => {
- testOutcome(method.toUpperCase(), '/test', 'Test');
+ // Both a path with and without a trailing slash should match
+ _.each([ '/test', '/test/' ], (path) => {
+ _.each(methods, (method) => {
+ testOutcome(method.toUpperCase(), path, `Test ${method}`);
+
+ // Check that the "all" handler was called
+ assert.calledOnce(allHandler);
+ allHandler.resetHistory();
+
+ // Check that only the one handler was called
+ _.each(handlers, (handler, handlerMethod) => {
+ if (method === handlerMethod) {
+ assert.calledOnce(handler);
+ } else {
+ assert.notCalled(handler);
+ }
+ handler.resetHistory();
+ });
+ });
});
- // Check that each handler was called exactly once
- _.each(handlers, (handler) => { assert.calledOnce(handler); });
-
- // Check that the "all" handler was called for each method
- expect(allHandler.callCount).to.strictlyEqual(methods.length);
-
// Other tests
_.each(methods, (method) => {
- // Ensure trailing slash matches
- testOutcome(method.toUpperCase(), '/test/', 'Test');
// Ensure only exact matches trigger the route handler
testOutcome(method.toUpperCase(), '/test/anything', 'not found');
}); |
No description provided.