Last active
November 1, 2018 21:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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