este-library-oldschool
Version:
Library for github.com/steida/este.git
359 lines • 11.3 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
suite('este.Router', function() {
var Router, dispatchGestureHandlerTapEvent, dispatchHistoryNavigateEvent, gestureHandler, history, router;
Router = este.Router;
history = null;
gestureHandler = null;
router = null;
setup(function() {
history = {
setToken: function(token) {
return dispatchHistoryNavigateEvent(token, false);
},
dispose: function() {},
setEnabled: function() {},
addEventListener: function() {}
};
gestureHandler = {
dispose: function() {},
addEventListener: function() {},
getElement: function() {
return document.createElement('div');
}
};
return router = new Router(history, gestureHandler);
});
dispatchHistoryNavigateEvent = function(token, isNavigation) {
if (isNavigation == null) {
isNavigation = true;
}
return goog.events.fireListeners(history, 'navigate', false, {
type: 'navigate',
token: token,
isNavigation: isNavigation
});
};
dispatchGestureHandlerTapEvent = function(target) {
return goog.events.fireListeners(gestureHandler, 'tap', false, {
type: 'tap',
target: target,
preventDefault: function() {}
});
};
suite('constructor', function() {
return test('should work', function() {
return assert.instanceOf(router, este.Router);
});
});
suite('start', function() {
return test('should call history.setEnabled with true', function(done) {
history.setEnabled = function(enabled) {
assert.isTrue(enabled);
return done();
};
return router.start();
});
});
suite('dispose', function() {
test('should call history.dispose', function(done) {
history.dispose = function() {
return done();
};
return router.dispose();
});
return test('should call gestureHandler.dispose', function(done) {
gestureHandler.dispose = function() {
return done();
};
return router.dispose();
});
});
suite('routing via history navigate event', function() {
suite('show should work', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "', token: '" + token + "'", function(done) {
router.add(path, function(params, isNavigation) {
assert.isTrue(isNavigation);
return done();
});
router.start();
return dispatchHistoryNavigateEvent(token);
});
};
testRoute('/foo', 'foo');
testRoute('/bla', 'bla');
testRoute('/user/:user', 'user/joe');
return testRoute('/user/:user', 'user/satriani');
});
suite('show should not be called with sensitive true', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "' should match token: '" + token + "'", function(done) {
router.add(path, (function() {}), {
hide: function() {
return done();
},
sensitive: true
});
router.start();
return dispatchHistoryNavigateEvent(token);
});
};
testRoute('/Foo', 'foo');
testRoute('/Bla', 'bla');
testRoute('/User/:user', 'user/joe');
return testRoute('/User/:user', 'user/satriani');
});
suite('hide should work', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "' should match token: '" + token + "'", function(done) {
router.add(path, (function() {}), {
hide: function() {
return done();
}
});
router.start();
return dispatchHistoryNavigateEvent(token);
});
};
testRoute('/foo', 'bla');
testRoute('/bla', 'foo');
testRoute('/user/:user', 'product/joe');
return testRoute('/user/:user', 'product/satriani');
});
return suite('exception in callback', function() {
return test('should not break processing', function() {
var count;
count = 0;
router.add('/foo', function() {
count++;
throw Error('Error');
});
router.add('/foo', function() {
return count++;
});
router.start();
dispatchHistoryNavigateEvent('foo');
return assert.equal(count, 2);
});
});
});
suite('routing via gestureHandler tap event', function() {
suite('show should work', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "', token: '" + token + "'", function(done) {
var fn, setTokenCalled;
setTokenCalled = false;
fn = history.setToken;
history.setToken = function() {
setTokenCalled = true;
return fn.apply(this, arguments);
};
router.add(path, function(params, isNavigation) {
assert.isFalse(isNavigation);
assert.isTrue(setTokenCalled);
return done();
});
router.start();
return dispatchGestureHandlerTapEvent({
nodeType: 1,
hasAttribute: function() {},
getAttribute: function(name) {
if (name === 'href') {
return token;
}
}
});
});
};
testRoute('/foo', 'foo');
testRoute('/bla', 'bla');
testRoute('/user/:user', 'user/joe');
return testRoute('/user/:user', 'user/satriani');
});
suite('show should work, but should not call history.setToken', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "', token: '" + token + "'", function(done) {
var fn, setTokenCalled;
router.navigateImmediately = false;
setTokenCalled = false;
fn = history.setToken;
history.setToken = function() {
setTokenCalled = true;
return fn.apply(this, arguments);
};
router.add(path, function(params, isNavigation) {
assert.isFalse(isNavigation);
assert.isFalse(setTokenCalled);
return done();
});
router.start();
return dispatchGestureHandlerTapEvent({
nodeType: 1,
hasAttribute: function() {},
getAttribute: function(name) {
if (name === 'href') {
return token;
}
}
});
});
};
testRoute('/foo', 'foo');
testRoute('/bla', 'bla');
testRoute('/user/:user', 'user/joe');
return testRoute('/user/:user', 'user/satriani');
});
suite('show should work in parent', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "', token: '" + token + "'", function(done) {
router.add(path, function() {
return done();
});
router.start();
return dispatchGestureHandlerTapEvent({
nodeType: 1,
hasAttribute: function() {},
getAttribute: function() {},
parentNode: {
nodeType: 1,
hasAttribute: function() {},
getAttribute: function(name) {
if (name === 'href') {
return token;
}
}
}
});
});
};
testRoute('/foo', 'foo');
testRoute('/bla', 'bla');
testRoute('/user/:user', 'user/joe');
return testRoute('/user/:user', 'user/satriani');
});
return suite('hide should work', function() {
var testRoute;
testRoute = function(path, token) {
return test("path: '" + path + "' should match token: '" + token + "'", function(done) {
router.add(path, (function() {}), {
hide: function() {
return done();
}
});
router.start();
return dispatchGestureHandlerTapEvent({
nodeType: 1,
hasAttribute: function() {},
getAttribute: function(name) {
if (name === 'href') {
return token;
}
}
});
});
};
testRoute('/foo', 'bla');
testRoute('/bla', 'foo');
testRoute('/user/:user', 'product/joe');
return testRoute('/user/:user', 'product/satriani');
});
});
suite('remove route', function() {
return test('should work for string route', function() {
var called;
called = false;
router.add('/user/:user', function(params) {
assert.equal(params['user'], 'joe');
return called = true;
});
router.start();
router.remove('user/:user');
dispatchHistoryNavigateEvent('users/joe');
return assert.isFalse(called);
});
});
suite('navigate', function() {
return test('should call setToken on history object', function(done) {
history.setToken = function(token) {
assert.equal(token, 'foo');
return done();
};
return router.navigate('foo');
});
});
suite('pathNavigate user/:name, name: joe', function() {
test('should call setToken on history object', function(done) {
history.setToken = function(token) {
assert.equal(token, 'user/joe');
return done();
};
router.start();
router.add('/user/:name', function() {});
return router.pathNavigate('user/:name', {
name: 'joe'
});
});
return test('should call router show', function() {
var called;
called = false;
router.start();
router.add('/user/:name', function() {
return called = true;
});
router.pathNavigate('user/:name', {
name: 'joe'
});
return assert.isTrue(called);
});
});
suite('pathNavigate user/:name, name: joe, true', function() {
test('should call setToken on history object', function(done) {
history.setToken = function(token) {
assert.equal(token, 'user/joe');
return done();
};
router.start();
router.add('/user/:name', function() {});
return router.pathNavigate('user/:name', {
name: 'joe'
}, true);
});
return test('should not call router show', function() {
var called;
called = false;
router.start();
router.add('/user/:name', function() {
return called = true;
});
router.pathNavigate('user/:name', {
name: 'joe'
}, true);
return assert.isFalse(called);
});
});
return suite('only first route from two or more matched', function() {
return test('should be processed', function() {
var calls;
calls = '';
router.add('/foo/bar', (function() {
return calls += 1;
}));
router.add('/foo/*', (function() {
return calls += 2;
}), {
hide: function() {
return calls += 3;
}
});
router.start();
dispatchHistoryNavigateEvent('foo/bar');
return assert.equal(calls, 13);
});
});
});