fix(core): update

This commit is contained in:
Philipp Kunz 2019-09-25 17:09:35 +02:00
parent b8d0ec55ad
commit 8d296cf08d
2 changed files with 12 additions and 4 deletions

View File

@ -36,14 +36,21 @@ tap.test('should select something', async () => {
});
});
tap.test('should dispatch a state action', async () => {
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');
expect(testStatePart.getState().currentFavorites).to.include('my favourite things');
await done.promise;
});
tap.start();

View File

@ -83,12 +83,13 @@ export class StatePart<TStatePartName, TStatePayload> {
public async waitUntilPresent<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Promise<T> {
const done = plugins.smartpromise.defer<T>();
const selectedObservable = this.select(selectorFn);
const subscription = selectedObservable.subscribe(value => {
const subscription = selectedObservable.subscribe(async value => {
if (value) {
subscription.unsubscribe();
done.resolve(value);
}
});
return await done.promise;
const result = await done.promise;
subscription.unsubscribe();
return result;
}
}