UNPKG

app-decorators

Version:

Collection of useful ES7 Decorators, writtin in ES6, that can be used for building webapps

1,622 lines (1,276 loc) 74.4 kB
System.register(['app-decorators/src/libs/element-to-function', '../../src/bootstrap', '../../src/libs/dependencies', '../mocks/event', '../../src/helpers/delay', 'jquery', '../../src/helpers/jquery.click-and-wait', '../../src/helpers/history.back-forward-and-wait', 'sinon'], function (_export, _context) { "use strict"; var _elementToFunc, bootstrapPolyfills, XRegExp, Event, delay, $, sinon, _createClass, _this; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } return { setters: [function (_appDecoratorsSrcLibsElementToFunction) { _elementToFunc = _appDecoratorsSrcLibsElementToFunction.default; }, function (_srcBootstrap) { bootstrapPolyfills = _srcBootstrap.bootstrapPolyfills; }, function (_srcLibsDependencies) { XRegExp = _srcLibsDependencies.XRegExp; }, function (_mocksEvent) { Event = _mocksEvent.Event; }, function (_srcHelpersDelay) { delay = _srcHelpersDelay.delay; }, function (_jquery) { $ = _jquery.default; }, function (_srcHelpersJqueryClickAndWait) {}, function (_srcHelpersHistoryBackForwardAndWait) {}, function (_sinon) { sinon = _sinon.default; }], execute: function () { _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); _this = this; describe('Class Router', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() { var _ref2, Router; return regeneratorRuntime.wrap(function _callee6$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return bootstrapPolyfills; case 2: _context7.next = 4; return System.import('src/apps/router'); case 4: _ref2 = _context7.sent; Router = _ref2.Router; describe('_isDynamicURL method', function () { it('should return true if has variable in url otherwise false', function () { // setup var router = Router.create(); // test router._isDynamicURL('{{a}}').should.be.true(); router._isDynamicURL('a').should.be.false(); // cleanup router.destroy(); }); }); describe('_convertURLToRegex method', function () { it('should convert passed url to regex', function () { // setup var 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('\\/\\|\\+\\*\\?\\.\\(\\)'); // test - strict router._convertRouteToXRegexExp('abc', true).should.be.equal('^abc$'); //cleanup router.destroy(); }); }); describe('_convertURLToRegex and match', function () { it('should match converted URLRegex', function () { // setup var router = Router.create(); // test var regex = router._convertRouteToXRegexExp('/a/{{b}}/c?id={{d}}&e#f&{{g}}'); var compiledRegex = router.RegExp('^' + regex + '$'); var result1 = compiledRegex.exec('/a/be/c?id=de&e#f&ge').groups(); result1.should.containEql({ b: "be", d: "de", g: "ge" }); var result2 = compiledRegex.exec('/a/be/c'); should(result2).be.null(); //cleanup router.destroy(); }); }); describe('_addRoute method', function () { // setup var router = null; beforeEach(function () { // 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(function () { return router.destroy(); }); it('should return registered routes', function () { router._routes.should.containEql({ pathname: { '/this/is/{{a}}/route/3': { name: 'name3', type: 'dynamic', route: '/this/is/{{a}}/route/3', handler: undefined, 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', handler: undefined, urlpart: 'pathname', regex: '^\\/this\\/is\\/(?<b>[\\d\\w?()|{}_.,-]+)\\/(?<c>[\\d\\w?()|{}_.,-]+)\\/route\\/4$', params: null, fragment: null, cache: false }, '/this/is/a/route/1': { name: 'name1', type: 'static', route: '/this/is/a/route/1', handler: undefined, urlpart: 'pathname', regex: '^\\/this\\/is\\/a\\/route\\/1$', params: null, fragment: null, cache: false }, '/this/is/a/route/2': { name: 'name2', type: 'static', route: '/this/is/a/route/2', handler: undefined, urlpart: 'pathname', regex: '^\\/this\\/is\\/a\\/route\\/2$', params: null, fragment: null, cache: false } }, search: { '?id={{id}}&name={{name}}': { name: 'name5', type: 'dynamic', route: '?id={{id}}&name={{name}}', handler: undefined, urlpart: 'search', regex: '\\?id=(?<id>[\\d\\w?()|{}_.,-]+)&name=(?<name>[\\d\\w?()|{}_.,-]+)', params: null, fragment: null, cache: false }, '?name=serkan': { name: 'name6', type: 'static', route: '?name=serkan', handler: undefined, urlpart: 'search', regex: '\\?name=serkan', params: null, fragment: null, cache: false } }, hash: { '#im-{{foo}}': { name: 'name7', type: 'dynamic', route: '#im-{{foo}}', handler: undefined, urlpart: 'hash', regex: '#im-(?<foo>[\\d\\w?()|{}_.,-]+)', params: null, fragment: null, cache: false }, '#scroll-to-foo': { name: 'name8', type: 'static', route: '#scroll-to-foo', handler: undefined, urlpart: 'hash', regex: '#scroll-to-foo', params: null, fragment: null, cache: false } } }); }); it('should throw error if duplicate route added', function () { (function () { return router._addRoute('/this/is/a/route/1', 'name1'); }).should.throw(); (function () { return router._addRoute('/this/is/a/route/2', 'name2'); }).should.throw(); (function () { return router._addRoute('/this/is/{{a}}/route/3', 'name3'); }).should.throw(); (function () { return router._addRoute('?id={{id}}&name={{name}}', 'name5'); }).should.throw(); (function () { return router._addRoute('?name=serkan', 'name6'); }).should.throw(); (function () { return router._addRoute('#im-{{foo}}', 'name7'); }).should.throw(); (function () { return router._addRoute('#scroll-to-foo', 'name8'); }).should.throw(); }); it('should throw error if not valid route passed', function () { (function () { return router._addRoute('/a/b?id=5', 'name1'); }).should.throw(); (function () { return router._addRoute('/a/b?id=5#foo', 'name2'); }).should.throw(); (function () { return router._addRoute('?id=5#foo', 'name3'); }).should.throw(); }); it('should throw error if eventName already exists', function () { (function () { return router._addRoute('/x/y?id=11', 'name1'); }).should.throw(); }); it('should throw error if not correct prefixed with "/, ?, #', function () { (function () { return router._addRoute('a/b.html', 'name1'); }).should.throw(); (function () { return router._addRoute('a=b', 'name2'); }).should.throw(); }); }); describe('_existsEvent method', function () { // setup var router = null; beforeEach(function () { // setup router = Router.create(); // add static routes router._addRoute('/this/is/a/route/1', 'name1'); router._addRoute('?id={{id}}&name={{name}}', 'name2'); router._addRoute('?name=serkan', 'name3'); router._addRoute('#im-{{foo}}', 'name4'); }); afterEach(function () { return router.destroy(); }); it('should check if event already exists', function () { router._existsEvent('name1').should.be.true(); router._existsEvent('name3').should.be.true(); }); it('should check if event not exists', function () { router._existsEvent('name5').should.be.false(); router._existsEvent('name6').should.be.false(); }); }); describe('_match method', function () { it('should return matchedObject by passed fragment', function () { // setup var router = Router.create(); router._addRoute('/{{a}}/b/{{c}}/d', 'route1'); router._addRoute('?id={{id}}&name={{name}}', 'route2'); router._addRoute('/this/is/a/route/1', 'route3'); router._addRoute('?name=serkan', 'route4'); // test 1: positiv router._match('/foo/b/bar/d', 'pathname').should.containEql({ name: 'route1', type: 'dynamic', route: '/{{a}}/b/{{c}}/d', handler: undefined, urlpart: 'pathname', regex: '^\\/(?<a>[\\d\\w?()|{}_.,-]+)\\/b\\/(?<c>[\\d\\w?()|{}_.,-]+)\\/d$', params: { a: 'foo', c: 'bar' }, fragment: '/foo/b/bar/d', cache: false }); // test 2: positiv router._match('?id=26&name=mars&update=true', 'search').should.containEql({ name: 'route2', type: 'dynamic', route: '?id={{id}}&name={{name}}', handler: undefined, urlpart: 'search', regex: '\\?id=(?<id>[\\d\\w?()|{}_.,-]+)&name=(?<name>[\\d\\w?()|{}_.,-]+)', params: { id: 26, name: 'mars' }, fragment: '?id=26&name=mars&update=true', cache: false }); // test 3: positiv router._match('/this/is/a/route/1', 'pathname').should.containEql({ name: 'route3', type: 'static', route: '/this/is/a/route/1', handler: undefined, urlpart: 'pathname', params: {}, regex: '^\\/this\\/is\\/a\\/route\\/1$', fragment: '/this/is/a/route/1', cache: false }); // test 4: positiv router._match('?name=serkan', 'search').should.containEql({ name: 'route4', type: 'static', route: '?name=serkan', handler: undefined, urlpart: 'search', params: {}, regex: '\\?name=serkan', fragment: '?name=serkan', cache: false }); // test: negativ should(router._match('/not/added/route', 'pathname')).be.exactly(null); should(router._match('?x=1', 'search')).be.exactly(null); should(router._match('#this-hash', 'hash')).be.exactly(null); // cleanup router.destroy(); }); }); describe('match method', function () { it.skip('it should match passed fragment', function () {}); }); describe('on method', function () { it.skip('it will pass always on match search and hash object', function () {}); }); describe('on method', function () { it.skip('cli commands', function () { router.on('myRoute5 ?c=d&e=f --no-history', function () {}); }); }); describe('_normalizeMatchedXRegex method', function () { it('it should normalize matched XRegex object', function () { // setup var router = Router.create(); var mockXRegexMatchedObject = { a: '123', b: 'be', c: 456, d: '1.34' }; router._normalizeMatchedXRegex(mockXRegexMatchedObject).should.containEql({ a: 123, b: 'be', c: 456, d: 1.34 }); // cleanup router.destroy(); }); }); describe('_matchURL method (integration test)', function () { var router = null; it('should return only valid value from _match by passed static path', function () { // setup router = Router.create(); router._addRoute('/some/static/path', 'route1'); // test: it should call _match router._matchURL('/some/static/path', ['pathname']); router._match.returnValues[0].name.should.be.equal('route1'); router._match.returnValues[0].cache.should.be.false(); should(router._getRouteCache.returnValues[0]).be.null(); }); it('should return only valid value from _match by passed dynamic path', function () { // setup router = Router.create(); router._addRoute('/{{dynamic}}/b/{{path}}/d', 'route2'); // test: it should call _match router._matchURL('/hey/b/there/d', ['pathname']); router._match.returnValues[0].name.should.be.equal('route2'); router._match.returnValues[0].cache.should.be.false(); should(router._getRouteCache.returnValues[0]).be.null(); }); it('should return on second call from cache by passed dynamic path', function () { // setup router = Router.create(); router._addRoute('/{{dynamic}}/b/{{path}}/d', 'route2'); // test: returned not from cache router._matchURL('/hey/b/there/d', ['pathname']); router._match.returnValues[0].cache.should.be.false(); // test: returned from cache router._matchURL('/hey/b/there/d', ['pathname']); router._getRouteCache.returnValues[1].cache.should.be.true(); }); it('should return array with matched object', function () { // setup router = Router.create(); router._addRoute('/{{dynamic}}/b/{{path}}/d', 'route1'); router._addRoute('?name=test', 'route2'); router._addRoute('#im-hash', 'route3'); // test 1 router._matchURL('/hey/b/there/d', ['pathname'])[0].should.containEql({ name: 'route1', cache: false }); // test 2 router._matchURL('?name=test&whats=up', ['search'])[0].should.containEql({ name: 'route2', cache: false }); }); it('should return array with matched parts', function () { // setup var changepart = void 0, matchedResults = void 0; router = Router.create(); router._addRoute('/{{dynamic}}/b/{{path}}/d', 'route1'); router._addRoute('?name={{name}}', 'route2'); router._addRoute('#im-hash', 'route3'); // test 1 if only changed changepart = ['pathname', 'search', 'hash']; matchedResults = router._matchURL('/hey/b/there/d?name=serkan#im-hash', changepart); matchedResults.should.be.length(3); matchedResults[0].should.containEql({ name: 'route1' }); matchedResults[1].should.containEql({ name: 'route2' }); matchedResults[2].should.containEql({ name: 'route3' }); // test 2 if only changed changepart = ['search', 'hash']; matchedResults = router._matchURL('/hey/b/there/d?name=serkan#im-hash', changepart); matchedResults.should.be.length(2); matchedResults[0].should.containEql({ name: 'route2' }); matchedResults[1].should.containEql({ name: 'route3' }); }); // setup beforeEach(function () { sinon.spy(Router.prototype, '_match'); sinon.spy(Router.prototype, '_getRouteCache'); }); afterEach(function () { router._match.restore(); router._getRouteCache.restore(); router.destroy(); }); }); describe('_applyActionEvent (integration test) method', function () { /** * We check only pushstate inside of _applyActionEvent, * all other methods are tested. */ it('should call pushstate if click event passed', function () { // setup var router = Router.create({ event: { action: 'click a' } }); var spy_pushState = sinon.spy(router, "pushState"); // mock click event var event = new Event('click', { target: { href: 'http://www.domain.com/some/path.html' } }); // test router._applyActionEvent(event); spy_pushState.callCount.should.equal(1); // cleanup router.pushState.restore(); router.destroy(); }); it('should not call pushstate if pushstate event passed', function () { // setup var router = Router.create({ event: { action: 'click a' } }); var spy_pushState = sinon.spy(router, "pushState"); // mock pushState vent var event = new Event('pushState', { target: { href: 'http://www.domain.com/some/path.html' } }); // test router._applyActionEvent(event); spy_pushState.callCount.should.equal(0); // cleanup router.pushState.restore(); router.destroy(); }); }); describe('_getDefinedEventAction method', function () { it('should get defined event action', function () { //setup var router = Router.create({ event: { action: 'my-action pattern' } }); // test router._getDefinedEventAction().should.be.equal('my-action'); // cleanup router.destroy(); }); }); describe('_isDefinedEventAction method', function () { it('should check if passed event_type is euqal to our defined event type', function () { // setup var router = Router.create({ event: { action: 'my-action pattern' } }); // test router._isDefinedEventAction('my-action').should.be.true(); router._isDefinedEventAction('other-action').should.be.false(); // cleanup router.destroy(); }); }); describe('_getCurrentHref method', function () { it('should get current href by passed event', function () { // set mock location.href history.replaceState({}, 'mock1', '/event/pushState/href.html'); var currentDomain = location.protocol + '//' + location.host; // setup var router = Router.create({ event: { action: 'myEvent a' } }); //*** Test click event *** // mock click event ( see above import) var eventClickMock = new Event('myEvent', { target: { href: 'http://mockdomain.com/event/click/href.html' } }); router._getCurrentHref(eventClickMock).should.be.equal('http://mockdomain.com/event/click/href.html'); //*** Test pushstate event *** // mock pushstate event ( see above import) var eventPushstateMock = new Event('pushstate'); router._getCurrentHref(eventPushstateMock).should.be.equal(currentDomain + '/event/pushState/href.html'); // cleanup router.destroy(); }); }); describe('_addPromise method', function () { it('should add and create promise collection on prototype.promise', function () { // setup var router = Router.create(); var promise1 = router._addPromise('name1'); var promise2 = router._addPromise('name2'); // test 1 promise1.should.be.Promise(); promise2.should.be.Promise(); // test 2 router.promise.should.have.propertyByPath('name1', 0).and.is.Function(); // resolve function router.promise.should.have.propertyByPath('name2', 0).and.is.Function(); // resolve function // cleanup: resolve promises router.promise.name1[0](); router.promise.name2[0](); // cleanup: destroy router router.destroy(); }); }); describe('_convertFragmentToParts method', function () { it('should convert fragment into parts', function () { // setup var router = Router.create(); // test 1 router._convertFragmentToParts('/a/b/c').should.containEql({ pathname: '/a/b/c', search: '', hash: '' }); // test 2 router._convertFragmentToParts('/a/b/c?a=1&b=2').should.containEql({ pathname: '/a/b/c', search: '?a=1&b=2', hash: '' }); // test 3 router._convertFragmentToParts('/a/b/c?a=1&b=2#hello-world').should.containEql({ pathname: '/a/b/c', search: '?a=1&b=2', hash: '#hello-world' }); // test 4 router._convertFragmentToParts('/a/b/c?a=1&b=2').should.containEql({ pathname: '/a/b/c', search: '?a=1&b=2', hash: '' }); // test 5 router._convertFragmentToParts('/?a=1&b=2#hello-world').should.containEql({ pathname: '/', search: '?a=1&b=2', hash: '#hello-world' }); // test 6 router._convertFragmentToParts('?a=1&b=2#hello-world').should.containEql({ pathname: '/', search: '?a=1&b=2', hash: '#hello-world' }); // test 7 router._convertFragmentToParts('#hello-world').should.containEql({ pathname: '/', search: '', hash: '#hello-world' }); // test 8 router._convertFragmentToParts('/#hello-world').should.containEql({ pathname: '/', search: '', hash: '#hello-world' }); // test 9 router._convertFragmentToParts('/').should.containDeep({ pathname: '/', search: '', hash: '' }); // cleanup router.destroy(); }); }); describe('_diffFragmentParts method', function () { it('should diff passed object', function () { // setup var router = Router.create(); var fragmentPart1 = void 0, fragmentPart2 = void 0; // test 1 fragmentPart1 = { pathname: '/', search: '', hash: '' }; fragmentPart2 = { pathname: '/', search: '', hash: '' }; router._diffFragmentParts(fragmentPart1, fragmentPart2).should.containDeep([]); // test 2 fragmentPart1 = { pathname: '/', search: '', hash: '' }; fragmentPart2 = { pathname: '/', search: 'b', hash: '' }; router._diffFragmentParts(fragmentPart1, fragmentPart2).should.containDeep(['search']); router._diffFragmentParts(fragmentPart1, fragmentPart2).should.not.containDeep(['pathname', 'hash']); // test 3 fragmentPart1 = { pathname: '/', search: '', hash: '' }; fragmentPart2 = { pathname: '/a', search: 'b', hash: '' }; router._diffFragmentParts(fragmentPart1, fragmentPart2).should.containDeep(['pathname', 'search']); router._diffFragmentParts(fragmentPart1, fragmentPart2).should.not.containDeep(['hash']); // test 4 fragmentPart1 = { pathname: '/', search: '', hash: '' }; fragmentPart2 = { pathname: '/a', search: 'b', hash: 'c' }; router._diffFragmentParts(fragmentPart1, fragmentPart2).should.containDeep(['pathname', 'search', 'hash']); // cleanup router.destroy(); }); }); describe('_diffFragments method', function () { it('should check if fragment is changed in combination with _setURLFragment', function () { // setup var router = Router.create(); // test 1 router._diffFragments('/', '/a/b/c').should.containEql({ changed: true, changepart: ['pathname'] }); // test 2 router._diffFragments('/a/c/c', '/a/c/c?a=b&c=d#foo').should.containEql({ changed: true, changepart: ['search', 'hash'] }); // test 3 router._diffFragments('/a/c/c?a=b&c=d#foo', '/a/c/c?a=b&c=d#foo').should.containEql({ changed: false, changepart: [] }); // test 4 router._diffFragments('/a/c/c?a=b&c=d#foo', '/a/c/e?a=b&c=d#foo').should.containEql({ changed: true, changepart: ['pathname'] }); // cleanup router.destroy(); }); }); describe('_isNumeric', function () { it('should check if passed value is number or not', function () { // setup var router = Router.create(); // test positiv: integer router._isNumeric(123).should.be.true(); router._isNumeric('123').should.be.true(); router._isNumeric(123.45).should.be.true(); router._isNumeric('123.45').should.be.true(); // test negativ: integer router._isNumeric('123.45a').should.be.false(); router._isNumeric('123b').should.be.false(); // cleanup router.destroy(); }); }); describe('_immutable', function () { it('should make passed object immutable', function () { // setup var myObject = { a: 1 }; var router = Router.create(); router._immutable(myObject).should.not.equal(myObject); }); }); describe('createURL method', function () { it('should return url object with additional "fragment" property', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test var url = router.createURL('http://www.mydomain.com/path/to/somewhere.html?a=1&b=2#foo'); url.pathname.should.equal('/path/to/somewhere.html'); url.search.should.equal('?a=1&b=2'); url.hash.should.equal('#foo'); url.fragment.should.equal('/path/to/somewhere.html?a=1&b=2#foo'); // cleanup router.destroy(); }); }); describe('guid method helper', function () { it('should generate uid', function () { // setup var router1 = Router.create({ scope: document.createElement('div') }); var router2 = Router.create({ scope: document.createElement('div') }); // test 1 router1._uid.match(/^[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+$/); router1._uid.should.not.be.equal(router2._uid); // test 2 var uid1 = router1.guid(); var uid2 = router1.guid(); uid1.should.match(/^[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+$/); uid1.should.not.be.equal(uid2); // cleanup router1.destroy(); router2.destroy(); }); }); describe('addRouteListener method', function () { it('should do the same like prototype.on', function () { // It´s not necessary to test it, it´s almost the same // like prototype.on. The only difference is // the argument signature }); }); describe('On method', function () { it('should throw error if passed handler is not Function or undefined', function () { // setup var router = Router.create({ scope: document.createElement('div') }); (function () { return router.on('route /a/b/c.html', null); }).should.throw(); (function () { return router.on('route /a/b/c.html', ''); }).should.throw(); (function () { return router.on('route /a/b/c.html', 123); }).should.throw(); (function () { return router.on('route /a/b/c.html', true); }).should.throw(); (function () { return router.on('route /a/b/c.html', []); }).should.throw(); (function () { return router.on('route /a/b/c.html', {}); }).should.throw(); (function () { return router.on('route /a/b/c.html', function () {}); }).should.not.throw(); (function () { return router.on('route /a/b/c.html', undefined); }).should.not.throw(); // cleanup router.destroy(); }); }); describe('stop method', function () { it('it should stop if router running', function () { // setup sinon.spy(Router.prototype, '_applyActionEvent'); var router = Router.create({ scope: document.createElement('div') }); router.on('action', router._onAction.bind(router)); // test router.stop(); router.trigger('action'); router._applyActionEvent.callCount.should.be.equal(0); //cleanup router._applyActionEvent.restore(); router.destroy(); }); }); describe('start method', function () { it('it should start if router stoped', function () { // setup sinon.spy(Router.prototype, '_applyActionEvent'); var router = Router.create({ scope: document.createElement('div') }); router.on('action', router._onAction.bind(router)); // test 1 router.stop(); router.trigger('action'); router._applyActionEvent.callCount.should.be.equal(0); // test 2 router.start(); router.trigger('action'); router._applyActionEvent.callCount.should.be.equal(1); // cleanup router._applyActionEvent.restore(); router.destroy(); }); }); describe('whoami method', function () { it('it should return route information by passed fragment', function () { function func1() {} function func2() {} function func3() {} function func4() {} function func5() {} function func6() {} // setup var router = Router.create(), result = void 0; router.on('myRoute1 /', func1); router.on('myRoute2 /some/path.html', func2); router.on('myRoute3 /some/{{integer}}/{{float}}/path.html', func3); router.on('myRoute4 /{{a}}/b', func4); router.on('myRoute5 ?c=d&e=f', func5); router.on('myRoute6 #hash-{{hash}}', func6); // test 1 - positiv result = router.whoami('/'); result.should.be.length(1); result[0].should.be.containEql({ name: 'myRoute1', handler: func1, params: {}, cache: false, fragment: '/' }); // test 2 - positiv result = router.whoami('/some/path.html'); result.should.be.length(1); result[0].should.be.containEql({ name: 'myRoute2', handler: func2, params: {}, cache: false, fragment: '/some/path.html' }); // test 3 - positiv result = router.whoami('/some/123/4.34/path.html'); result.should.be.length(1); result[0].should.be.containEql({ name: 'myRoute3', handler: func3, params: { integer: 123, float: 4.34 }, cache: false, fragment: '/some/123/4.34/path.html' }); // test 4 - positiv result = router.whoami('/im/b?c=d&e=f#hash-tag'); result.should.be.length(3); result[0].should.be.containEql({ name: 'myRoute4', handler: func4, params: { a: 'im' }, cache: false, fragment: '/im/b' }); result[1].should.be.containEql({ name: 'myRoute5', handler: func5, params: {}, cache: false, fragment: '?c=d&e=f' }); result[2].should.be.containEql({ name: 'myRoute6', handler: func6, params: { hash: 'tag' }, cache: false, fragment: '#hash-tag' }); // test 3 - negativ router.whoami('/im/b/c').should.be.length(0); router.whoami('/unknown/path.html').should.be.length(0); // cleanup router.destroy(); }); }); describe('which method', function () { it('it should return route information by passed name', function () { function func1() {} function func2() {} // setup var router = Router.create(); router.on('myRoute1 /some/{{integer}}/{{float}}/path.html', func1); router.on('myRoute2 /a/b/c.html', func2); // test 1 - positiv router.which('myRoute1').should.be.containEql({ name: 'myRoute1', type: 'dynamic', handler: func1, fragment: null, route: '/some/{{integer}}/{{float}}/path.html', urlpart: 'pathname', regex: '^\\/some\\/(?<integer>[\\d\\w?()|{}_.,-]+)\\/(?<float>[\\d\\w?()|{}_.,-]+)\\/path\\.html$', params: null, cache: false }); // test 2 - negativ should(router.which('unknownRoute')).be.exactly(null); // cleanup router.destroy(); }); }); describe('_constructDynamicURL method', function () { it('should construct url by passt dynamic routename', function () { // setup var router = Router.create(); // test 1 router._constructDynamicURL('/person/{{name}}/{{surename}}/id/{{id}}', { name: 'serkan', surename: 'sipahi', id: 333 }).should.equal('/person/serkan/sipahi/id/333'); // test 2 router._constructDynamicURL('?page={{page}}&id={{id}}', { page: 'details', id: 999 }).should.equal('?page=details&id=999'); // cleanup router.destroy(); }); it('should throw error if something gone wrong', function () { // setup var router = Router.create(); // should throw if not params passed (function () { return router._constructDynamicURL('/person/{{name}}'); }).should.throw(); // should throw if param is missing (function () { return router._constructDynamicURL('/person/{{name}}', { id: 1 }); }).should.throw(); // cleanup router.destroy(); }); }); describe('_constructStaticURL method', function () { it('should return static url', function () { // setup var router = Router.create(); // test 1 router._constructStaticURL('/static/path/').should.equal('/static/path/'); // cleanup router.destroy(); }); }); describe('_constructURL method', function () { it('should call _constructStaticURL if static route passed', function () { // setup sinon.spy(Router.prototype, '_constructStaticURL'); sinon.spy(Router.prototype, '_constructDynamicURL'); var router = Router.create(); router.on('myRoute /static/path', function () {}); // test 1 router._constructURL('myRoute'); router._constructStaticURL.callCount.should.equal(1); router._constructDynamicURL.callCount.should.equal(0); // cleanup router.destroy(); router._constructStaticURL.restore(); router._constructDynamicURL.restore(); }); it('should call _constructDynamicURL if dynamic route passed', function () { // setup sinon.spy(Router.prototype, '_constructStaticURL'); sinon.spy(Router.prototype, '_constructDynamicURL'); var router = Router.create(); router.on('myRoute /{{dynamic}}/{{path}}', function () {}); // test 1 router._constructURL('myRoute', { dynamic: 'hello', path: 'world' }); router._constructStaticURL.callCount.should.equal(0); router._constructDynamicURL.callCount.should.equal(1); // cleanup router.destroy(); router._constructStaticURL.restore(); router._constructDynamicURL.restore(); }); it('should throw error if no route passed', function () { // setup var router = Router.create(); // test 1 (function () { return router._constructURL(); }).should.throw(); // cleanup router.destroy(); }); it('should throw error if route not exists', function () { // setup var router = Router.create(); // test 1 (function () { return router._constructURL('notExistsRoute'); }).should.throw(); // cleanup router.destroy(); }); }); describe('constructURL method', function () { it('should construct url by passed static routename', function () { // setup var router = Router.create(); router.on('myRoute1 /some/static/path.html', function () {}); // test 1 router.constructURL('myRoute1').should.be.equal('/some/static/path.html'); // test 2 - ingores params router.constructURL('myRoute1', { a: 1, b: 2 }).should.be.equal('/some/static/path.html'); // cleanup router.destroy(); }); it('should construct url by passed dynmic routename and params ', function () { // setup var router = Router.create(); router.on('myRoute1 /{{static}}/path/{{id}}.html', function () {}); // test 1 router.constructURL('myRoute1', { static: 'foo', id: 44 }).should.be.equal('/foo/path/44.html'); router.constructURL('myRoute1', { static: 'bar', id: 66 }).should.be.equal('/bar/path/66.html'); // cleanup router.destroy(); }); }); describe('go method', function () { it('should go by passed static routename', function () { //reset location.href history.pushState(null, null, '/'); var router = Router.create(); router.on('myRoute1 /some/static/path.html', function () {}); sinon.spy(router, "pushState"); // test 1 router.go('myRoute1'); var url = router.createURL(location.href); url.fragment.should.equal('/some/static/path.html'); // test 2 - ingores params router.go('myRoute1', { a: 1, b: 2 }); router.pushState.callCount.should.be.equal(2); router.pushState.restore(); router.destroy(); }); it('should go by passed dynmic routename and params ', function () { //reset location.href history.pushState(null, null, '/'); var router = Router.create(); router.on('myRoute1 /{{static}}/{{id}}.html', function () {}); sinon.spy(router, "pushState"); // test 1 router.go('myRoute1', { static: 'foo', id: 44 }); router.pushState.callCount.should.be.equal(1); var url = router.createURL(location.href); url.fragment.should.equal('/foo/44.html'); router.pushState.restore(); router.destroy(); }); }); describe('redirect method', function () { it('description', function () {}); }); describe('on method, no arguments passed', function () { it('should throw an error', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test (function () { return router.on(); }).should.throw(); // cleanup router.destroy(); }); }); describe('on method, no handler passed', function () { it('should not throw an error', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test (function () { router.on('urlchange'); }).should.not.throw(); // cleanup router.destroy(); }); }); describe('on method, eventType and handler passed', function () { it('should not throw an error', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test (function () { router.on('someevent', function () {}); }).should.not.throw(); // cleanup router.destroy(); }); }); describe('on method, passed only eventType', function () { it('should return promise', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test router.on('urlchange').should.be.Promise(); router.on('urlchange').then(function () {}).should.be.Promise(); // cleanup router.destroy(); }); }); describe('on method, passed eventType and handler', function () { it('should return null', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // test should(router.on('myRoute', function (event) {})).be.null(); // cleanup router.destroy(); }); }); describe('on method', function () { it('should handle promise or handler correctly', _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var router, spy_handler; return regeneratorRuntime.wrap(function _callee$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: // setup router = Router.create({ scope: document.createElement('div') }); spy_handler = sinon.spy(function () {}); router.on('urlchange', spy_handler); // test if promise resolved router.on('urlchange').should.be.finally.propertyByPath('thats').eql('nice'); router.on('urlchange').should.be.finally.propertyByPath('thats').eql('nice'); router.on('urlchange').then(function (_ref4) { var thats = _ref4.thats; return '#' + thats + '#'; }).should.be.finally.eql('#nice#'); // test promise count router.promise.urlchange.should.be.length(3); // test triggering of urlchange router.trigger('urlchange', { thats: 'nice' }); spy_handler.callCount.should.equal(1); router.promise.urlchange.should.be.length(0); // test with trigger args router.trigger('urlchange', { hello: 'world' }); spy_handler.callCount.should.equal(2); spy_handler.args[1][0].should.propertyByPath('hello').eql('world'); _context2.next = 15; return delay(1); case 15: // cleanup router.destroy(); case 16: case 'end': return _context2.stop(); } } }, _callee, _this); }))); }); describe('on method, registered many events', function () { it('should assign routes correctly', function () { // setup var router = Router.create({ scope: document.createElement('div') }); router.on('Startpage /index.html', function () {}); router.on('Resultpage /results.html', function () {}); router.on('Detailpage /details.html', function () {}); router.on('Configurator /configurator.html', function () {}); // test routes count // INFO: _getRoutes umbenennen in getRoutes Object.keys(router._routes.pathname).should.be.length(4); // should throw error because route is already exists (function () { return router.on('Detailpage /details.html', function () {}); }).should.throw(); // cleanup router.destroy(); }); }); describe('on method, if explicitly triggering without params', function () { it('should trigger correct handler', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // spyies var spy_startpage_handler = sinon.spy(function () {}); var spy_resultpage_handler = sinon.spy(function () {}); router.on('Startpage /index.html', spy_startpage_handler); router.on('Resultpage /results.html', spy_resultpage_handler); router.trigger('Startpage'); router.trigger('Resultpage'); router.trigger('Resultpage'); spy_startpage_handler.callCount.should.equal(1); spy_resultpage_handler.callCount.should.equal(2); // cleanup router.destroy(); }); }); describe('on method, explicitly triggering with params', function () { it('should trigger correct handler', function () { // setup var router = Router.create({ scope: document.createElement('div') }); // spyies var spy_configurator_handler = sinon.spy(function () {}); router.on('Product /product/{{id}}-{{name}}.html', spy_configurator_handler); // test: positiv router.trigger('Product', {