<commit_before>
// internal libs
import { Router } from 'src/apps/router';
import { XRegExp } from 'src/libs/dependencies';
import { Event } from 'test/mocks/event';
import { Location } from 'test/mocks/location';

describe('Class Router', () => {

	describe('_isDynamicURL method', () => {

		it('should return true if has variable in url otherwise false', () => {

			// setup
			let router = Router.create();

			// test
			router._isDynamicURL('{{a}}').should.be.true();
			router._isDynamicURL('a').should.be.false();

			// cleanup
			router.destroy();

		});

	});

	describe('_convertURLToRegex method', () => {

		it('should convert passed url to regex', () => {

			// setup
			let router = Router.create();

			// test
			router._convertRouteToXRegexExp('{{year}}').should.be.equal('(?<year>[\\d\\w?()|{}_.,-]+)');
			router._convertRouteToXRegexExp('{{hour}}:{{min}}').should.be.equal('(?<hour>[\\d\\w?()|{}_.,-]+):(?<min>[\\d\\w?()|{}_.,-]+)');
			// test - convert special regex characters
			router._convertRouteToXRegexExp('/|+*?.()').should.be.equal('\\/\\|\\+\\*\\?\\.\\(\\)');

			//cleanup
			router.destroy();

		});

	});

	describe('_addRoute method', () => {

		// setup
		let router = null;

		beforeEach(() => {

			// setup
			router = Router.create();
			// add static routes
			router._addRoute('/this/is/a/route/1', 'name1');
			router._addRoute('/this/is/a/route/2', 'name2');
			router._addRoute('/this/is/{{a}}/route/3', 'name3');
			router._addRoute('/this/is/{{b}}/{{c}}/route/4', 'name4');
			router._addRoute('?id={{id}}&name={{name}}', 'name5');
			router._addRoute('?name=serkan', 'name6');
			router._addRoute('#im-{{foo}}', 'name7');
			router._addRoute('#scroll-to-foo', 'name8');
		});

		afterEach(() => router.destroy() );

		it('should return registered routes', () => {

			router._routes.should.containEql({
				static: {
					pathname: {
						'/this/is/a/route/1': {
							name: 'name1',
							type: 'static',
							route: '/this/is/a/route/1',
							urlpart: 'pathname',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						},
						'/this/is/a/route/2': {
							name: 'name2',
							type: 'static',
							route: '/this/is/a/route/2',
							urlpart: 'pathname',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					},
					search: {
						'?name=serkan': {
							name: 'name6',
							type: 'static',
							route: '?name=serkan',
							urlpart: 'search',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					},
					hash: {
						'#scroll-to-foo': {
							name: 'name8',
							type: 'static',
							route: '#scroll-to-foo',
							urlpart: 'hash',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					}
				},
				dynamic: {
					pathname: {
						'/this/is/{{a}}/route/3': {
							name: 'name3',
							type: 'dynamic',
							route: '/this/is/{{a}}/route/3',
							urlpart: 'pathname',
							regex: '\\/this\\/is\\/(?<a>[\\d\\w?()|{}_.,-]+)\\/route\\/3',
							params: null,
							fragment: null,
							cache: false,
						},
						'/this/is/{{b}}/{{c}}/route/4': {
							name: 'name4',
							type: 'dynamic',
							route: '/this/is/{{b}}/{{c}}/route/4',
							urlpart: 'pathname',
							regex: '\\/this\\/is\\/(?<b>[\\d\\w?()|{}_.,-]+)\\/(?<c>[\\d\\w?()|{}_.,-]+)\\/route\\/4',
							params: null,
							fragment: null,
							cache: false,
						}
					},
					search: {
						'?id={{id}}&name={{name}}': {
							name: 'name5',
							type: 'dynamic',
							route: '?id={{id}}&name={{name}}',
							urlpart: 'search',
							regex: '\\?id=(?<id>[\\d\\w?()|{}_.,-]+)&name=(?<name>[\\d\\w?()|{}_.,-]+)',
							params: null,
							fragment: null,
							cache: false,
						}
					},
					hash: {
						'#im-{{foo}}': {
							name: 'name7',
							type: 'dynamic',
							route: '#im-{{foo}}',
							urlpart: 'hash',
							regex: '#im-(?<foo>[\\d\\w?()|{}_.,-]+)',
							params: null,
							fragment: null,
							cache: false,
						}
					}
				}
			});

		});

		it('should throw error if duplicate route added', () => {

			(() => { router._addRoute('/this/is/a/route/2', 'name2'); }).should.throw();
			(() => { router._addRoute('/this/is/{{b}}/{{c}}/route/4', 'name4'); }).should.throw();
		});

		it('should throw error if not valid route passed', () => {

			(() => { router._addRoute('/a/b?id=5', 'name1'); }).should.throw();
			(() => { router._addRoute('/a/b?id=5#foo', 'name2'); }).should.throw();
			(() => { router._addRoute('?id=5#foo', 'name3'); }).should.throw();
		});

	});

	describe('_matchStaticURL method', () => {

		it('should return matchedObject by passed fragment', () => {

			// setup
			let router = Router.create();
			router._addRoute('/this/is/a/route/1', 'route1');

			// test: positiv
			router._matchStaticURL('/this/is/a/route/1').should.containEql({
				name: 'route1',<commit_msg>Refactor: refactor router._addRoute for matching url separatly(pathname, search and hash), part1<commit_after>
// internal libs
import { Router } from 'src/apps/router';
import { XRegExp } from 'src/libs/dependencies';
import { Event } from 'test/mocks/event';
import { Location } from 'test/mocks/location';

describe('Class Router', () => {

	describe('_isDynamicURL method', () => {

		it('should return true if has variable in url otherwise false', () => {

			// setup
			let router = Router.create();

			// test
			router._isDynamicURL('{{a}}').should.be.true();
			router._isDynamicURL('a').should.be.false();

			// cleanup
			router.destroy();

		});

	});

	describe('_convertURLToRegex method', () => {

		it('should convert passed url to regex', () => {

			// setup
			let router = Router.create();

			// test
			router._convertRouteToXRegexExp('{{year}}').should.be.equal('(?<year>[\\d\\w?()|{}_.,-]+)');
			router._convertRouteToXRegexExp('{{hour}}:{{min}}').should.be.equal('(?<hour>[\\d\\w?()|{}_.,-]+):(?<min>[\\d\\w?()|{}_.,-]+)');
			// test - convert special regex characters
			router._convertRouteToXRegexExp('/|+*?.()').should.be.equal('\\/\\|\\+\\*\\?\\.\\(\\)');

			//cleanup
			router.destroy();

		});

	});

	describe('_addRoute method', () => {

		// setup
		let router = null;

		beforeEach(() => {

			// setup
			router = Router.create();
			// add static routes
			router._addRoute('/this/is/a/route/1', 'name1');
			router._addRoute('/this/is/a/route/2', 'name2');
			router._addRoute('/this/is/{{a}}/route/3', 'name3');
			router._addRoute('/this/is/{{b}}/{{c}}/route/4', 'name4');
			router._addRoute('?id={{id}}&name={{name}}', 'name5');
			router._addRoute('?name=serkan', 'name6');
			router._addRoute('#im-{{foo}}', 'name7');
			router._addRoute('#scroll-to-foo', 'name8');
		});

		afterEach(() => router.destroy() );

		it('should return registered routes', () => {

			router._routes.should.containEql({
				static: {
					pathname: {
						'/this/is/a/route/1': {
							name: 'name1',
							type: 'static',
							route: '/this/is/a/route/1',
							urlpart: 'pathname',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						},
						'/this/is/a/route/2': {
							name: 'name2',
							type: 'static',
							route: '/this/is/a/route/2',
							urlpart: 'pathname',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					},
					search: {
						'?name=serkan': {
							name: 'name6',
							type: 'static',
							route: '?name=serkan',
							urlpart: 'search',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					},
					hash: {
						'#scroll-to-foo': {
							name: 'name8',
							type: 'static',
							route: '#scroll-to-foo',
							urlpart: 'hash',
							regex: null,
							params: null,
							fragment: null,
							cache: false,
						}
					}
				},
				dynamic: {
					pathname: {
						'/this/is/{{a}}/route/3': {
							name: 'name3',
							type: 'dynamic',
							route: '/this/is/{{a}}/route/3',
							urlpart: 'pathname',
							regex: '\\/this\\/is\\/(?<a>[\\d\\w?()|{}_.,-]+)\\/route\\/3',
							params: null,
							fragment: null,
							cache: false,
						},
						'/this/is/{{b}}/{{c}}/route/4': {
							name: 'name4',
							type: 'dynamic',
							route: '/this/is/{{b}}/{{c}}/route/4',
							urlpart: 'pathname',
							regex: '\\/this\\/is\\/(?<b>[\\d\\w?()|{}_.,-]+)\\/(?<c>[\\d\\w?()|{}_.,-]+)\\/route\\/4',
							params: null,
							fragment: null,
							cache: false,
						}
					},
					search: {
						'?id={{id}}&name={{name}}': {
							name: 'name5',
							type: 'dynamic',
							route: '?id={{id}}&name={{name}}',
							urlpart: 'search',
							regex: '\\?id=(?<id>[\\d\\w?()|{}_.,-]+)&name=(?<name>[\\d\\w?()|{}_.,-]+)',
							params: null,
							fragment: null,
							cache: false,
						}
					},
					hash: {
						'#im-{{foo}}': {
							name: 'name7',
							type: 'dynamic',
							route: '#im-{{foo}}',
							urlpart: 'hash',
							regex: '#im-(?<foo>[\\d\\w?()|{}_.,-]+)',
							params: null,
							fragment: null,
							cache: false,
						}
					}
				}
			});

		});

		it('should throw error if duplicate route added', () => {

			(() => { router._addRoute('/this/is/a/route/1', 'name1'); }).should.throw();
			(() => { router._addRoute('/this/is/a/route/2', 'name2'); }).should.throw();
			(() => { router._addRoute('/this/is/{{a}}/route/3', 'name3'); }).should.throw();
			(() => { router._addRoute('/this/is/{{b}}/{{c}}/route/4', 'name4'); }).should.throw();
			(() => { router._addRoute('?id={{id}}&name={{name}}', 'name5'); }).should.throw();
			(() => { router._addRoute('?name=serkan', 'name6'); }).should.throw();
			(() => { router._addRoute('#im-{{foo}}', 'name7'); }).should.throw();
			(() => { router._addRoute('#scroll-to-foo', 'name8'); }).should.throw();
		});

		it('should throw error if not valid route passed', () => {

			(() => { router._addRoute('/a/b?id=5', 'name1'); }).should.throw();
			(() => { router._addRoute('/a/b?id=5#foo', 'name2'); }).should.throw();
			(() => { router._addRoute('?id=5#foo', 'name3'); }).should.throw();
		});

	});

	describe('_matchStaticURL method', () => {

		it('should return matchedObject by passed fragment', () => {

			// setup
			let router = Router.create();
			router._addRoute('/this/is/a/route/1', 'route1');

			// test: positiv
			router._matchStaticURL('/this/is/a/route/1').should.containEql({
				name: 'route1',
{'fraction_non_alphanumeric': 0.25689032503326364, 'fraction_numerical': 0.009218779699676867, 'mean_word_length': 2.345945945945946, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '739468', 'n_tokens_mistral': 5094, 'n_tokens_neox': 3722, 'n_words': 615}