From 4a5db7eb418fd0db9cf893e67f7a38bb8547634c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20=C3=85berg?= Date: Tue, 16 Sep 2014 08:58:11 +0200 Subject: [PATCH] [Added] Tests for optional dynamic segments --- specs/Path.spec.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/specs/Path.spec.js b/specs/Path.spec.js index bacbf07a70..ce22062741 100644 --- a/specs/Path.spec.js +++ b/specs/Path.spec.js @@ -47,6 +47,20 @@ describe('Path.extractParams', function () { }); }); + describe('and the pattern is optional', function () { + var pattern = 'comments/:id?/edit' + + describe('and the path matches with supplied param', function () { + it('returns an object with the params', function () { + expect(Path.extractParams(pattern, 'comments/123/edit')).toEqual({ id: '123' }); + }); + }); + describe('and the path matches without supplied param', function () { + it('returns an object with param set to null', function () { + expect(Path.extractParams(pattern, 'comments/edit')).toEqual({id: null}); + }); + }); + }); describe('and the path does not match', function () { it('returns null', function () { expect(Path.extractParams(pattern, 'users/123')).toBe(null); @@ -166,6 +180,18 @@ describe('Path.injectParams', function () { }); }); + describe('and a param is optional', function () { + var pattern = 'comments/:id?/edit'; + + it('returns the correct path when param is supplied', function () { + expect(Path.injectParams(pattern, {id:'123'})).toEqual('comments/123/edit'); + }); + + it('returns the correct path when param is not supplied', function () { + expect(Path.injectParams(pattern, {})).toEqual('comments/edit'); + }); + }); + describe('and all params are present', function () { it('returns the correct path', function () { expect(Path.injectParams(pattern, { id: 'abc' })).toEqual('comments/abc/edit');