|
| 1 | +import { Route } from '../route'; |
| 2 | +import { matchRoute, parsePath } from './path-parser'; |
| 3 | + |
| 4 | +describe('parsePath', () => { |
| 5 | + it('should parse empty route', () => { |
| 6 | + expect(parsePath({ path: '', options: {} })).toEqual(/^[\/#\?]?$/i); |
| 7 | + expect(parsePath({ path: '/', options: {} })).toEqual(/^[\/#\?]?$/i); |
| 8 | + }); |
| 9 | + it('should parse empty wildcard route', () => { |
| 10 | + expect(parsePath({ path: '', options: { exact: false } })).toEqual( |
| 11 | + /^(?:[\/#\?](?=[]|$))?/i |
| 12 | + ); |
| 13 | + expect(parsePath({ path: '/', options: { exact: false } })).toEqual( |
| 14 | + /^(?:[\/#\?](?=[]|$))?/i |
| 15 | + ); |
| 16 | + }); |
| 17 | + it('should parse static route', () => { |
| 18 | + expect(parsePath({ path: 'first/second', options: {} })).toEqual( |
| 19 | + /^\/first\/second[\/#\?]?$/i |
| 20 | + ); |
| 21 | + expect(parsePath({ path: '/first/second', options: {} })).toEqual( |
| 22 | + /^\/first\/second[\/#\?]?$/i |
| 23 | + ); |
| 24 | + }); |
| 25 | + it('should remove ending slash', () => { |
| 26 | + expect(parsePath({ path: 'first/', options: {} })).toEqual( |
| 27 | + /^\/first[\/#\?]?$/i |
| 28 | + ); |
| 29 | + expect(parsePath({ path: '/first/', options: {} })).toEqual( |
| 30 | + /^\/first[\/#\?]?$/i |
| 31 | + ); |
| 32 | + }); |
| 33 | + it('should parse static wildcard route', () => { |
| 34 | + expect( |
| 35 | + parsePath({ path: 'first/second', options: { exact: false } }) |
| 36 | + ).toEqual(/^\/first\/second(?:[\/#\?](?=[]|$))?(?=[\/#\?]|[]|$)/i); |
| 37 | + expect( |
| 38 | + parsePath({ path: '/first/second', options: { exact: false } }) |
| 39 | + ).toEqual(/^\/first\/second(?:[\/#\?](?=[]|$))?(?=[\/#\?]|[]|$)/i); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should parse dynamic route', () => { |
| 43 | + expect(parsePath({ path: ':id', options: {} })).toEqual( |
| 44 | + /^(?:\/([^\/#\?]+?))[\/#\?]?$/i |
| 45 | + ); |
| 46 | + expect(parsePath({ path: '/books/:bookId', options: {} })).toEqual( |
| 47 | + /^\/books(?:\/([^\/#\?]+?))[\/#\?]?$/i |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should parse dynamic wildcard route', () => { |
| 52 | + expect(parsePath({ path: ':id', options: { exact: false } })).toEqual( |
| 53 | + /^(?:\/([^\/#\?]+?))(?:[\/#\?](?=[]|$))?(?=[\/#\?]|[]|$)/i |
| 54 | + ); |
| 55 | + expect( |
| 56 | + parsePath({ path: '/books/:bookId', options: { exact: false } }) |
| 57 | + ).toEqual( |
| 58 | + /^\/books(?:\/([^\/#\?]+?))(?:[\/#\?](?=[]|$))?(?=[\/#\?]|[]|$)/i |
| 59 | + ); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('matchRoute', () => { |
| 64 | + it('should match wildcard route', () => { |
| 65 | + const route: Route = { path: '', options: { exact: false } }; |
| 66 | + route.matcher = parsePath(route); |
| 67 | + |
| 68 | + expect(matchRoute('/', route)).toEqual({ path: '/', params: {} }); |
| 69 | + expect(matchRoute('/first', route)).toEqual({ path: '', params: {} }); |
| 70 | + expect(matchRoute('/first/second/third', route)).toEqual({ |
| 71 | + path: '', |
| 72 | + params: {}, |
| 73 | + }); |
| 74 | + }); |
| 75 | + it('should match empty route', () => { |
| 76 | + const route: Route = { path: '', options: {} }; |
| 77 | + route.matcher = parsePath(route); |
| 78 | + |
| 79 | + expect(matchRoute('/', route)).toEqual({ path: '/', params: {} }); |
| 80 | + expect(matchRoute('/first', route)).not.toBeDefined(); |
| 81 | + expect(matchRoute('/first/second', route)).not.toBeDefined(); |
| 82 | + }); |
| 83 | + it('should match static wildcard route', () => { |
| 84 | + const route: Route = { path: 'first/second', options: { exact: false } }; |
| 85 | + route.matcher = parsePath(route); |
| 86 | + |
| 87 | + expect(matchRoute('/first/second', route)).toEqual({ |
| 88 | + path: '/first/second', |
| 89 | + params: {}, |
| 90 | + }); |
| 91 | + expect(matchRoute('/first', route)).not.toBeDefined(); |
| 92 | + expect(matchRoute('/first/second/third', route)).toEqual({ |
| 93 | + path: '/first/second', |
| 94 | + params: {}, |
| 95 | + }); |
| 96 | + }); |
| 97 | + it('should match static route', () => { |
| 98 | + const route: Route = { path: 'first/second', options: {} }; |
| 99 | + route.matcher = parsePath(route); |
| 100 | + |
| 101 | + expect(matchRoute('/first/second', route)).toEqual({ |
| 102 | + path: '/first/second', |
| 103 | + params: {}, |
| 104 | + }); |
| 105 | + expect(matchRoute('/first', route)).not.toBeDefined(); |
| 106 | + expect(matchRoute('/first/second/third', route)).not.toBeDefined(); |
| 107 | + }); |
| 108 | + it('should match dynamic wildcard route', () => { |
| 109 | + const route: Route = { path: 'first/:id', options: { exact: false } }; |
| 110 | + route.matcher = parsePath(route); |
| 111 | + |
| 112 | + expect(matchRoute('/first/second', route)).toEqual({ |
| 113 | + path: '/first/second', |
| 114 | + params: { id: 'second' }, |
| 115 | + }); |
| 116 | + expect(matchRoute('/first', route)).not.toBeDefined(); |
| 117 | + expect(matchRoute('/first/second/third', route)).toEqual({ |
| 118 | + path: '/first/second', |
| 119 | + params: { id: 'second' }, |
| 120 | + }); |
| 121 | + }); |
| 122 | + it('should match dynamic route', () => { |
| 123 | + const route: Route = { path: 'first/:id/:name', options: {} }; |
| 124 | + route.matcher = parsePath(route); |
| 125 | + |
| 126 | + expect(matchRoute('/first/second', route)).not.toBeDefined(); |
| 127 | + expect(matchRoute('/first', route)).not.toBeDefined(); |
| 128 | + expect(matchRoute('/first/second/third', route)).toEqual({ |
| 129 | + path: '/first/second/third', |
| 130 | + params: { id: 'second', name: 'third' }, |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments