Skip to content
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: removeAllRoutes public method #93

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,18 @@ Router.prototype.process_params = function process_params(layer, called, req, re
param()
}

/**
* Removes all paths from the current stack
*
* Removing routes could be useful when you need to add/remove routes on runtime.
*
* @public
*/

Router.prototype.removeAllRoutes = function removeAllRoutes() {
this.stack = []
}

/**
* Use the given middleware function, with optional path, defaulting to "/".
*
Expand Down
15 changes: 15 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ describe('Router', function () {
router({}, {}, done)
})

describe('.removeAllRoutes()', function () {
it('should remove all routes from the stack', function (done) {
var router = new Router()

for (var i = 0; i < 10; i++) {
router.get('/thing' + i, helloWorld)
}

router.removeAllRoutes()

assert.equal(router.stack.length, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should only be asserting against the public-visible behavior/API. I assume that by adding this method that you are saying that router.stack is private and not public. This test using .stack to make an assertion would seemingly re-enforce the idea that it is public (as changing the name of the property would break this test -- the indicator it's not private). If it is public, then I'm missing the purpose of this function, as router.stack = [] would already be clearly possible for a user to do, is that right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I could do that already, but having it in the router as a function makes it more "safe" against updates that could break that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but having it in the router as a function makes it more "safe" against updates that could break that

How so? Can you ellaborate on that?

done()
})
})

describe('.all(path, fn)', function () {
it('should be chainable', function () {
var router = new Router()
Expand Down