Skip to content

Commit

Permalink
tests: do not use callbacks anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Nov 11, 2016
1 parent 1ebc262 commit 8066a6b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 101 deletions.
1 change: 1 addition & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ globals:
before: false
beforeEach: false
after: false
Promise: false

rules:
strict: 0
Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/proclaim/lib/proclaim.js"></script>
<script src="../node_modules/lie/dist/lie.polyfill.js"></script>
<script src="../bag.js"></script>
<script>
mocha.ui('bdd');
Expand Down
41 changes: 21 additions & 20 deletions test/test_extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,34 @@ describe('extra tests', function () {
});


it('reset localstorage (namespace) when quota exceeded (2.5-5Mb)', function (done) {
it('reset localstorage (namespace) when quota exceeded (2.5-5Mb)', function () {
var b = new window.Bag({ stores: [ 'localstorage' ] });

// create big data (>5mb in json)
var huge = new Array(1000000);
for (var i = 0, l = huge.length; i < l; i++) huge[i] = '1234567890';

b.set('permanent', 'permanent', function (err) {
assert.notOk(err);
b.set('tmp', 'tmp', 15, function (err) {
assert.notOk(err);
b.set('huge', huge, function (err) {
assert.ok(err);

b.get('tmp', function (err, val) {
assert.notOk(err);
assert.isUndefined(val);

b.get('permanent', function (err, val) {
assert.notOk(err);
assert.isUndefined(val);
b.clear(done);
});
});
});
return b.set('permanent', 'permanent')
.then(function () {
return b.set('tmp', 'tmp', 15);
})
.then(function () {
return b.set('huge', huge);
})
.then(function () {
throw new Error('Set hude data should fail');
}, function () {
return b.get('tmp');
})
.then(function (val) {
assert.isUndefined(val);

return b.get('permanent');
})
.then(function (val) {
assert.isUndefined(val);
return b.clear();
});
});
});


Expand Down
49 changes: 22 additions & 27 deletions test/test_indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,32 @@ describe('IndexedDB tests', function () {
});


it('keep not expired', function (done) {
bag.set(key, obj, 1, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.deepEqual(obj, data);
done();
});
});
}, 10);
function pTimeout(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}


it('keep not expired', function () {
return bag.set(key, obj, 1)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.deepEqual(obj, data);
});
});


it('clear expired', function (done) {
bag.set(key, obj, 0.005, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.isUndefined(data);
done();
});
});
}, 10);
});
it('clear expired', function () {
return bag.set(key, obj, 0.005)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.isUndefined(data);
});
});

});
48 changes: 21 additions & 27 deletions test/test_localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,31 @@ describe('localStorage tests', function () {
});


it('keep not expired', function (done) {
bag.set(key, obj, 1, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.deepEqual(obj, data);
done();
});
});
}, 10);
function pTimeout(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}

it('keep not expired', function () {
return bag.set(key, obj, 1)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.deepEqual(obj, data);
});
});


it('clear expired', function (done) {
bag.set(key, obj, 0.005, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.isUndefined(data);
done();
});
});
}, 10);
});
it('clear expired', function () {
return bag.set(key, obj, 0.005)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.isUndefined(data);
});
});

});
48 changes: 21 additions & 27 deletions test/test_websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,31 @@ describe('WebSQL tests', function () {
});


it('keep not expired', function (done) {
bag.set(key, obj, 1, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.deepEqual(obj, data);
done();
});
});
}, 10);
function pTimeout(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}

it('keep not expired', function () {
return bag.set(key, obj, 1)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.deepEqual(obj, data);
});
});


it('clear expired', function (done) {
bag.set(key, obj, 0.005, function (err) {
assert.notOk(err);
setTimeout(function () {
bag.clear(true, function (err) {
assert.notOk(err);
bag.get(key, function (err, data) {
assert.notOk(err);
assert.isUndefined(data);
done();
});
});
}, 10);
});
it('clear expired', function () {
return bag.set(key, obj, 0.005)
.then(function () { return pTimeout(10); })
.then(function () { return bag.clear(true); })
.then(function () { return bag.get(key); })
.then(function (data) {
assert.isUndefined(data);
});
});

});

0 comments on commit 8066a6b

Please sign in to comment.