2023-07-27 13:20:24 +00:00
|
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
2022-03-25 12:31:21 +00:00
|
|
|
import * as smartstate from '../ts/index.js';
|
2020-11-29 23:51:05 +00:00
|
|
|
|
|
|
|
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>();
|
2022-03-25 12:31:21 +00:00
|
|
|
expect(testState).toBeInstanceOf(smartstate.Smartstate);
|
2020-11-29 23:51:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should create a new StatePart', async () => {
|
2023-10-03 14:20:34 +00:00
|
|
|
testStatePart = await testState.getStatePart<TStatePartPayload>('testStatePart', {
|
2020-11-29 23:51:05 +00:00
|
|
|
currentFavorites: [],
|
|
|
|
deep: {
|
|
|
|
hi: 2,
|
|
|
|
},
|
|
|
|
});
|
2022-03-25 12:31:21 +00:00
|
|
|
expect(testStatePart).toBeInstanceOf(smartstate.StatePart);
|
2020-11-29 23:51:05 +00:00
|
|
|
console.log(testStatePart);
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should select something', async () => {
|
|
|
|
testStatePart
|
|
|
|
.select((state) => state.deep.hi)
|
|
|
|
.subscribe((substate) => {
|
2022-03-25 12:31:21 +00:00
|
|
|
expect(substate).toEqual(2);
|
2020-11-29 23:51:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should dispatch a state action', async (tools) => {
|
|
|
|
const done = tools.defer();
|
|
|
|
const addFavourite = testStatePart.createAction<string>(async (statePart, payload) => {
|
|
|
|
const currentState = statePart.getState();
|
|
|
|
currentState.currentFavorites.push(payload);
|
|
|
|
return currentState;
|
|
|
|
});
|
|
|
|
testStatePart
|
|
|
|
.waitUntilPresent((state) => {
|
|
|
|
return state.currentFavorites[0];
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
await testStatePart.dispatchAction(addFavourite, 'my favourite things');
|
2022-03-25 12:31:21 +00:00
|
|
|
expect(testStatePart.getState().currentFavorites).toContain('my favourite things');
|
2020-11-29 23:51:05 +00:00
|
|
|
await done.promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.start();
|