<commit_before>import router from 'wee-routes';
import { RouteHandler } from 'wee-routes';

const basicRoutes = [
	{
		path: '/',
		handler() {
			//
		}
	},
	{
		path: '/about',
		handler() {
			//
		}
	}
];

const testUri = 'https://www.weepower.com:9000/scripts?foo=bar#hash';

function setPath(path) {
	window.history.pushState({}, 'Title', path);
}

describe('Routes', () => {
	describe('map', () => {
		beforeEach(router.reset);

		it('should accept an array of objects', () => {
			router.map(basicRoutes);

			expect(router.routes()).to.be.an('array');
		});

		it('should overwrite existing path object', () => {
			router.map(basicRoutes)
				.map([
					{ path: '/', handler: 'new handler' }
				]);

			let routes = router.routes();

			expect(routes.length).to.equal(2);
			expect(routes[0].handler).to.equal('new handler');
			expect(routes[1].path).to.equal('/about');
		});
	});

	describe('routes', () => {
		afterEach(router.reset);

		it('should return route array', () => {
			router.map(basicRoutes);

			expect(router.routes()).to.be.an('array');
			expect(router.routes()[0].path).to.equal('/');
		});

		it('should return the route with specific path', () => {
			router.map(basicRoutes);

			expect(router.routes().length).to.be.greaterThan(0);
			expect(router.routes('/')).to.be.an('object');
			expect(router.routes('/about').path).to.equal('/about');
		});

		it('should return the route with specific name', () => {
			router.map([{ path: '/other', name: 'other', handler: () => {} }].concat(basicRoutes));

			expect(router.routes('other').path).to.equal('/other');
		});
	});

	describe('filter', () => {
		let state = false;
		afterEach(() => {
			router.reset();
			state = false;
		});

		it('should return filters object', () => {
			router.addFilter('test', () => {
				return true;
			});

			expect(router.filters()).to.be.an('object');
		});

		it('should add a filter to the filters object', () => {
			router.addFilter('test', () => {
				return true;
			});

			expect(router.filters()).to.have.property('test');
			expect(router.filters().test()).to.equal(true);
		});

		it('should add multiple filters to the filters object', () => {
			router.addFilter({
				test() {
					return 'test';
				},
				test2() {
					return 'test2';
				}
			});

			expect(router.filters()).to.have.property('test');
			expect(router.filters()).to.have.property('test2');
			expect(router.filters().test()).to.equal('test');
			expect(router.filters().test2()).to.equal('test2');
		});
	});

	describe('run', () => {
		let state = false;
		let stateArray = [];

		afterEach(() => {
			router.reset();
			state = false;
			stateArray = [];
		});

		it('should evaluate existing routes against current URL', () => {
			setPath('/');
			router.map([
				{
					path: '/',
					handler() {
						state = true;
					}
				}
			]).run();

			expect(state).to.equal(true);
		});

		it('should parse url variable parameters and pass to handler', () => {
			setPath('/blog/5');
			router.map([
				{
					path: '/blog/:id',
					handler(params) {
						state = true;
						expect(params.id).to.equal(5);
					}
				}
			]).run();

			expect(state).to.equal(true);
		});

		it('should pass multiple url variables as an object to handler', () => {
			setPath('/blog/tech/2017/10/5/blog-title');
			router.map([
				{
					path: '/blog/:category/:year/:month/:day/:slug',
					handler(params) {
						expect(params.category).to.equal('tech');
						expect(params.year).to.equal(2017);
						expect(params.month).to.equal(10);
						expect(params.day).to.equal(5);
						expect(params.slug).to.equal('blog-title');
					}
				}
			]).run();
		});

		it('should evaluate wildcard routes and run handlers accordingly', () => {
			setPath('/test/test2');
			router.map([
				{
					path: '/test/*',
					handler() {
						stateArray.push(1);
					}
				},
				{
					path: '/test/test2',
					handler() {
						stateArray.push(2);
					}
				}
			]).run();

			expect(stateArray.length).to.equal(2);<commit_msg>Add additional assertions to wildcard route test<commit_after>import router from 'wee-routes';
import { RouteHandler } from 'wee-routes';

const basicRoutes = [
	{
		path: '/',
		handler() {
			//
		}
	},
	{
		path: '/about',
		handler() {
			//
		}
	}
];

const testUri = 'https://www.weepower.com:9000/scripts?foo=bar#hash';

function setPath(path) {
	window.history.pushState({}, 'Title', path);
}

describe('Routes', () => {
	describe('map', () => {
		beforeEach(router.reset);

		it('should accept an array of objects', () => {
			router.map(basicRoutes);

			expect(router.routes()).to.be.an('array');
		});

		it('should overwrite existing path object', () => {
			router.map(basicRoutes)
				.map([
					{ path: '/', handler: 'new handler' }
				]);

			let routes = router.routes();

			expect(routes.length).to.equal(2);
			expect(routes[0].handler).to.equal('new handler');
			expect(routes[1].path).to.equal('/about');
		});
	});

	describe('routes', () => {
		afterEach(router.reset);

		it('should return route array', () => {
			router.map(basicRoutes);

			expect(router.routes()).to.be.an('array');
			expect(router.routes()[0].path).to.equal('/');
		});

		it('should return the route with specific path', () => {
			router.map(basicRoutes);

			expect(router.routes().length).to.be.greaterThan(0);
			expect(router.routes('/')).to.be.an('object');
			expect(router.routes('/about').path).to.equal('/about');
		});

		it('should return the route with specific name', () => {
			router.map([{ path: '/other', name: 'other', handler: () => {} }].concat(basicRoutes));

			expect(router.routes('other').path).to.equal('/other');
		});
	});

	describe('filter', () => {
		let state = false;
		afterEach(() => {
			router.reset();
			state = false;
		});

		it('should return filters object', () => {
			router.addFilter('test', () => {
				return true;
			});

			expect(router.filters()).to.be.an('object');
		});

		it('should add a filter to the filters object', () => {
			router.addFilter('test', () => {
				return true;
			});

			expect(router.filters()).to.have.property('test');
			expect(router.filters().test()).to.equal(true);
		});

		it('should add multiple filters to the filters object', () => {
			router.addFilter({
				test() {
					return 'test';
				},
				test2() {
					return 'test2';
				}
			});

			expect(router.filters()).to.have.property('test');
			expect(router.filters()).to.have.property('test2');
			expect(router.filters().test()).to.equal('test');
			expect(router.filters().test2()).to.equal('test2');
		});
	});

	describe('run', () => {
		let state = false;
		let stateArray = [];

		afterEach(() => {
			router.reset();
			state = false;
			stateArray = [];
		});

		it('should evaluate existing routes against current URL', () => {
			setPath('/');
			router.map([
				{
					path: '/',
					handler() {
						state = true;
					}
				}
			]).run();

			expect(state).to.equal(true);
		});

		it('should parse url variable parameters and pass to handler', () => {
			setPath('/blog/5');
			router.map([
				{
					path: '/blog/:id',
					handler(params) {
						state = true;
						expect(params.id).to.equal(5);
					}
				}
			]).run();

			expect(state).to.equal(true);
		});

		it('should pass multiple url variables as an object to handler', () => {
			setPath('/blog/tech/2017/10/5/blog-title');
			router.map([
				{
					path: '/blog/:category/:year/:month/:day/:slug',
					handler(params) {
						expect(params.category).to.equal('tech');
						expect(params.year).to.equal(2017);
						expect(params.month).to.equal(10);
						expect(params.day).to.equal(5);
						expect(params.slug).to.equal('blog-title');
					}
				}
			]).run();
		});

		it('should evaluate wildcard routes and run handlers accordingly', () => {
			setPath('/test/test2/3');
			router.map([
				{
					path: '/test/*',
					handler(params) {
						expect(params[0]).to.equal('test2/3');
						stateArray.push(1);
					}
				},
				{
					path: '/test/*/:id',
					handler(params) {
						expect(params[0]).to.equal('test2');
						expect(params.id).to.equal(3);
						stateArray.push(2);
					}
				}
			]).run();

			expect(stateArray.length).to.equal(2);
{'fraction_non_alphanumeric': 0.2200729927007299, 'fraction_numerical': 0.009245742092457421, 'mean_word_length': 2.9486071085494716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1343201', 'n_tokens_mistral': 3459, 'n_tokens_neox': 2764, 'n_words': 450}