Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudy committed Sep 15, 2020
1 parent 1fff54e commit ae5764b
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 10 deletions.
47 changes: 37 additions & 10 deletions __tests__/routing.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { createRouting, number, segment } from '../src/index';

describe('routing', () => {
it('should create routing', () => {
describe('createRouting', () => {
it('creates a simple route', () => {
const routes = createRouting({
company: segment`/companies/${number('companyId')}`,
contact: segment`/contacts/${number('contactId')}`,
insight: segment`/insight/${number('insightId')}`,
admin: {
...segment`/admin`,
products: segment`/products`,
} as const);

const route = routes.products();

expect(route).toEqual('/products');
});

it('creates nested routes', () => {
const routes = createRouting({
products: {
...segment`/products`,
children: {
create: segment`/create`,
},
},
} as const);

const mainRoute = routes.products();
const nestedRoute = routes.products.create();

expect(mainRoute).toEqual('/products');
expect(nestedRoute).toEqual('/products/create');
});

it('creates nested routes with params', () => {
const routes = createRouting({
products: {
...segment`/products/${number('productId')}`,
children: {
users: segment`/users`,
edit: segment`/edit`,
},
},
onetime: segment`/onetime`,
} as const);

routes.company({ companyId: '20' });
const mainRoute = routes.products({ productId: '2' });
const nestedRoute = routes.products.edit({ productId: '2' });

expect(mainRoute).toEqual('/products/2');
expect(nestedRoute).toEqual('/products/2/edit');
});
});
47 changes: 47 additions & 0 deletions __tests__/segments/arg.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { arg, createRouting, segment } from '../../src';

describe('arg segment', () => {
it('creates route with an arg segment', () => {
const routes = createRouting({
product: segment`/product/${arg('productId')}`,
} as const);

const route = routes.product({ productId: 'id' });

expect(route).toEqual('/product/id');
});

it('creates route with an optional arg segment', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
optional: true,
})}`,
} as const);

const route = routes.product();

expect(route).toEqual('/product');
});

it('creates route with a custom pattern param', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
pattern: /[0-9]{2}/.source,
})}`,
} as const);

const route = routes.product({ productId: '23' });

expect(route).toEqual('/product/23');
});

it('route with a custom pattern param gets correctly validated', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
pattern: /[0-9]{2}/.source,
})}`,
} as const);

expect(() => routes.product({ productId: '123' })).toThrow();
});
});
31 changes: 31 additions & 0 deletions __tests__/segments/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createRouting, number, segment } from '../../src';

describe('number segment', () => {
it('creates route with an optional number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId', true)}`,
} as const);

const route = routes.product();

expect(route).toEqual('/product');
});

it('creates route with a required number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId')}`,
} as const);

const route = routes.product({ productId: '1' });

expect(route).toEqual('/product/1');
});

it('throws when given an invalid number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId')}`,
} as const);

expect(() => routes.product({ productId: 'aaa' })).toThrow();
});
});
36 changes: 36 additions & 0 deletions __tests__/segments/query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createRouting, number, query, segment } from '../../src';

describe('query segment', () => {
it('creates route with an optional query param', () => {
const routes = createRouting({
product: segment`/product${query({ productId: false })}`,
} as const);

const route = routes.product();

expect(route).toEqual('/product');
});

it('creates route with a required query param', () => {
const routes = createRouting({
product: segment`/product${query({ productId: true })}`,
} as const);

const route = routes.product({}, { productId: '2' });

expect(route).toEqual('/product?productId=2');
});

it('creates route with multiple query params and they are sorted in alphabetical order', () => {
const routes = createRouting({
product: segment`/product${query({
productId: true,
details: true,
})}`,
} as const);

const route = routes.product({}, { productId: '2', details: 'false' });

expect(route).toEqual(`/product?details=false&productId=2`);
});
});
43 changes: 43 additions & 0 deletions __tests__/segments/uuid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createRouting, number, segment, uuid } from '../../src';

describe('uuid segment', () => {
const testUuid = '3d368832-0bc0-4fcc-bf75-5bd8794c1ad3';

it('creates route with an optional uuid param', () => {
const routes = createRouting({
product: segment`/product/${uuid('productId', true)}`,
} as const);

const route = routes.product();

expect(route).toEqual('/product');
});

it('creates route with a required uuid param', () => {
const routes = createRouting({
product: segment`/product/${uuid('productId')}`,
} as const);

const route = routes.product({ productId: testUuid });

expect(route).toEqual(`/product/${testUuid}`);
});

it('throws when given a plain string', () => {
const routes = createRouting({
product: segment`/product/${uuid('productId')}`,
} as const);

expect(() => routes.product({ productId: 'a' })).toThrow();
});

it('throws when given an invalid uuid', () => {
const routes = createRouting({
product: segment`/product/${uuid('productId')}`,
} as const);

expect(() =>
routes.product({ productId: '3d368832-0bc-4fcc-bf75-5bd8794c1ad3' })
).toThrow();
});
});

0 comments on commit ae5764b

Please sign in to comment.