Files
cosmopolite/static/test.html

133 lines
3.2 KiB
HTML
Raw Normal View History

<html>
<head>
<meta charset="utf-8">
<title>Cosmopolite tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-git.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-git.js"></script>
<script src="/cosmopolite/static/cosmopolite.js" charset="UTF-8"></script>
<script>
QUnit.begin(localStorage.clear.bind(localStorage));
QUnit.done(localStorage.clear.bind(localStorage));
var randstring = function() {
var ret = [];
for (var i = 0; i < 16; i++) {
var ran = (Math.random() * 16) | 0;
ret.push(ran.toString(16));
}
return ret.join('');
};
test('Construct/shutdown', function() {
expect(2);
var cosmo = new Cosmopolite({});
ok(true, 'new Cosmopolite() succeeds');
cosmo.shutdown();
ok(true, 'shutdown() succeeds');
});
asyncTest('onLogin or onLogout', function() {
expect(1);
var callbacks = {
'onLogin': function(user, logout_url) {
ok(true, 'onLogin fired');
cosmo.shutdown();
start();
},
'onLogout': function(login_url) {
ok(true, 'onLogout fired');
cosmo.shutdown();
start();
}
};
var cosmo = new Cosmopolite(callbacks);
});
asyncTest('Message round trip', function() {
expect(2);
var subject = randstring();
var message = randstring();
var callbacks1 = {
'onReady': function() {
cosmo1.sendMessage(subject, message);
},
};
var callbacks2 = {
'onReady': function() {
cosmo2.subscribe(subject, -1);
},
'onMessage': function(e) {
equal(e['subject'], subject, 'subject matches');
equal(e['message'], message, 'message matches');
cosmo1.shutdown();
cosmo2.shutdown();
start();
},
};
var cosmo1 = new Cosmopolite(callbacks1, null, randstring());
var cosmo2 = new Cosmopolite(callbacks2, null, randstring());
});
asyncTest('Overwrite key', function() {
expect(8);
var subject = randstring();
var message1 = randstring();
var message2 = randstring();
var key = randstring();
var messages1 = 0;
var callbacks1 = {
'onReady': function() {
cosmo1.subscribe(subject, -1);
cosmo1.sendMessage(subject, message1, key);
},
'onMessage': function(e) {
messages1++;
if (messages1 == 1) {
cosmo1.sendMessage(subject, message2, key);
}
},
};
var messages2 = 0;
var callbacks2 = {
'onReady': function() {
cosmo2.subscribe(subject, -1);
},
'onMessage': function(e) {
messages2++;
equal(e['subject'], subject, 'subject matches');
equal(e['key'], key, 'key matches');
if (messages2 == 1) {
equal(e['message'], message1, 'message #1 matches');
equal(cosmo2.getKeyMessage(subject, key)['message'], message1, 'message #1 matches by key')
return;
}
equal(e['message'], message2, 'message #2 matches');
equal(cosmo2.getKeyMessage(subject, key)['message'], message2, 'message #2 matches by key')
cosmo1.shutdown();
cosmo2.shutdown();
start();
},
};
var cosmo1 = new Cosmopolite(callbacks1, null, randstring());
var cosmo2 = new Cosmopolite(callbacks2, null, randstring());
});
</script>
</body>
</html>