From 857e181bbe4185daa74861c5198b31aadf780f4c Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Wed, 30 Mar 2016 14:47:42 -0500 Subject: [PATCH] test(MatchPattern): Added unit tests for match pattern --- spec/match-pattern.spec.ts | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/match-pattern.spec.ts diff --git a/spec/match-pattern.spec.ts b/spec/match-pattern.spec.ts new file mode 100644 index 0000000..87f82ad --- /dev/null +++ b/spec/match-pattern.spec.ts @@ -0,0 +1,41 @@ +import { matchPattern } from '../lib/match-pattern' + +describe('matchPattern', function() { + function assertMatch(pattern, pathname, remainingPathname, paramNames, paramValues) { + expect(matchPattern(pattern, pathname)).toEqual({ + remainingPathname, + paramNames, + paramValues + }); + } + + it('works without params', function() { + assertMatch('/', '/path', 'path', [], []); + }); + + it('works with named params', function() { + assertMatch('/:id', '/path', '', [ 'id' ], [ 'path' ]); + assertMatch('/:id.:ext', '/path.jpg', '', [ 'id', 'ext' ], [ 'path', 'jpg' ]); + }); + + it('works with named params that contain spaces', function() { + assertMatch('/:id', '/path+more', '', [ 'id' ], [ 'path+more' ]); + assertMatch('/:id', '/path%20more', '', [ 'id' ], [ 'path more' ]); + }); + + it('works with splat params', function() { + assertMatch('/files/*.*', '/files/path.jpg', '', [ 'splat', 'splat' ], [ 'path', 'jpg' ]); + }); + + it('ignores trailing slashes', function() { + assertMatch('/:id', '/path/', '', [ 'id' ], [ 'path' ]); + }); + + it('works with greedy splat (**)', function() { + assertMatch('/**/g', '/greedy/is/good/g', '', [ 'splat' ], [ 'greedy/is/good' ]); + }); + + it('works with greedy and non-greedy splat', function() { + assertMatch('/**/*.jpg', '/files/path/to/file.jpg', '', [ 'splat', 'splat' ], [ 'files/path/to', 'file' ]); + }); +});