From f23366460e08b70ddb41b259f8a90d683760ebf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20P=C3=A1l=20Koszta?= Date: Tue, 5 Jul 2016 14:06:06 +0200 Subject: [PATCH] Fix nested groups on server --- package.js | 1 + server/group.js | 21 +++++++++++++++------ test/server/group.spec.js | 13 +++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 test/server/group.spec.js diff --git a/package.js b/package.js index 10a6e0c..713faee 100644 --- a/package.js +++ b/package.js @@ -42,6 +42,7 @@ Package.onTest(function(api) { api.addFiles('test/client/trigger.spec.js', 'client'); api.addFiles('test/client/triggers.js', 'client'); + api.addFiles('test/server/group.spec.js', 'server'); api.addFiles('test/server/plugins/fast_render.js', 'server'); api.addFiles('test/common/router.path.spec.js', ['client', 'server']); diff --git a/server/group.js b/server/group.js index 89a6d02..4e470a6 100644 --- a/server/group.js +++ b/server/group.js @@ -1,8 +1,20 @@ -Group = function(router, options) { +Group = function(router, options, parent) { options = options || {}; + + if (options.prefix && !/^\/.*/.test(options.prefix)) { + var message = "group's prefix must start with '/'"; + throw new Error(message); + } + + this._router = router; this.prefix = options.prefix || ''; + this.name = options.name; this.options = options; - this._router = router; + + this.parent = parent; + if (this.parent) { + this.prefix = parent.prefix + this.prefix; + } }; Group.prototype.route = function(pathDef, options) { @@ -11,8 +23,5 @@ Group.prototype.route = function(pathDef, options) { }; Group.prototype.group = function(options) { - var group = new Group(this._router, options); - group.parent = this; - - return group; + return new Group(this._router, options, this); }; diff --git a/test/server/group.spec.js b/test/server/group.spec.js new file mode 100644 index 0000000..4257f3e --- /dev/null +++ b/test/server/group.spec.js @@ -0,0 +1,13 @@ +Tinytest.add('Server - Group - define route with nested prefix', function (test) { + var firstPrefix = Random.id(); + var secondPrefix = Random.id(); + var routePath = Random.id(); + var routeName = Random.id(); + + var firstGroup = FlowRouter.group({prefix: '/' + firstPrefix}); + var secondGroup = firstGroup.group({prefix: '/' + secondPrefix}); + + secondGroup.route('/' + routePath, {name: routeName}); + + test.equal(FlowRouter.path(routeName), '/' + firstPrefix + '/' + secondPrefix + '/' + routePath); +});