este-library-oldschool
Version:
Library for github.com/steida/este.git
603 lines • 18.8 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
suite('este.App', function() {
var App, Presenter0, Presenter1, Presenter2, app, arrangePresenters, arrangeRoutes, mockPresenter, mockRouter, mockScreen, presenter0, presenter1, presenter2, router, screen, window;
App = este.App;
router = null;
screen = null;
app = null;
window = null;
Presenter0 = null;
Presenter1 = null;
Presenter2 = null;
presenter0 = null;
presenter1 = null;
presenter2 = null;
setup(function() {
router = mockRouter();
screen = mockScreen();
app = new App(router, screen);
window = {
alert: function() {}
};
app.window = window;
arrangePresenters();
return arrangeRoutes();
});
mockRouter = function() {
return {
routes: [],
add: function(path, callback) {
return this.routes.push({
path: path,
callback: callback
});
},
start: function() {
return this.routes[0].callback({
someParams: true
});
},
pathNavigate: function() {},
isHtml5historyEnabled: function() {
return true;
},
dispose: function() {}
};
};
mockScreen = function() {
return {
dispose: function() {}
};
};
arrangePresenters = function() {
Presenter0 = function() {};
Presenter1 = function() {};
Presenter2 = function() {};
presenter0 = mockPresenter(Presenter0, true);
presenter1 = mockPresenter(Presenter1);
return presenter2 = mockPresenter(Presenter2);
};
mockPresenter = function(presenterClass, sync) {
presenterClass.prototype.load = function(params) {};
presenterClass.prototype.beforeShow = function() {};
presenterClass.prototype.beforeHide = function() {};
presenterClass.prototype.dispose = function() {};
return new presenterClass;
};
arrangeRoutes = function() {
return app.addRoutes({
'/': presenter0,
'/foo': presenter1,
'/bla/:id': presenter2
});
};
suite('constructor', function() {
return test('should work', function() {
return assert.instanceOf(app, App);
});
});
suite('start', function() {
test('should throw error if no route is defined', function(done) {
var e, error;
app = new App(router);
try {
return app.start();
} catch (error) {
e = error;
assert.equal(e.message, 'Assertion failed: At least one route has to be defined.');
return done();
}
});
suite('with urlEnabled == false', function() {
return test('should call presenter0.load', function(done) {
app.urlEnabled = false;
presenter0.load = function() {
done();
return goog.result.successfulResult(null);
};
presenter1.load = function() {
throw Error('error');
};
presenter2.load = function() {
throw Error('error');
};
return app.start();
});
});
return suite('with default urlEnabled', function() {
test('should start router with navigateImmediately disabled', function(done) {
router.start = function() {
assert.isFalse(router.navigateImmediately);
return done();
};
return app.start();
});
return test('should call presenter0.load', function(done) {
presenter0.load = function() {
done();
return goog.result.successfulResult(null);
};
presenter1.load = function() {
throw Error('error');
};
presenter2.load = function() {
throw Error('error');
};
return app.start();
});
});
});
suite('router', function() {
suite('start', function() {
test('should dispatch load event then call load', function(done) {
var loadDispatched;
loadDispatched = false;
goog.events.listen(app, 'load', function(e) {
var jsonA, jsonB;
jsonA = JSON.stringify(e.request);
jsonB = JSON.stringify({
presenter: presenter0,
params: {
someParams: true
},
isNavigation: false
});
assert.equal(jsonA, jsonB);
return loadDispatched = true;
});
presenter0.load = function() {
assert.isTrue(loadDispatched);
done();
return goog.result.successfulResult(null);
};
return app.start();
});
return test('should dispatch show event then call beforeShow', function(done) {
var showDispatched;
showDispatched = false;
goog.events.listen(app, 'show', function(e) {
var jsonA, jsonB;
jsonA = JSON.stringify(e.request);
jsonB = JSON.stringify({
presenter: presenter0,
params: {
someParams: true
},
isNavigation: false
});
assert.equal(jsonA, jsonB);
return showDispatched = true;
});
presenter0.beforeShow = function() {
assert.isTrue(showDispatched);
done();
return goog.result.successfulResult(null);
};
return app.start();
});
});
return suite('routes', function() {
setup(function() {
return app.start();
});
test('routes[0] should beforeShow presenter0', function(done) {
presenter0.beforeShow = function() {
return done();
};
return router.routes[0].callback();
});
test('routes[1] should beforeShow presenter1', function(done) {
presenter1.beforeShow = function() {
return done();
};
return router.routes[1].callback();
});
return test('routes[2] should beforeShow presenter2', function(done) {
presenter2.beforeShow = function() {
return done();
};
return router.routes[2].callback();
});
});
});
suite('show/hide logic', function() {
return test('should dispatch hide event then call presenter1 beforeHide', function(done) {
var hideDispatched;
app.start();
hideDispatched = false;
goog.events.listen(app, 'hide', function(e) {
var jsonA, jsonB;
jsonA = JSON.stringify(e.request);
jsonB = JSON.stringify({
presenter: presenter0,
params: {
someParams: true
},
isNavigation: false
});
assert.equal(jsonA, jsonB);
return hideDispatched = true;
});
presenter0.beforeHide = function() {
assert.isTrue(hideDispatched);
return done();
};
return router.routes[1].callback();
});
});
suite('prepare presenter', function() {
suite('storage', function() {
test('should be set on presenter, if not defined', function() {
app.storage = {};
arrangeRoutes();
assert.equal(presenter0.storage, app.storage);
assert.equal(presenter1.storage, app.storage);
return assert.equal(presenter2.storage, app.storage);
});
return test('should not be set on presenter, if defined', function() {
var storage0, storage1, storage2;
app.storage = {};
storage0 = presenter0.storage = {};
storage1 = presenter1.storage = {};
storage2 = presenter2.storage = {};
arrangeRoutes();
assert.equal(presenter0.storage, storage0);
assert.equal(presenter1.storage, storage1);
return assert.equal(presenter2.storage, storage2);
});
});
suite('screen', function() {
test('should be set on presenter, if not defined', function() {
assert.equal(presenter0.screen, app.screen);
assert.equal(presenter1.screen, app.screen);
return assert.equal(presenter2.screen, app.screen);
});
return test('should not be set on presenter, if defined', function() {
var screen0, screen1, screen2;
screen0 = presenter0.screen = {};
screen1 = presenter1.screen = {};
screen2 = presenter2.screen = {};
arrangeRoutes();
assert.equal(presenter0.screen, screen0);
assert.equal(presenter1.screen, screen1);
return assert.equal(presenter2.screen, screen2);
});
});
suite('createUrl', function() {
test('should be set on presenter, if not defined', function() {
var calls;
calls = '';
app.createUrl = function(presenterClass, params) {
calls += presenterClass;
return calls += params;
};
presenter0.createUrl = null;
presenter1.createUrl = null;
presenter2.createUrl = null;
arrangeRoutes();
presenter0.createUrl(0, 0);
presenter1.createUrl(1, 1);
presenter2.createUrl(2, 2);
return assert.equal(calls, '001122');
});
return test('should not be set on presenter, if defined', function() {
var createUrl0, createUrl1, createUrl2;
app.createUrl = function() {};
createUrl0 = presenter0.createUrl = function() {};
createUrl1 = presenter1.createUrl = function() {};
createUrl2 = presenter2.createUrl = function() {};
arrangeRoutes();
assert.equal(presenter0.createUrl, createUrl0);
assert.equal(presenter1.createUrl, createUrl1);
return assert.equal(presenter2.createUrl, createUrl2);
});
});
return suite('redirect', function() {
test('should be set on presenter, if not defined', function() {
var calls;
calls = '';
app.redirect = function(presenterClass, params) {
calls += presenterClass;
return calls += params;
};
presenter0.redirect = null;
presenter1.redirect = null;
presenter2.redirect = null;
arrangeRoutes();
presenter0.redirect(0, 0);
presenter1.redirect(1, 1);
presenter2.redirect(2, 2);
return assert.equal(calls, '001122');
});
return test('should not be set on presenter, if defined', function() {
var redirect0, redirect1, redirect2;
app.redirect = function() {};
redirect0 = presenter0.redirect = function() {};
redirect1 = presenter1.redirect = function() {};
redirect2 = presenter2.redirect = function() {};
arrangeRoutes();
assert.equal(presenter0.redirect, redirect0);
assert.equal(presenter1.redirect, redirect1);
return assert.equal(presenter2.redirect, redirect2);
});
});
});
suite('router.pathNavigate', function() {
test('should not be called if dispatched by app start', function() {
var called;
called = false;
router.pathNavigate = function() {
return called = true;
};
app.start();
return assert.isFalse(called);
});
test('should be called if route matched and isNavigation == false', function() {
var called;
called = false;
router.pathNavigate = function() {
return called = true;
};
app.start();
assert.isFalse(called);
router.routes[0].callback({}, false);
return assert.isTrue(called);
});
return test('should not be called if route matched and isNavigation == true', function() {
var called;
called = false;
router.pathNavigate = function() {
return called = true;
};
app.start();
assert.isFalse(called);
router.routes[0].callback({}, true);
return assert.isFalse(called);
});
});
suite('createUrl', function() {
suite('pushState enabled', function() {
return test('should create URL from presenter and params', function() {
var url;
app.start();
url = app.createUrl(Presenter2, {
'id': 123
});
return assert.equal(url, '/bla/123');
});
});
return suite('pushState disabled', function() {
return test('should create URL from presenter and params', function() {
var url;
router.isHtml5historyEnabled = function() {
return false;
};
app.start();
url = app.createUrl(Presenter2, {
'id': 123
});
return assert.equal(url, '#/bla/123');
});
});
});
suite('redirect', function() {
return test('should call presenter with passed params', function(done) {
var params;
app.start();
params = {
'id': 123
};
presenter2.load = function(p_params) {
assert.equal(p_params, params);
done();
return goog.result.successfulResult(null);
};
return app.redirect(Presenter2, params);
});
});
suite('dispose', function() {
return test('should work', function() {
var calls;
calls = 0;
app.start();
app.router.dispose = function() {
return calls++;
};
app.screen.dispose = function() {
return calls++;
};
presenter0.dispose = function() {
return calls++;
};
presenter1.dispose = function() {
return calls++;
};
presenter2.dispose = function() {
return calls++;
};
app.dispose();
return assert.equal(calls, 5);
});
});
suite('last click win', function() {
setup(function() {
return app.start();
});
suite('presenter1 loaded twice', function() {
return test('should be fired once', function(done) {
presenter1.beforeShow = function() {
return done();
};
router.routes[1].callback();
return setTimeout(function() {
return router.routes[1].callback();
}, 4);
});
});
return suite('presenter1, then presenter2', function() {
return test('should call only presenter2.beforeShow', function(done) {
presenter1.beforeShow = function() {
throw Error('error');
};
presenter2.beforeShow = function() {
return done();
};
router.routes[1].callback();
return setTimeout(function() {
return router.routes[2].callback();
}, 4);
});
});
});
suite('dont repeat same requests', function() {
suite('presenter1, then presenter2, then presenter1', function() {
return test('should call only presenter1.beforeShow', function(done) {
presenter1.beforeShow = function() {
throw Error('error');
};
presenter2.beforeShow = function() {
return done();
};
router.routes[1].callback();
setTimeout(function() {
return router.routes[2].callback();
}, 2);
return setTimeout(function() {
return router.routes[1].callback();
}, 4);
});
});
suite('presenter1 trying to be loaded twice', function() {
return test('should dispatch only one load event', function(done) {
var loadCount;
app.start();
loadCount = 0;
goog.events.listen(app, 'load', function(e) {
return loadCount++;
});
goog.events.listen(app, 'show', function(e) {
assert.equal(loadCount, 1);
return done();
});
app.load(presenter1);
return app.load(presenter1);
});
});
suite('presenter1 trying to be loaded twice with different params', function() {
return test('should dispatch two load events', function(done) {
var loadCount;
app.start();
loadCount = 0;
goog.events.listen(app, 'load', function(e) {
return loadCount++;
});
goog.events.listen(app, 'show', function(e) {
assert.equal(loadCount, 2);
return done();
});
app.load(presenter1);
return app.load(presenter1, {
a: 1
});
});
});
suite('browser BF', function() {
return test('should reset queue cache', function(done) {
var loadCount;
app.start();
loadCount = 0;
goog.events.listen(app, 'load', function(e) {
return loadCount++;
});
goog.events.listen(app, 'show', function(e) {
assert.equal(loadCount, 2);
return done();
});
app.load(presenter1, {});
return app.load(presenter2, {}, true);
});
});
return suite('onLoad', function() {
return test('should clear queue', function(done) {
var showCount;
app.start();
showCount = 0;
goog.events.listen(app, 'show', function(e) {
showCount++;
if (showCount === 2) {
done();
return;
}
return app.load(presenter1);
});
return app.load(presenter1);
});
});
});
suite('timeout event', function() {
test('should be dispatched if loading time > timeoutMs', function(done) {
app.timeoutMs = 4;
app.start();
goog.events.listen(app, 'timeout', function(e) {
return done();
});
return app.load(presenter1);
});
test('should display alert', function(done) {
app.timeoutMs = 4;
app.start();
app.window.alert = function(msg) {
assert.equal(msg, App.MSG_REQUEST_TIMEOUT);
return done();
};
return app.load(presenter1);
});
return test('should not display alert', function(done) {
var alertCalled;
app.showAlertOnError = false;
app.timeoutMs = 4;
app.start();
alertCalled = false;
app.window.alert = function(msg) {
return alertCalled = true;
};
goog.events.listen(app, 'timeout', function(e) {
assert.isFalse(alertCalled);
return done();
});
return app.load(presenter1);
});
});
return suite('error event', function() {
test('should be dispatched if result has non cancelled error', function(done) {
app.start();
goog.events.listen(app, 'error', function(e) {
assert.equal(e.error, 'foo');
return done();
});
return app.load(presenter0);
});
test('should display alert', function(done) {
app.start();
app.window.alert = function(msg) {
assert.equal(msg, App.MSG_REQUEST_ERROR);
return done();
};
return app.load(presenter0);
});
return test('should not display alert', function(done) {
var alertCalled;
app.showAlertOnError = false;
app.start();
alertCalled = false;
app.window.alert = function() {
return alertCalled = true;
};
goog.events.listen(app, 'error', function(e) {
assert.isFalse(alertCalled);
return done();
});
return app.load(presenter0);
});
});
});