Skip to content

Instantly share code, notes, and snippets.

@toranb toranb/wait-for-redux.js
Last active Nov 1, 2018

Embed
What would you like to do?
custom ember helper to wait for redux to be in a given state
import { Promise } from 'rsvp';
import { next } from '@ember/runloop';
import { registerWaiter } from '@ember/test';
import { getContext } from '@ember/test-helpers';
export async function waitForRedux(key, value) {
return new Promise(async (resolve) => {
let counter = 1;
registerWaiter(() => counter === 0);
const { owner } = getContext();
const redux = owner.lookup('service:redux');
const unsubscribe = redux.store.subscribe(() => {
const state = redux.store.getState();
const currentValue = key.split('.').reduce((prev, path) => prev[path], state);
if (currentValue === value) {
unsubscribe();
counter -= 1;
next(null, resolve);
}
});
});
}
// await waitForRedux('foo.bar.baz', 'hello world');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.