Skip to content

Commit 135287d

Browse files
committed
Add more tests
No need to publish. I haven't changed code
1 parent a50bd92 commit 135287d

File tree

6 files changed

+140
-1
lines changed

6 files changed

+140
-1
lines changed

test/api.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
});

test/lib.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getQueryString } from '../src/lib';
2+
3+
describe('lib getQueryString', () => {
4+
it('returns null for null or undefined url', () => {
5+
expect(getQueryString(null)).toBeNull();
6+
expect(getQueryString(undefined)).toBeNull();
7+
});
8+
9+
it('returns null when url has no ?', () => {
10+
expect(getQueryString('http://example.com/path')).toBeNull();
11+
});
12+
13+
it('returns query string from ? to end when ? is present', () => {
14+
expect(getQueryString('http://example.com?foo=1')).toBe('?foo=1');
15+
expect(getQueryString('/path?query=string')).toBe('?query=string');
16+
});
17+
18+
it('returns null when ? is followed by : (so ?: is not a query string)', () => {
19+
expect(getQueryString('get /path?:optional')).toBeNull();
20+
});
21+
});

test/match-priority.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Parth from '../src';
2+
3+
describe('match priority', () => {
4+
it('deeper path wins over shallower when both match', () => {
5+
const parth = new Parth();
6+
parth.set('/:a').set('/:a/:b');
7+
const result = parth.get('/x/y');
8+
expect(result).not.toBeNull();
9+
expect(result!.path).toBe('/:a/:b');
10+
expect(result!.params).toEqual({ a: 'x', b: 'y' });
11+
});
12+
13+
it('when depth is equal, stem localeCompare determines winner', () => {
14+
const parth = new Parth();
15+
parth.set('/foo/:id');
16+
parth.set('/bar/:id');
17+
expect(parth.get('/foo/1')).toMatchObject({ path: '/foo/:id', params: { id: '1' } });
18+
expect(parth.get('/bar/1')).toMatchObject({ path: '/bar/:id', params: { id: '1' } });
19+
});
20+
});

test/normalization.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Parth from '../src';
2+
3+
describe('normalization', () => {
4+
it('set trims and collapses whitespace in path', () => {
5+
const parth = new Parth();
6+
parth.set(' get /hello/:there ');
7+
const result = parth.get('get /hello/world');
8+
expect(result).not.toBeNull();
9+
expect(result!.params).toEqual({ there: 'world' });
10+
});
11+
12+
it('get trims and collapses whitespace in input path', () => {
13+
const parth = new Parth().set('get /hello/:there');
14+
const result = parth.get(' get /hello/world ');
15+
expect(result).not.toBeNull();
16+
expect(result!.params).toEqual({ there: 'world' });
17+
});
18+
});

test/set-merge.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Parth from '../src';
2+
3+
describe('set merge', () => {
4+
it('setting same path twice merges options into existing entry', () => {
5+
const parth = new Parth();
6+
parth.set('/hello/:id');
7+
parth.set('/hello/:id', { path: '/hello/:id' });
8+
const result = parth.get('/hello/42');
9+
expect(result).not.toBeNull();
10+
expect(result!.params).toEqual({ id: '42' });
11+
});
12+
13+
it('set(path, opt) passes optional PathOpt into stored entry', () => {
14+
const parth = new Parth();
15+
parth.set('/a', { path: '/a', custom: 'value' } as unknown as { path?: string; custom?: string });
16+
const result = parth.get('/a');
17+
expect(result).not.toBeNull();
18+
expect((result as { custom?: string })!.custom).toBe('value');
19+
});
20+
});

test/verify-publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"private": true,
44
"type": "module",
55
"dependencies": {
6-
"parth": "file:../../parth-4.2.3.tgz"
6+
"parth": "file:../../parth-5.0.1.tgz"
77
}
88
}

0 commit comments

Comments
 (0)