store async cb cleanup

This commit is contained in:
Radek Stepan 2016-01-26 18:34:07 +01:00
parent 110354d740
commit 208d2f2cea
2 changed files with 84 additions and 49 deletions

View File

@ -20,7 +20,6 @@ export default class Store extends EventEmitter {
} }
// Register an async function callback, handle loading state. // Register an async function callback, handle loading state.
// TODO: unit-test.
cb(fn) { cb(fn) {
let id = _.uniqueId(); let id = _.uniqueId();
actions.emit('system.loading', true); actions.emit('system.loading', true);

View File

@ -1,57 +1,93 @@
import { assert } from 'chai'; import { assert } from 'chai';
import Store from '../src/js/lib/Store.js'; import Store from '../src/js/lib/Store.js';
import actions from '../src/js/actions/appActions.js';
export default { export default {
Store: { 'store - set': (done) => {
set(done) { let s = new Store();
let s = new Store();
s.set('A.B', 1); s.set('A.B', 1);
assert.deepEqual({ A: { B: 1 }}, s.get()); assert.deepEqual({ A: { B: 1 }}, s.get());
done();
},
'store - setSilent': (done) => {
let s = new Store();
let val;
s.onAny(v => val = v);
s.set('A.B', 1, true);
s.set('A.B', 2, false);
assert.deepEqual({ A: { B: 2 }}, s.get());
assert.equal(1, val);
done();
},
'store - assign': (done) => {
let s = new Store({ A: 1 });
s.set({ B: 1 });
assert.deepEqual({ A: 1, B: 1 }, s.get());
done();
},
'store - changes': (done) => {
let s = new Store({ A: { B: { C: 1 } } });
let key;
s.onAny((v, k) => key = k);
s.set('A', { B: { C: 2 } });
assert.equal('A.B.C', key);
done();
},
'store - cb called': (done) => {
let s = new Store();
let events = [];
actions.on('system.loading', (val) => events.push(val));
let called = false;
let cb = s.cb(() => called = true);
assert.equal(Object.keys(s._cbs).length, 1);
cb();
assert.ok(called);
assert.equal(Object.keys(s._cbs).length, 0);
assert.deepEqual(events, [ true, false ]);
done();
},
'store - cb cancelled': (done) => {
let s = new Store();
let events = [];
actions.on('system.loading', (val) => events.push(val));
let called = false;
let cb = s.cb(() => called = true);
assert.equal(Object.keys(s._cbs).length, 1);
setTimeout(cb, 10);
s.clean();
assert.equal(Object.keys(s._cbs).length, 0);
setTimeout(() => {
assert.ok(!called);
assert.deepEqual(events, [ true, false ]);
done(); done();
}, }, 20);
setSilent(done) {
let s = new Store();
let val;
s.onAny(v => val = v);
s.set('A.B', 1, true);
s.set('A.B', 2, false);
assert.deepEqual({ A: { B: 2 }}, s.get());
assert.equal(1, val);
done();
},
assign(done) {
let s = new Store({ A: 1 });
s.set({ B: 1 });
assert.deepEqual({ A: 1, B: 1 }, s.get());
done();
},
changes(done) {
let s = new Store({ A: { B: { C: 1 } } });
let key;
s.onAny((v, k) => key = k);
s.set('A', { B: { C: 2 } });
assert.equal('A.B.C', key);
done();
}
} }
}; };