smartstate/test/test.ts

59 lines
1.7 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
});
});
2020-05-18 04:10:36 +00:00
tap.test('should dispatch a state action', async tools => {
2019-09-25 15:09:35 +00:00
const done = tools.defer();
2019-02-26 17:09:38 +00:00
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;
});
2020-05-18 04:10:36 +00:00
testStatePart
.waitUntilPresent(state => {
return state.currentFavorites[0];
})
.then(() => {
done.resolve();
});
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-09-25 15:09:35 +00:00
await done.promise;
2019-02-26 17:09:38 +00:00
});
2019-02-21 20:48:39 +00:00
tap.start();