forked from balderdashy/sails
-
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.
Stubs reverse routing functions and adds failing tests for aspects of…
… their implementation as specced balderdashy#3402 (comment).
- Loading branch information
1 parent
a5f99d7
commit 5f7e3f2
Showing
6 changed files
with
187 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
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,55 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var util = require('util'); | ||
var _ = require('lodash'); | ||
|
||
|
||
/** | ||
* getRouteFor() | ||
* | ||
* Look up more information about the first explicit route defined in this app | ||
* which has the given route target. | ||
* | ||
* Note that this function _only searches explicit routes_ which have been configured | ||
* manually (e.g. in `config/routes.js`). For more info, see: | ||
* https://github.com/balderdashy/sails/issues/3402#issuecomment-171633341 | ||
* ---------------------------------------------------------------------------------------- | ||
* | ||
* Usage: | ||
* | ||
* ``` | ||
* getRouteFor('DuckController.quack'); | ||
* getRouteFor({ target: 'DuckController.quack' }); | ||
* // => | ||
* // { | ||
* // url: '/ducks/:id/quack', | ||
* // method: 'post' | ||
* // } | ||
* ``` | ||
*/ | ||
module.exports = function getRouteFor(routeQuery){ | ||
|
||
// Validate and normalize usage. | ||
var routeTargetToLookup; | ||
if ( _.isString(routeQuery) ) { | ||
routeTargetToLookup = routeQuery; | ||
} | ||
else if ( _.isObject(routeQuery) && _.isString(routeQuery.target) ) { | ||
routeTargetToLookup = routeQuery.target; | ||
} | ||
else { | ||
var usageErr = new Error('Usage error: `sails.getRouteFor()` expects a string route target (e.g. "DuckController.quack") or a dictionary with a target property (e.g. `{target: "DuckController.quack"}`). But instead, it received a `'+typeof routeQuery+'`: '+util.inspect(routeQuery, {depth: null}) ); | ||
usageErr.code = 'E_USAGE'; | ||
throw usageErr; | ||
} | ||
|
||
// Now look up the first route with this target (`routeTargetToLookup`). | ||
var routeInfo = {url:'todo',method:'todo'}; | ||
// TODO | ||
|
||
// And finally return the route info. | ||
return routeInfo; | ||
|
||
}; |
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,36 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var util = require('util'); | ||
var _ = require('lodash'); | ||
var getRouteFor = require('./get-route-for'); | ||
|
||
|
||
/** | ||
* getUrlFor() | ||
* | ||
* Look up the URL of this app's first explicit route with the given route target. | ||
* | ||
* Note that this function _only searches explicit routes_ which have been configured | ||
* manually (e.g. in `config/routes.js`). For more info, see: | ||
* https://github.com/balderdashy/sails/issues/3402#issuecomment-171633341 | ||
* ---------------------------------------------------------------------------------------- | ||
* | ||
* Usage: | ||
* | ||
* ``` | ||
* getUrlFor('DuckController.quack'); | ||
* // => '/ducks/:id/quack' | ||
* | ||
* getUrlFor({ target: 'DuckController.quack' }); | ||
* // => '/ducks/:id/quack' | ||
* ``` | ||
*/ | ||
module.exports = function getUrlFor(routeQuery){ | ||
|
||
// Now attempt to look up the first route that matches the specified argument | ||
// and if it works, then return its URL. | ||
return getRouteFor(routeQuery).url; | ||
|
||
}; |
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,46 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var assert = require('assert'); | ||
var util = require('util'); | ||
var Sails = require('../../lib').constructor; | ||
|
||
|
||
describe.skip('app.getRouteFor()', function (){ | ||
|
||
var app; | ||
before(function (done){ | ||
app = new Sails(); | ||
app.load({ | ||
globals: false, | ||
loadHooks: [], | ||
routes: { | ||
'get /signup': 'PageController.signup', | ||
'post /signup': 'UserController.signup', | ||
'post /*': 'UserController.signup' | ||
} | ||
}, done); | ||
}); | ||
|
||
|
||
it('should return appropriate route info dictionary with simplified usage', function () { | ||
var route = app.getRouteFor('PageController.signup'); | ||
assert.equal(route.method, 'get'); | ||
assert.equal(route.url, '/signup'); | ||
}); | ||
|
||
it('should return appropriate route info dictionary with expanded usage', function () { | ||
var route = app.getRouteFor({ target: 'PageController.signup' }); | ||
assert.equal(route.method, 'get'); | ||
assert.equal(route.url, '/signup'); | ||
}); | ||
|
||
it('should return the _first_ matching route', function () { | ||
var route = app.getRouteFor({ target: 'UserController.signup' }); | ||
assert.equal(route.method, 'post'); | ||
assert.equal(route.url, '/signup'); | ||
}); | ||
|
||
}); | ||
|
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,40 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var assert = require('assert'); | ||
var util = require('util'); | ||
var Sails = require('../../lib').constructor; | ||
|
||
|
||
describe.skip('app.getUrlFor()', function (){ | ||
|
||
var app; | ||
before(function (done){ | ||
app = new Sails(); | ||
app.load({ | ||
globals: false, | ||
loadHooks: [], | ||
routes: { | ||
'get /signup': 'PageController.signup', | ||
'post /login': 'UserController.login', | ||
'post /*': 'UserController.login' | ||
} | ||
}, done); | ||
}); | ||
|
||
|
||
it('should return appropriate route URL with simplified usage', function () { | ||
assert.equal( app.getUrlFor('PageController.signup'), '/signup' ); | ||
}); | ||
|
||
it('should return appropriate route URL with expanded usage', function () { | ||
assert.equal( app.getUrlFor({ target: 'PageController.login' }), '/signup' ); | ||
}); | ||
|
||
it('should return the _first_ matching route URL for the given target', function () { | ||
assert.equal( app.getUrlFor('UserController.login'), '/login' ); | ||
}); | ||
|
||
}); | ||
|
Empty file.