smartstate/test/test.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-02-21 20:48:39 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartstate from '../ts/index';
type TMyStateParts = 'testStatePart';
interface TStatePartPayload {
currentFavorites: string[];
deep: {
hi: number;
};
}
let testState: smartstate.Smartstate<TMyStateParts>;
let testStatePart: smartstate.StatePart<TMyStateParts, TStatePartPayload>;
tap.test('should create a new SmartState', async () => {
testState = new smartstate.Smartstate<TMyStateParts>();
expect(testState).to.be.instanceOf(smartstate.Smartstate);
});
tap.test('should create a new StatePart', async () => {
testStatePart = testState.getStatePart<TStatePartPayload>('testStatePart', {
currentFavorites: [],
deep: {
hi: 2
}
});
expect(testStatePart).to.be.instanceOf(smartstate.StatePart);
console.log(testStatePart);
});
tap.test('should select something', async () => {
testStatePart
2019-02-26 17:09:38 +00:00
.select(state => state.deep.hi)
2019-02-21 20:48:39 +00:00
.subscribe(substate => {
2019-02-26 17:09:38 +00:00
expect(substate).to.equal(2);
2019-02-21 20:48:39 +00:00
});
});
2019-02-26 17:09:38 +00:00
tap.test('should dispatch a state action', async () => {
const addFavourite = testStatePart.createAction<string>(async (statePart, payload) => {
const currentState = statePart.getState();
2019-02-27 01:00:47 +00:00
currentState.currentFavorites.push(payload);
2019-02-26 17:09:38 +00:00
return currentState;
});
2019-02-27 01:00:47 +00:00
await testStatePart.dispatchAction(addFavourite, 'my favourite things');
expect(testStatePart.getState().currentFavorites).to.include('my favourite things');
2019-02-26 17:09:38 +00:00
});
2019-02-21 20:48:39 +00:00
tap.start();