Commit 6cb31064 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

QUnit tests are more understandable now.

parent 8fe4ddb3
...@@ -71,7 +71,16 @@ catch (e) { ...@@ -71,7 +71,16 @@ catch (e) {
} }
// //// QUnit Tests //// //// clear jio localstorage
for (var k in cookieOrLocal.getAll()) {
var splitk = k.split('/');
if ( splitk[0] === 'jio' )
cookieOrLocal.deleteItem(k);
}
//// end clear jio localstorage
//// QUnit Tests ////
module ('Jio Global tests'); module ('Jio Global tests');
...@@ -92,10 +101,8 @@ test ( "Jio simple methods", function () { ...@@ -92,10 +101,8 @@ test ( "Jio simple methods", function () {
ok ( JIO.addStorageType('qunit', function(){}) , ok ( JIO.addStorageType('qunit', function(){}) ,
"adding storage type."); "adding storage type.");
deepEqual ( o.jio.isReady(), false, '1 must be not ready'); deepEqual ( o.jio.isReady(), true, '1 must be not ready');
deepEqual ( o.jio.start(), true, '1 must be ready now');
o.jio2.start();
ok ( o.jio2.id !== o.jio.id, '1 and 2 must be different'); ok ( o.jio2.id !== o.jio.id, '1 and 2 must be different');
deepEqual ( o.jio.stop(), true, '1 must be stopped'); deepEqual ( o.jio.stop(), true, '1 must be stopped');
...@@ -115,7 +122,6 @@ test ( 'Jio Publish/Sububscribe/Unsubscribe methods', function () { ...@@ -115,7 +122,6 @@ test ( 'Jio Publish/Sububscribe/Unsubscribe methods', function () {
var o = {}; var o = {};
o.jio = JIO.createNew(); o.jio = JIO.createNew();
o.jio.start();
var spy1 = this.spy(); var spy1 = this.spy();
...@@ -138,40 +144,31 @@ test ('Check name availability', function () { ...@@ -138,40 +144,31 @@ test ('Check name availability', function () {
// Test if LocalStorage can check the availabality of a name. // Test if LocalStorage can check the availabality of a name.
// We remove MrCheckName from local storage, and checking must return true. // We remove MrCheckName from local storage, and checking must return true.
// We now add MrCheckName to local storage, and checking must return false. // We now add MrCheckName to local storage, and checking must return false.
var o = {}
var o = {};
var clock = this.sandbox.useFakeTimers(); var clock = this.sandbox.useFakeTimers();
// name must be available var t = this;
var mytest = function (value){
o.f = function (result) { o.f = function (result) {
deepEqual ( result.isAvailable, true, deepEqual(result.isAvailable,value,'checking name availabality');};
'checking name availabality'); var spy = t.spy(o,'f');
};
var spy = this.spy(o,'f');
cookieOrLocal.deleteItem ('jio/local/MrCheckName/jiotests/file');
o.jio = JIO.createNew({'type':'local','userName':'noname'},
{"ID":'noid'});
o.jio.start();
o.jio.checkNameAvailability( o.jio.checkNameAvailability(
{'userName':'MrCheckName','callback': o.f}); {'userName':'MrCheckName','callback': o.f});
clock.tick(510); clock.tick(510);
ok (o.f.calledOnce, 'callback called once'); ok (o.f.calledOnce, 'callback called once');
// name must not be available
o.f2 = function (result) {
deepEqual ( result.isAvailable, false,
'another checking name availabality');
}; };
var spy2 = this.spy(o,'f2');
cookieOrLocal.setItem ('jio/local/MrCheckName/jiotests/file',{}); // new jio
o.jio = JIO.createNew({'type':'local','userName':'noname'},
{"ID":'noid'});
o.jio.checkNameAvailability( // name must be available
{'userName':'MrCheckName','callback': o.f2}); cookieOrLocal.deleteItem ('jio/local/MrCheckName/jiotests/file');
mytest(true);
clock.tick(510); // name must be unavailable
ok (o.f2.calledOnce, 'another callback called once'); cookieOrLocal.setItem ('jio/local/MrCheckName/jiotests/file',{});
mytest(false);
o.jio.stop(); o.jio.stop();
}); });
...@@ -181,49 +178,30 @@ test ('Document save', function () { ...@@ -181,49 +178,30 @@ test ('Document save', function () {
// We launch a saving to localstorage and we check if the file is // We launch a saving to localstorage and we check if the file is
// realy saved. Then save again and check if // realy saved. Then save again and check if
var o = {} var o = {}; var clock = this.sandbox.useFakeTimers(); var t = this;
var clock = this.sandbox.useFakeTimers(); var mytest = function (value,lm,cd){
// save and check document existence
o.f = function (result) { o.f = function (result) {
deepEqual ( result.isSaved, true, deepEqual(result.isSaved,value,'saving document');};
'saving document'); var spy = t.spy(o,'f');
};
var spy = this.spy(o,'f');
cookieOrLocal.deleteItem ('jio/local/MrSaveName/jiotests/file');
clock.tick(200);
o.jio = JIO.createNew({'type':'local','userName':'MrSaveName'},
{"ID":'jiotests'});
o.jio.start();
o.jio.saveDocument( o.jio.saveDocument(
{'fileName':'file','fileContent':'content','callback': o.f}); {'fileName':'file','fileContent':'content','callback': o.f});
clock.tick(510); clock.tick(510);
ok (o.f.calledOnce, 'callback called once'); ok (o.f.calledOnce, 'callback called once');
// check content // check content
var tmp = cookieOrLocal.getItem ('jio/local/MrSaveName/jiotests/file'); var tmp = cookieOrLocal.getItem ('jio/local/MrSaveName/jiotests/file');
deepEqual (tmp,{'fileName':'file','fileContent':'content', deepEqual (tmp,{'fileName':'file','fileContent':'content',
'lastModified':200,'creationDate':200}, 'check content'); 'lastModified':lm,'creationDate':cd},'check content');
// re-save and check modification date
o.f2 = function (result) {
deepEqual ( result.isSaved, true,
'saving document again');
}; };
var spy2 = this.spy(o,'f2');
o.jio.saveDocument( o.jio = JIO.createNew({'type':'local','userName':'MrSaveName'},
{'fileName':'file','fileContent':'content','callback': o.f2}); {"ID":'jiotests'});
cookieOrLocal.deleteItem ('jio/local/MrSaveName/jiotests/file');
clock.tick(510); // save and check document existence
ok (o.f2.calledOnce, 'another callback called once'); clock.tick(200);
mytest(true,200,200); // value, lastmodified, creationdate
// check content // re-save and check modification date
tmp = cookieOrLocal.getItem ('jio/local/MrSaveName/jiotests/file'); mytest(true,710,200); // 710 = 200 + 510 ms (clock tick)
deepEqual (tmp,{'fileName':'file','fileContent':'content',
'lastModified':710,'creationDate':200}, 'check content');
o.jio.stop(); o.jio.stop();
}); });
...@@ -233,43 +211,27 @@ test ('Document load', function () { ...@@ -233,43 +211,27 @@ test ('Document load', function () {
// We launch a loading from localstorage and we check if the file is // We launch a loading from localstorage and we check if the file is
// realy loaded. // realy loaded.
var o = {} var o = {}; var clock = this.sandbox.useFakeTimers(); var t = this;
var clock = this.sandbox.useFakeTimers(); var mytest = function (res,value){
// load a non existing file
o.f = function (result) { o.f = function (result) {
deepEqual ( result.status, 'fail', deepEqual(result[res],value,'loading document');};
'loading document'); var spy = t.spy(o,'f');
};
var spy = this.spy(o,'f');
cookieOrLocal.deleteItem ('jio/local/MrLoadName/jiotests/file');
o.jio = JIO.createNew({'type':'local','userName':'MrLoadName'},
{"ID":'jiotests'});
o.jio.start();
o.jio.loadDocument( o.jio.loadDocument(
{'fileName':'file','callback': o.f}); {'fileName':'file','callback': o.f});
clock.tick(510); clock.tick(510);
ok (o.f.calledOnce, 'callback called once'); ok (o.f.calledOnce, 'callback called once');
// re-load file after saving it manually
var tmp = cookieOrLocal.setItem ('jio/local/MrLoadName/jiotests/file',
{'fileName':'file','fileContent':'content',
'lastModified':1234,'creationDate':1000});
o.f2 = function (result) {
deepEqual ( result.document,
{'fileName':'file','fileContent':'content',
'lastModified':1234,'creationDate':1000},
'loading document again, check content');
}; };
var spy2 = this.spy(o,'f2'); o.jio = JIO.createNew({'type':'local','userName':'MrLoadName'},
{"ID":'jiotests'});
o.jio.loadDocument( // load a non existing file
{'fileName':'file','callback': o.f2}); cookieOrLocal.deleteItem ('jio/local/MrLoadName/jiotests/file');
mytest ('status','fail');
clock.tick(510); // re-load file after saving it manually
ok (o.f2.calledOnce, 'another callback called once'); var doc = {'fileName':'file','fileContent':'content',
'lastModified':1234,'creationDate':1000};
cookieOrLocal.setItem ('jio/local/MrLoadName/jiotests/file',doc);
mytest ('document',doc);
o.jio.stop(); o.jio.stop();
}); });
...@@ -279,28 +241,24 @@ test ('Document remove', function () { ...@@ -279,28 +241,24 @@ test ('Document remove', function () {
// We launch a remove from localstorage and we check if the file is // We launch a remove from localstorage and we check if the file is
// realy removed. // realy removed.
var o = {} var o = {}; var clock = this.sandbox.useFakeTimers(); var t = this;
var clock = this.sandbox.useFakeTimers(); var mytest = function (){
o.f = function (result) { o.f = function (result) {
deepEqual ( result.isRemoved, true, deepEqual(result.isRemoved,true,'removing document');};
'removing document'); var spy = t.spy(o,'f');
};
var spy = this.spy(o,'f');
cookieOrLocal.setItem ('jio/local/MrRemoveName/jiotests/file',{});
o.jio = JIO.createNew({'type':'local','userName':'MrRemoveName'},
{"ID":'jiotests'});
o.jio.start();
o.jio.removeDocument( o.jio.removeDocument(
{'fileName':'file','callback': o.f}); {'fileName':'file','callback': o.f});
clock.tick(510); clock.tick(510);
ok (o.f.calledOnce, 'callback called once'); ok (o.f.calledOnce, 'callback called once');
// check if the file is still there
var tmp = cookieOrLocal.getItem ('jio/local/MrRemoveName/jiotests/file'); var tmp = cookieOrLocal.getItem ('jio/local/MrRemoveName/jiotests/file');
ok (!tmp, 'check no content'); ok (!tmp, 'check no content');
};
o.jio = JIO.createNew({'type':'local','userName':'MrRemoveName'},
{"ID":'jiotests'});
// test removing a file
cookieOrLocal.setItem ('jio/local/MrRemoveName/jiotests/file',{});
mytest ();
o.jio.stop(); o.jio.stop();
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment