Skip to content

Instantly share code, notes, and snippets.

@toranb
Last active November 1, 2018 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toranb/3e4564781e606413d2f2d0e247b3eea1 to your computer and use it in GitHub Desktop.
Save toranb/3e4564781e606413d2f2d0e247b3eea1 to your computer and use it in GitHub Desktop.
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