|
| 1 | +import Parth from '../src'; |
| 2 | + |
| 3 | +describe('api', () => { |
| 4 | + describe('invalid inputs', () => { |
| 5 | + it('set(non-string) returns this and does not add a route', () => { |
| 6 | + const parth = new Parth(); |
| 7 | + const chain = parth.set('get /:id').set(123 as unknown as string); |
| 8 | + expect(chain).toBe(parth); |
| 9 | + expect(parth.get('get /1')).toMatchObject({ params: { id: '1' } }); |
| 10 | + expect(parth.get('123')).toBeNull(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('get(non-string) returns null', () => { |
| 14 | + const parth = new Parth().set('get /:id'); |
| 15 | + expect(parth.get(123 as unknown as string)).toBeNull(); |
| 16 | + expect(parth.get(null as unknown as string)).toBeNull(); |
| 17 | + expect(parth.get(undefined as unknown as string)).toBeNull(); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('no match', () => { |
| 22 | + it('get(path) returns null when no route matches', () => { |
| 23 | + const parth = new Parth().set('get /:id'); |
| 24 | + expect(parth.get('post /1')).toBeNull(); |
| 25 | + expect(parth.get('get')).toBeNull(); |
| 26 | + expect(parth.get('')).toBeNull(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('exact store match', () => { |
| 31 | + it('get(path) when path is exact registered path returns early with empty notFound and params', () => { |
| 32 | + const parth = new Parth().set('get /hello/:there'); |
| 33 | + const exactPath = 'get /hello/:there'; |
| 34 | + const result = parth.get(exactPath); |
| 35 | + expect(result).not.toBeNull(); |
| 36 | + expect(result!.match).toBe(exactPath); |
| 37 | + expect(result!.notFound).toBe(''); |
| 38 | + expect(result!.params).toEqual({}); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('constructor', () => { |
| 43 | + it('can be called with no options', () => { |
| 44 | + const parth = new Parth(); |
| 45 | + parth.set('/:a'); |
| 46 | + expect(parth.get('/x')).toMatchObject({ params: { a: 'x' } }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('result immutability', () => { |
| 51 | + it('get() returns a clone; mutating result does not affect store', () => { |
| 52 | + const parth = new Parth().set('/:id'); |
| 53 | + const result1 = parth.get('/foo'); |
| 54 | + const result2 = parth.get('/foo'); |
| 55 | + expect(result1!.params.id).toBe('foo'); |
| 56 | + result1!.params.id = 'mutated'; |
| 57 | + expect(result2!.params.id).toBe('foo'); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments