update store tests

pull/1348/head
Rich Harris 7 years ago
parent 9070969418
commit 9bc8b74107

@ -6,14 +6,6 @@ import { Store } from '../../store.js';
describe('store', () => { describe('store', () => {
describe('get', () => { describe('get', () => {
it('gets a specific key', () => {
const store = new Store({
foo: 'bar'
});
assert.equal(store.get('foo'), 'bar');
});
it('gets the entire state object', () => { it('gets the entire state object', () => {
const store = new Store({ const store = new Store({
foo: 'bar' foo: 'bar'
@ -31,12 +23,12 @@ describe('store', () => {
foo: 'bar' foo: 'bar'
}); });
assert.equal(store.get('foo'), 'bar'); assert.equal(store.get().foo, 'bar');
}); });
}); });
describe('observe', () => { describe('on', () => {
it('observes state', () => { it('listens to an event', () => {
let newFoo; let newFoo;
let oldFoo; let oldFoo;
@ -44,65 +36,39 @@ describe('store', () => {
foo: 'bar' foo: 'bar'
}); });
store.observe('foo', (n, o) => { store.on('state', ({ changed, current, previous }) => {
newFoo = n; newFoo = current.foo;
oldFoo = o; oldFoo = previous.foo;
}); });
assert.equal(newFoo, 'bar'); store.set({ foo: 'baz' });
assert.equal(oldFoo, undefined);
store.set({
foo: 'baz'
});
assert.equal(newFoo, 'baz'); assert.equal(newFoo, 'baz');
assert.equal(oldFoo, 'bar'); assert.equal(oldFoo, 'bar');
}); });
}); });
describe('onchange', () => { describe('fire', () => {
it('fires a callback when state changes', () => { let answer;
const store = new Store();
let count = 0;
let args;
store.onchange((state, changed) => {
count += 1;
args = { state, changed };
});
store.set({ foo: 'bar' });
assert.equal(count, 1); const store = new Store();
assert.deepEqual(args, {
state: { foo: 'bar' },
changed: { foo: true }
});
// this should be a noop store.on('custom', event => {
store.set({ foo: 'bar' }); answer = event.answer;
assert.equal(count, 1); });
// this shouldn't store.fire('custom', { answer: 42 });
store.set({ foo: 'baz' });
assert.equal(count, 2); assert.equal(answer, 42);
assert.deepEqual(args, {
state: { foo: 'baz' },
changed: { foo: true }
});
});
}); });
it('allows user to cancel state change callback', () => { it('allows user to cancel state change callback', () => {
const store = new Store(); const store = new Store();
const handler = store.onchange(() => {}); const handler = store.on('state', () => {});
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
handler.cancel(); handler.cancel();
}, TypeError, 'this._changeHandlers is undefined'); }, TypeError, 'this._handlers is undefined');
}); });
describe('computed', () => { describe('computed', () => {
@ -112,16 +78,16 @@ describe('store', () => {
}); });
store.compute('bar', ['foo'], foo => foo * 2); store.compute('bar', ['foo'], foo => foo * 2);
assert.equal(store.get('bar'), 2); assert.equal(store.get().bar, 2);
const values = []; const values = [];
store.observe('bar', bar => { store.on('state', ({ current }) => {
values.push(bar); values.push(current.bar);
}); });
store.set({ foo: 2 }); store.set({ foo: 2 });
assert.deepEqual(values, [2, 4]); assert.deepEqual(values, [4]);
}); });
it('computes a property based on another computed property', () => { it('computes a property based on another computed property', () => {
@ -131,16 +97,16 @@ describe('store', () => {
store.compute('bar', ['foo'], foo => foo * 2); store.compute('bar', ['foo'], foo => foo * 2);
store.compute('baz', ['bar'], bar => bar * 2); store.compute('baz', ['bar'], bar => bar * 2);
assert.equal(store.get('baz'), 4); assert.equal(store.get().baz, 4);
const values = []; const values = [];
store.observe('baz', baz => { store.on('state', ({ current }) => {
values.push(baz); values.push(current.baz);
}); });
store.set({ foo: 2 }); store.set({ foo: 2 });
assert.deepEqual(values, [4, 8]); assert.deepEqual(values, [8]);
}); });
it('prevents computed properties from being set', () => { it('prevents computed properties from being set', () => {
@ -192,29 +158,19 @@ describe('store', () => {
foo: value1 foo: value1
}, { immutable: true }); }, { immutable: true });
store.observe('foo', (n, o) => { store.on('state', ({ current, previous }) => {
callCount++; callCount++;
newFoo = n; newFoo = current.foo;
oldFoo = o; oldFoo = previous.foo;
}); });
assert.equal(callCount, 1); store.set({ foo: value1 });
assert.equal(newFoo, value1);
assert.equal(oldFoo, undefined);
store.set({ assert.equal(callCount, 0);
foo: value1
});
assert.equal(callCount, 1); store.set({ foo: value2 });
assert.equal(newFoo, value1);
assert.equal(oldFoo, undefined);
store.set({ assert.equal(callCount, 1);
foo: value2
});
assert.equal(callCount, 2);
assert.equal(newFoo, value2); assert.equal(newFoo, value2);
assert.equal(oldFoo, value1); assert.equal(oldFoo, value1);
}); });

Loading…
Cancel
Save