router.js (lodash-4.0.0) | : | router.js (lodash-4.17.21) | ||
---|---|---|---|---|
(function() { | (function(QUnit) { | |||
var router = null; | var router = null; | |||
var location = null; | var location = null; | |||
var lastRoute = null; | var lastRoute = null; | |||
var lastArgs = []; | var lastArgs = []; | |||
var onRoute = function(router, route, args) { | var onRoute = function(routerParam, route, args) { | |||
lastRoute = route; | lastRoute = route; | |||
lastArgs = args; | lastArgs = args; | |||
}; | }; | |||
var Location = function(href) { | var Location = function(href) { | |||
this.replace(href); | this.replace(href); | |||
}; | }; | |||
_.extend(Location.prototype, { | _.extend(Location.prototype, { | |||
skipping to change at line 31 | skipping to change at line 31 | |||
replace: function(href) { | replace: function(href) { | |||
this.parser.href = href; | this.parser.href = href; | |||
_.extend(this, _.pick(this.parser, | _.extend(this, _.pick(this.parser, | |||
'href', | 'href', | |||
'hash', | 'hash', | |||
'host', | 'host', | |||
'search', | 'search', | |||
'fragment', | 'fragment', | |||
'pathname', | 'pathname', | |||
'protocol' | 'protocol' | |||
)); | )); | |||
// In IE, anchor.pathname does not contain a leading slash though | // In IE, anchor.pathname does not contain a leading slash though | |||
// window.location.pathname does. | // window.location.pathname does. | |||
if (!/^\//.test(this.pathname)) this.pathname = '/' + this.pathname; | if (!/^\//.test(this.pathname)) this.pathname = '/' + this.pathname; | |||
}, | }, | |||
toString: function() { | toString: function() { | |||
return this.href; | return this.href; | |||
} | } | |||
}); | }); | |||
QUnit.module('Backbone.Router', { | QUnit.module('Backbone.Router', { | |||
setup: function() { | beforeEach: function() { | |||
location = new Location('http://example.com'); | location = new Location('http://example.com'); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
router = new Router({testing: 101}); | router = new Router({testing: 101}); | |||
Backbone.history.interval = 9; | Backbone.history.interval = 9; | |||
Backbone.history.start({pushState: false}); | Backbone.history.start({pushState: false}); | |||
lastRoute = null; | lastRoute = null; | |||
lastArgs = []; | lastArgs = []; | |||
Backbone.history.on('route', onRoute); | Backbone.history.on('route', onRoute); | |||
}, | }, | |||
teardown: function() { | afterEach: function() { | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history.off('route', onRoute); | Backbone.history.off('route', onRoute); | |||
} | } | |||
}); | }); | |||
var ExternalObject = { | var ExternalObject = { | |||
value: 'unset', | value: 'unset', | |||
routingFunction: function(value) { | routingFunction: function(value) { | |||
skipping to change at line 98 | skipping to change at line 99 | |||
'named/optional/(y:z)': 'namedOptional', | 'named/optional/(y:z)': 'namedOptional', | |||
'splat/*args/end': 'splat', | 'splat/*args/end': 'splat', | |||
':repo/compare/*from...*to': 'github', | ':repo/compare/*from...*to': 'github', | |||
'decode/:named/*splat': 'decode', | 'decode/:named/*splat': 'decode', | |||
'*first/complex-*part/*rest': 'complex', | '*first/complex-*part/*rest': 'complex', | |||
'query/:entity': 'query', | 'query/:entity': 'query', | |||
'function/:value': ExternalObject.routingFunction, | 'function/:value': ExternalObject.routingFunction, | |||
'*anything': 'anything' | '*anything': 'anything' | |||
}, | }, | |||
preinitialize: function(options) { | ||||
this.testpreinit = 'foo'; | ||||
}, | ||||
initialize: function(options) { | initialize: function(options) { | |||
this.testing = options.testing; | this.testing = options.testing; | |||
this.route('implicit', 'implicit'); | this.route('implicit', 'implicit'); | |||
}, | }, | |||
counter: function() { | counter: function() { | |||
this.count++; | this.count++; | |||
}, | }, | |||
implicit: function() { | implicit: function() { | |||
skipping to change at line 124 | skipping to change at line 129 | |||
}, | }, | |||
charUTF: function() { | charUTF: function() { | |||
this.charType = 'UTF'; | this.charType = 'UTF'; | |||
}, | }, | |||
charEscaped: function() { | charEscaped: function() { | |||
this.charType = 'escaped'; | this.charType = 'escaped'; | |||
}, | }, | |||
contacts: function(){ | contacts: function() { | |||
this.contact = 'index'; | this.contact = 'index'; | |||
}, | }, | |||
newContact: function(){ | newContact: function() { | |||
this.contact = 'new'; | this.contact = 'new'; | |||
}, | }, | |||
loadContact: function(){ | loadContact: function() { | |||
this.contact = 'load'; | this.contact = 'load'; | |||
}, | }, | |||
optionalItem: function(arg){ | optionalItem: function(arg) { | |||
this.arg = arg != void 0 ? arg : null; | this.arg = arg !== void 0 ? arg : null; | |||
}, | }, | |||
splat: function(args) { | splat: function(args) { | |||
this.args = args; | this.args = args; | |||
}, | }, | |||
github: function(repo, from, to) { | github: function(repo, from, to) { | |||
this.repo = repo; | this.repo = repo; | |||
this.from = from; | this.from = from; | |||
this.to = to; | this.to = to; | |||
skipping to change at line 184 | skipping to change at line 189 | |||
routeEvent: function(arg) { | routeEvent: function(arg) { | |||
} | } | |||
}); | }); | |||
QUnit.test('initialize', function(assert) { | QUnit.test('initialize', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
assert.equal(router.testing, 101); | assert.equal(router.testing, 101); | |||
}); | }); | |||
QUnit.test('preinitialize', function(assert) { | ||||
assert.expect(1); | ||||
assert.equal(router.testpreinit, 'foo'); | ||||
}); | ||||
QUnit.test('routes (simple)', function(assert) { | QUnit.test('routes (simple)', function(assert) { | |||
assert.expect(4); | assert.expect(4); | |||
location.replace('http://example.com#search/news'); | location.replace('http://example.com#search/news'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
assert.equal(router.query, 'news'); | assert.equal(router.query, 'news'); | |||
assert.equal(router.page, void 0); | assert.equal(router.page, void 0); | |||
assert.equal(lastRoute, 'search'); | assert.equal(lastRoute, 'search'); | |||
assert.equal(lastArgs[0], 'news'); | assert.equal(lastArgs[0], 'news'); | |||
}); | }); | |||
skipping to change at line 237 | skipping to change at line 247 | |||
Backbone.history.navigate('search/manhattan/p20', true); | Backbone.history.navigate('search/manhattan/p20', true); | |||
assert.equal(router.query, 'manhattan'); | assert.equal(router.query, 'manhattan'); | |||
assert.equal(router.page, '20'); | assert.equal(router.page, '20'); | |||
}); | }); | |||
QUnit.test('reports matched route via nagivate', function(assert) { | QUnit.test('reports matched route via nagivate', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
assert.ok(Backbone.history.navigate('search/manhattan/p20', true)); | assert.ok(Backbone.history.navigate('search/manhattan/p20', true)); | |||
}); | }); | |||
QUnit.test('route precedence via navigate', function(assert){ | QUnit.test('route precedence via navigate', function(assert) { | |||
assert.expect(6); | assert.expect(6); | |||
// check both 0.9.x and backwards-compatibility options | ||||
_.each([{trigger: true}, true], function( options ){ | // Check both 0.9.x and backwards-compatibility options | |||
_.each([{trigger: true}, true], function(options) { | ||||
Backbone.history.navigate('contacts', options); | Backbone.history.navigate('contacts', options); | |||
assert.equal(router.contact, 'index'); | assert.equal(router.contact, 'index'); | |||
Backbone.history.navigate('contacts/new', options); | Backbone.history.navigate('contacts/new', options); | |||
assert.equal(router.contact, 'new'); | assert.equal(router.contact, 'new'); | |||
Backbone.history.navigate('contacts/foo', options); | Backbone.history.navigate('contacts/foo', options); | |||
assert.equal(router.contact, 'load'); | assert.equal(router.contact, 'load'); | |||
}); | }); | |||
}); | }); | |||
QUnit.test('loadUrl is not called for identical routes.', function(assert) { | QUnit.test('loadUrl is not called for identical routes.', function(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
Backbone.history.loadUrl = function(){ assert.ok(false); }; | Backbone.history.loadUrl = function() { assert.ok(false); }; | |||
location.replace('http://example.com#route'); | location.replace('http://example.com#route'); | |||
Backbone.history.navigate('route'); | Backbone.history.navigate('route'); | |||
Backbone.history.navigate('/route'); | Backbone.history.navigate('/route'); | |||
Backbone.history.navigate('/route'); | Backbone.history.navigate('/route'); | |||
}); | }); | |||
QUnit.test('use implicit callback if none provided', function(assert) { | QUnit.test('use implicit callback if none provided', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
router.count = 0; | router.count = 0; | |||
router.navigate('implicit', {trigger: true}); | router.navigate('implicit', {trigger: true}); | |||
skipping to change at line 348 | skipping to change at line 359 | |||
}); | }); | |||
QUnit.test('Decode named parameters, not splats.', function(assert) { | QUnit.test('Decode named parameters, not splats.', function(assert) { | |||
assert.expect(2); | assert.expect(2); | |||
location.replace('http://example.com#decode/a%2Fb/c%2Fd/e'); | location.replace('http://example.com#decode/a%2Fb/c%2Fd/e'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
assert.strictEqual(router.named, 'a/b'); | assert.strictEqual(router.named, 'a/b'); | |||
assert.strictEqual(router.path, 'c/d/e'); | assert.strictEqual(router.path, 'c/d/e'); | |||
}); | }); | |||
QUnit.test("fires event when router doesn't have callback on it", function(ass ert) { | QUnit.test('fires event when router doesn\'t have callback on it', function(as sert) { | |||
assert.expect(1); | assert.expect(1); | |||
router.on('route:noCallback', function(){ assert.ok(true); }); | router.on('route:noCallback', function() { assert.ok(true); }); | |||
location.replace('http://example.com#noCallback'); | location.replace('http://example.com#noCallback'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
}); | }); | |||
QUnit.test('No events are triggered if #execute returns false.', function(asse rt) { | QUnit.test('No events are triggered if #execute returns false.', function(asse rt) { | |||
assert.expect(1); | assert.expect(1); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
foo: function() { | foo: function() { | |||
assert.ok(true); | assert.ok(true); | |||
} | } | |||
}, | }, | |||
execute: function(callback, args) { | execute: function(callback, args) { | |||
callback.apply(this, args); | callback.apply(this, args); | |||
return false; | return false; | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
router.on('route route:foo', function() { | myRouter.on('route route:foo', function() { | |||
assert.ok(false); | assert.ok(false); | |||
}); | }); | |||
Backbone.history.on('route', function() { | Backbone.history.on('route', function() { | |||
assert.ok(false); | assert.ok(false); | |||
}); | }); | |||
location.replace('http://example.com#foo'); | location.replace('http://example.com#foo'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
}); | }); | |||
skipping to change at line 539 | skipping to change at line 550 | |||
}); | }); | |||
}); | }); | |||
QUnit.test('Normalize root - leading slash.', function(assert) { | QUnit.test('Normalize root - leading slash.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/root'); | location.replace('http://example.com/root'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(){} | replaceState: function() {} | |||
} | } | |||
}); | }); | |||
Backbone.history.start({root: 'root'}); | Backbone.history.start({root: 'root'}); | |||
assert.strictEqual(Backbone.history.root, '/root/'); | assert.strictEqual(Backbone.history.root, '/root/'); | |||
}); | }); | |||
QUnit.test('Transition from hashChange to pushState.', function(assert) { | QUnit.test('Transition from hashChange to pushState.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/root#x/y'); | location.replace('http://example.com/root#x/y'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(state, title, url){ | replaceState: function(state, title, url) { | |||
assert.strictEqual(url, '/root/x/y'); | assert.strictEqual(url, '/root/x/y'); | |||
} | } | |||
} | } | |||
}); | }); | |||
Backbone.history.start({ | Backbone.history.start({ | |||
root: 'root', | root: 'root', | |||
pushState: true | pushState: true | |||
}); | }); | |||
}); | }); | |||
QUnit.test('#1619: Router: Normalize empty root', function(assert) { | QUnit.test('#1619: Router: Normalize empty root', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/'); | location.replace('http://example.com/'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(){} | replaceState: function() {} | |||
} | } | |||
}); | }); | |||
Backbone.history.start({root: ''}); | Backbone.history.start({root: ''}); | |||
assert.strictEqual(Backbone.history.root, '/'); | assert.strictEqual(Backbone.history.root, '/'); | |||
}); | }); | |||
QUnit.test('#1619: Router: nagivate with empty root', function(assert) { | QUnit.test('#1619: Router: nagivate with empty root', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/'); | location.replace('http://example.com/'); | |||
skipping to change at line 628 | skipping to change at line 639 | |||
}); | }); | |||
}); | }); | |||
QUnit.test('#1695 - hashChange to pushState with search.', function(assert) { | QUnit.test('#1695 - hashChange to pushState with search.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/root#x/y?a=b'); | location.replace('http://example.com/root#x/y?a=b'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(state, title, url){ | replaceState: function(state, title, url) { | |||
assert.strictEqual(url, '/root/x/y?a=b'); | assert.strictEqual(url, '/root/x/y?a=b'); | |||
} | } | |||
} | } | |||
}); | }); | |||
Backbone.history.start({ | Backbone.history.start({ | |||
root: 'root', | root: 'root', | |||
pushState: true | pushState: true | |||
}); | }); | |||
}); | }); | |||
QUnit.test('#1746 - Router allows empty route.', function(assert) { | QUnit.test('#1746 - Router allows empty route.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: {'': 'empty'}, | routes: {'': 'empty'}, | |||
empty: function(){}, | empty: function() {}, | |||
route: function(route){ | route: function(route) { | |||
assert.strictEqual(route, ''); | assert.strictEqual(route, ''); | |||
} | } | |||
}); | }); | |||
new Router; | new MyRouter; | |||
}); | }); | |||
QUnit.test('#1794 - Trailing space in fragments.', function(assert) { | QUnit.test('#1794 - Trailing space in fragments.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var history = new Backbone.History; | var history = new Backbone.History; | |||
assert.strictEqual(history.getFragment('fragment '), 'fragment'); | assert.strictEqual(history.getFragment('fragment '), 'fragment'); | |||
}); | }); | |||
QUnit.test('#1820 - Leading slash and trailing space.', 1, function(assert) { | QUnit.test('#1820 - Leading slash and trailing space.', function(assert) { | |||
assert.expect(1); | ||||
var history = new Backbone.History; | var history = new Backbone.History; | |||
assert.strictEqual(history.getFragment('/fragment '), 'fragment'); | assert.strictEqual(history.getFragment('/fragment '), 'fragment'); | |||
}); | }); | |||
QUnit.test('#1980 - Optional parameters.', function(assert) { | QUnit.test('#1980 - Optional parameters.', function(assert) { | |||
assert.expect(2); | assert.expect(2); | |||
location.replace('http://example.com#named/optional/y'); | location.replace('http://example.com#named/optional/y'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
assert.strictEqual(router.z, undefined); | assert.strictEqual(router.z, undefined); | |||
location.replace('http://example.com#named/optional/y123'); | location.replace('http://example.com#named/optional/y123'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
assert.strictEqual(router.z, '123'); | assert.strictEqual(router.z, '123'); | |||
}); | }); | |||
QUnit.test("#2062 - Trigger 'route' event on router instance.", function(asser t) { | QUnit.test('#2062 - Trigger "route" event on router instance.', function(asser t) { | |||
assert.expect(2); | assert.expect(2); | |||
router.on('route', function(name, args) { | router.on('route', function(name, args) { | |||
assert.strictEqual(name, 'routeEvent'); | assert.strictEqual(name, 'routeEvent'); | |||
assert.deepEqual(args, ['x', null]); | assert.deepEqual(args, ['x', null]); | |||
}); | }); | |||
location.replace('http://example.com#route-event/x'); | location.replace('http://example.com#route-event/x'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
}); | }); | |||
QUnit.test('#2255 - Extend routes by making routes a function.', function(asse rt) { | QUnit.test('#2255 - Extend routes by making routes a function.', function(asse rt) { | |||
skipping to change at line 701 | skipping to change at line 713 | |||
} | } | |||
}); | }); | |||
var RouterExtended = RouterBase.extend({ | var RouterExtended = RouterBase.extend({ | |||
routes: function() { | routes: function() { | |||
var _super = RouterExtended.__super__.routes; | var _super = RouterExtended.__super__.routes; | |||
return _.extend(_super(), {show: 'show', search: 'search'}); | return _.extend(_super(), {show: 'show', search: 'search'}); | |||
} | } | |||
}); | }); | |||
var router = new RouterExtended(); | var myRouter = new RouterExtended(); | |||
assert.deepEqual({home: 'root', index: 'index.html', show: 'show', search: ' | assert.deepEqual({home: 'root', index: 'index.html', show: 'show', search: ' | |||
search'}, router.routes); | search'}, myRouter.routes); | |||
}); | }); | |||
QUnit.test('#2538 - hashChange to pushState only if both requested.', function (assert) { | QUnit.test('#2538 - hashChange to pushState only if both requested.', function (assert) { | |||
assert.expect(0); | assert.expect(0); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/root?a=b#x/y'); | location.replace('http://example.com/root?a=b#x/y'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(){ assert.ok(false); } | replaceState: function() { assert.ok(false); } | |||
} | } | |||
}); | }); | |||
Backbone.history.start({ | Backbone.history.start({ | |||
root: 'root', | root: 'root', | |||
pushState: true, | pushState: true, | |||
hashChange: false | hashChange: false | |||
}); | }); | |||
}); | }); | |||
QUnit.test('No hash fallback.', function(assert) { | QUnit.test('No hash fallback.', function(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){}, | pushState: function() {}, | |||
replaceState: function(){} | replaceState: function() {} | |||
} | } | |||
}); | }); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
hash: function() { assert.ok(false); } | hash: function() { assert.ok(false); } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
location.replace('http://example.com/'); | location.replace('http://example.com/'); | |||
Backbone.history.start({ | Backbone.history.start({ | |||
pushState: true, | pushState: true, | |||
hashChange: false | hashChange: false | |||
}); | }); | |||
location.replace('http://example.com/nomatch#hash'); | location.replace('http://example.com/nomatch#hash'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
}); | }); | |||
QUnit.test('#2656 - No trailing slash on root.', function(assert) { | QUnit.test('#2656 - No trailing slash on root.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(state, title, url){ | pushState: function(state, title, url) { | |||
assert.strictEqual(url, '/root'); | assert.strictEqual(url, '/root'); | |||
} | } | |||
} | } | |||
}); | }); | |||
location.replace('http://example.com/root/path'); | location.replace('http://example.com/root/path'); | |||
Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); | Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); | |||
Backbone.history.navigate(''); | Backbone.history.navigate(''); | |||
}); | }); | |||
QUnit.test('#2656 - No trailing slash on root.', function(assert) { | QUnit.test('#2656 - No trailing slash on root.', function(assert) { | |||
skipping to change at line 788 | skipping to change at line 800 | |||
Backbone.history.start({pushState: true, hashChange: false}); | Backbone.history.start({pushState: true, hashChange: false}); | |||
Backbone.history.navigate(''); | Backbone.history.navigate(''); | |||
}); | }); | |||
QUnit.test('#2656 - No trailing slash on root.', function(assert) { | QUnit.test('#2656 - No trailing slash on root.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(state, title, url){ | pushState: function(state, title, url) { | |||
assert.strictEqual(url, '/root?x=1'); | assert.strictEqual(url, '/root?x=1'); | |||
} | } | |||
} | } | |||
}); | }); | |||
location.replace('http://example.com/root/path'); | location.replace('http://example.com/root/path'); | |||
Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); | Backbone.history.start({pushState: true, hashChange: false, root: 'root'}); | |||
Backbone.history.navigate('?x=1'); | Backbone.history.navigate('?x=1'); | |||
}); | }); | |||
QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) { | QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) { | |||
skipping to change at line 810 | skipping to change at line 822 | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(state, title, url) { | pushState: function(state, title, url) { | |||
assert.strictEqual(url, '/path?query#hash'); | assert.strictEqual(url, '/path?query#hash'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
path: function() { assert.ok(true); } | path: function() { assert.ok(true); } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
location.replace('http://example.com/'); | location.replace('http://example.com/'); | |||
Backbone.history.start({pushState: true, hashChange: false}); | Backbone.history.start({pushState: true, hashChange: false}); | |||
Backbone.history.navigate('path?query#hash', true); | Backbone.history.navigate('path?query#hash', true); | |||
}); | }); | |||
QUnit.test('Do not decode the search params.', function(assert) { | QUnit.test('Do not decode the search params.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
path: function(params){ | path: function(params) { | |||
assert.strictEqual(params, 'x=y%3Fz'); | assert.strictEqual(params, 'x=y%3Fz'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.navigate('path?x=y%3Fz', true); | Backbone.history.navigate('path?x=y%3Fz', true); | |||
}); | }); | |||
QUnit.test('Navigate to a hash url.', function(assert) { | QUnit.test('Navigate to a hash url.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
path: function(params) { | path: function(params) { | |||
assert.strictEqual(params, 'x=y'); | assert.strictEqual(params, 'x=y'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
location.replace('http://example.com/path?x=y#hash'); | location.replace('http://example.com/path?x=y#hash'); | |||
Backbone.history.checkUrl(); | Backbone.history.checkUrl(); | |||
}); | }); | |||
QUnit.test('#navigate to a hash url.', function(assert) { | QUnit.test('#navigate to a hash url.', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
path: function(params) { | path: function(params) { | |||
assert.strictEqual(params, 'x=y'); | assert.strictEqual(params, 'x=y'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.navigate('path?x=y#hash', true); | Backbone.history.navigate('path?x=y#hash', true); | |||
}); | }); | |||
QUnit.test('unicode pathname', function(assert) { | QUnit.test('unicode pathname', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/myyjä'); | location.replace('http://example.com/myyjä'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
myyjä: function() { | myyjä: function() { | |||
assert.ok(true); | assert.ok(true); | |||
} | } | |||
} | } | |||
}); | }); | |||
new Router; | new MyRouter; | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
}); | }); | |||
QUnit.test('unicode pathname with % in a parameter', function(assert) { | QUnit.test('unicode pathname with % in a parameter', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/myyjä/foo%20%25%3F%2f%40%25%20bar'); | location.replace('http://example.com/myyjä/foo%20%25%3F%2f%40%25%20bar'); | |||
location.pathname = '/myyj%C3%A4/foo%20%25%3F%2f%40%25%20bar'; | location.pathname = '/myyj%C3%A4/foo%20%25%3F%2f%40%25%20bar'; | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
'myyjä/:query': function(query) { | 'myyjä/:query': function(query) { | |||
assert.strictEqual(query, 'foo %?/@% bar'); | assert.strictEqual(query, 'foo %?/@% bar'); | |||
} | } | |||
} | } | |||
}); | }); | |||
new Router; | new MyRouter; | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
}); | }); | |||
QUnit.test('newline in route', function(assert) { | QUnit.test('newline in route', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/stuff%0Anonsense?param=foo%0Abar'); | location.replace('http://example.com/stuff%0Anonsense?param=foo%0Abar'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
'stuff\nnonsense': function() { | 'stuff\nnonsense': function() { | |||
assert.ok(true); | assert.ok(true); | |||
} | } | |||
} | } | |||
}); | }); | |||
new Router; | new MyRouter; | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
}); | }); | |||
QUnit.test('Router#execute receives callback, args, name.', function(assert) { | QUnit.test('Router#execute receives callback, args, name.', function(assert) { | |||
assert.expect(3); | assert.expect(3); | |||
location.replace('http://example.com#foo/123/bar?x=y'); | location.replace('http://example.com#foo/123/bar?x=y'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: {'foo/:id/bar': 'foo'}, | routes: {'foo/:id/bar': 'foo'}, | |||
foo: function(){}, | foo: function() {}, | |||
execute: function(callback, args, name) { | execute: function(callback, args, name) { | |||
assert.strictEqual(callback, this.foo); | assert.strictEqual(callback, this.foo); | |||
assert.deepEqual(args, ['123', 'x=y']); | assert.deepEqual(args, ['123', 'x=y']); | |||
assert.strictEqual(name, 'foo'); | assert.strictEqual(name, 'foo'); | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start(); | Backbone.history.start(); | |||
}); | }); | |||
QUnit.test('pushState to hashChange with only search params.', function(assert ) { | QUnit.test('pushState to hashChange with only search params.', function(assert ) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com?a=b'); | location.replace('http://example.com?a=b'); | |||
location.replace = function(url) { | location.replace = function(url) { | |||
assert.strictEqual(url, '/#?a=b'); | assert.strictEqual(url, '/#?a=b'); | |||
}; | }; | |||
skipping to change at line 956 | skipping to change at line 968 | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
}); | }); | |||
QUnit.test('#3123 - History#navigate decodes before comparison.', function(ass ert) { | QUnit.test('#3123 - History#navigate decodes before comparison.', function(ass ert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/shop/search?keyword=short%20dress'); | location.replace('http://example.com/shop/search?keyword=short%20dress'); | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: function(){ assert.ok(false); }, | pushState: function() { assert.ok(false); }, | |||
replaceState: function(){ assert.ok(false); } | replaceState: function() { assert.ok(false); } | |||
} | } | |||
}); | }); | |||
Backbone.history.start({pushState: true}); | Backbone.history.start({pushState: true}); | |||
Backbone.history.navigate('shop/search?keyword=short%20dress', true); | Backbone.history.navigate('shop/search?keyword=short%20dress', true); | |||
assert.strictEqual(Backbone.history.fragment, 'shop/search?keyword=short dre ss'); | assert.strictEqual(Backbone.history.fragment, 'shop/search?keyword=short dre ss'); | |||
}); | }); | |||
QUnit.test('#3175 - Urls in the params', function(assert) { | QUnit.test('#3175 - Urls in the params', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com#login?a=value&backUrl=https%3A%2F%2Fwww .msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); | location.replace('http://example.com#login?a=value&backUrl=https%3A%2F%2Fwww .msn.com%2Fidp%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var router = new Backbone.Router; | var myRouter = new Backbone.Router; | |||
router.route('login', function(params) { | myRouter.route('login', function(params) { | |||
assert.strictEqual(params, 'a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fid p%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); | assert.strictEqual(params, 'a=value&backUrl=https%3A%2F%2Fwww.msn.com%2Fid p%2Fidpdemo%3Fspid%3Dspdemo%26target%3Db'); | |||
}); | }); | |||
Backbone.history.start(); | Backbone.history.start(); | |||
}); | }); | |||
QUnit.test('#3358 - pushState to hashChange transition with search params', fu nction(assert) { | QUnit.test('#3358 - pushState to hashChange transition with search params', fu nction(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
location.replace('http://example.com/root?foo=bar'); | location.replace('http://example.com/root?foo=bar'); | |||
location.replace = function(url) { | location.replace = function(url) { | |||
skipping to change at line 994 | skipping to change at line 1006 | |||
Backbone.history = _.extend(new Backbone.History, { | Backbone.history = _.extend(new Backbone.History, { | |||
location: location, | location: location, | |||
history: { | history: { | |||
pushState: undefined, | pushState: undefined, | |||
replaceState: undefined | replaceState: undefined | |||
} | } | |||
}); | }); | |||
Backbone.history.start({root: '/root', pushState: true}); | Backbone.history.start({root: '/root', pushState: true}); | |||
}); | }); | |||
QUnit.test("Paths that don't match the root should not match no root", functio n(assert) { | QUnit.test('Paths that don\'t match the root should not match no root', functi on(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
location.replace('http://example.com/foo'); | location.replace('http://example.com/foo'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
foo: function(){ | foo: function() { | |||
assert.ok(false, 'should not match unless root matches'); | assert.ok(false, 'should not match unless root matches'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start({root: 'root', pushState: true}); | Backbone.history.start({root: 'root', pushState: true}); | |||
}); | }); | |||
QUnit.test("Paths that don't match the root should not match roots of the same length", function(assert) { | QUnit.test('Paths that don\'t match the root should not match roots of the sam e length', function(assert) { | |||
assert.expect(0); | assert.expect(0); | |||
location.replace('http://example.com/xxxx/foo'); | location.replace('http://example.com/xxxx/foo'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: { | routes: { | |||
foo: function(){ | foo: function() { | |||
assert.ok(false, 'should not match unless root matches'); | assert.ok(false, 'should not match unless root matches'); | |||
} | } | |||
} | } | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start({root: 'root', pushState: true}); | Backbone.history.start({root: 'root', pushState: true}); | |||
}); | }); | |||
QUnit.test('roots with regex characters', function(assert) { | QUnit.test('roots with regex characters', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/x+y.z/foo'); | location.replace('http://example.com/x+y.z/foo'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: {foo: function(){ assert.ok(true); }} | routes: {foo: function() { assert.ok(true); }} | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start({root: 'x+y.z', pushState: true}); | Backbone.history.start({root: 'x+y.z', pushState: true}); | |||
}); | }); | |||
QUnit.test('roots with unicode characters', function(assert) { | QUnit.test('roots with unicode characters', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/®ooτ/foo'); | location.replace('http://example.com/®ooτ/foo'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: {foo: function(){ assert.ok(true); }} | routes: {foo: function() { assert.ok(true); }} | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start({root: '®ooτ', pushState: true}); | Backbone.history.start({root: '®ooτ', pushState: true}); | |||
}); | }); | |||
QUnit.test('roots without slash', function(assert) { | QUnit.test('roots without slash', function(assert) { | |||
assert.expect(1); | assert.expect(1); | |||
location.replace('http://example.com/®ooτ'); | location.replace('http://example.com/®ooτ'); | |||
Backbone.history.stop(); | Backbone.history.stop(); | |||
Backbone.history = _.extend(new Backbone.History, {location: location}); | Backbone.history = _.extend(new Backbone.History, {location: location}); | |||
var Router = Backbone.Router.extend({ | var MyRouter = Backbone.Router.extend({ | |||
routes: {'': function(){ assert.ok(true); }} | routes: {'': function() { assert.ok(true); }} | |||
}); | }); | |||
var router = new Router; | var myRouter = new MyRouter; | |||
Backbone.history.start({root: '®ooτ', pushState: true}); | Backbone.history.start({root: '®ooτ', pushState: true}); | |||
}); | }); | |||
})(); | QUnit.test('#4025 - navigate updates URL hash as is', function(assert) { | |||
assert.expect(1); | ||||
var route = 'search/has%20space'; | ||||
Backbone.history.navigate(route); | ||||
assert.strictEqual(location.hash, '#' + route); | ||||
}); | ||||
})(QUnit); | ||||
End of changes. 70 change blocks. | ||||
83 lines changed or deleted | 95 lines changed or added |