Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
tests for context
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarshall committed Dec 17, 2018
1 parent c24f9c9 commit a3fb7b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sync-crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function syncCrawlRoutesDir(rootpath, options = {}) {

if (!mapping.routeInstance.expressRouter) {
if (fileHandler) {
mapping.routeInstance = fileHandler(mapping.routeInstance, { filename, routePath, verb: verbStr })
mapping.routeInstance = fileHandler(mapping.routeInstance, {
filename,
routePath,
verb: mapping.verb
})
}
// repeated check in case the above handler is not used or is not effective
if (!mapping.routeInstance.expressRouter) {
Expand Down
3 changes: 3 additions & 0 deletions test/sync-crawl/helpers/routes-09/xyz/fancy-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
name: 'tim'
}
26 changes: 26 additions & 0 deletions test/sync-crawl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,29 @@ test('should allow custom file handling as a fallback', t => {
}
})
})

test('should pass expected context when calling custom file handling', t => {
let cachedContext // set at file handling

const router = new Router()
router.use(crawl('routes-09', {
verbs: {
get: /.+/ // any name...
},

fileHandler: (content, context) => {
cachedContext = context

const route = new Route()
route.push((req, res) => {
res.send(`name is ${content.name}`)
})
return route
}
}))

// checking context
t.is(cachedContext.filename, 'fancy-name.js')
t.true(/routes-09\/xyz\/fancy-name\.js$/.test(cachedContext.routePath))
t.is(cachedContext.verb, 'get')
})

0 comments on commit a3fb7b9

Please sign in to comment.