Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 58a6161

Browse files
committed
Add test util function to do assertion on the state.
1 parent 02b5fbc commit 58a6161

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

packages/devtools-reps/src/object-inspector/tests/test-utils.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
// @flow
6-
6+
import type { Store } from "../types";
77
const {WAIT_UNTIL_TYPE} = require("../../shared/redux/middleware/waitUntilService");
88

99
/*
@@ -67,7 +67,83 @@ function waitForDispatch(store: Object, type: string) {
6767
});
6868
}
6969

70+
/**
71+
* Wait until the condition evaluates to something truethy
72+
* @param {function} condition: function that we need for returning something truthy.
73+
* @param {int} interval: Time to wait before trying to evaluate condition again
74+
* @param {int} maxTries: Number of evaluation to try.
75+
*/
76+
async function waitFor(
77+
condition: (any) => any,
78+
interval: number = 50,
79+
maxTries: number = 100
80+
) {
81+
let res = condition();
82+
while (!res) {
83+
await new Promise(done => setTimeout(done, interval));
84+
maxTries--;
85+
86+
if (maxTries <= 0) {
87+
throw new Error("waitFor - maxTries limit hit");
88+
}
89+
90+
res = condition();
91+
}
92+
return res;
93+
}
94+
95+
/**
96+
* Wait until the state has all the expected keys for the loadedProperties state prop.
97+
* @param {Redux Store} store: function that we need for returning something truthy.
98+
* @param {Array} expectedKeys: Array of stringified keys.
99+
* @param {int} interval: Time to wait before trying to evaluate condition again
100+
* @param {int} maxTries: Number of evaluation to try.
101+
*/
102+
function waitForLoadedProperties(
103+
store: Store,
104+
expectedKeys: Array<string>,
105+
interval: number,
106+
maxTries: number
107+
) : Promise<any> {
108+
return waitFor(() =>
109+
storeHasLoadedPropertiesKeys(store, expectedKeys), interval, maxTries);
110+
}
111+
112+
function storeHasLoadedPropertiesKeys(store: Store, expectedKeys: Array<string>) {
113+
return expectedKeys.every(key => storeHasLoadedProperty(store, key));
114+
}
115+
116+
function storeHasLoadedProperty(store: Store, key: string) : boolean {
117+
return [...store.getState().loadedProperties.keys()].some(k => k.toString() === key);
118+
}
119+
120+
function storeHasExactLoadedProperties(store: Store, expectedKeys: Array<string>) {
121+
return expectedKeys.length === store.getState().loadedProperties.size
122+
&& expectedKeys.every(key => storeHasLoadedProperty(store, key));
123+
}
124+
125+
function storeHasExpandedPaths(store: Store, expectedKeys: Array<string>) {
126+
return expectedKeys.every(key => storeHasExpandedPath(store, key));
127+
}
128+
129+
function storeHasExpandedPath(store: Store, key: string) : boolean {
130+
return [...store.getState().expandedPaths.keys()].some(k => k.toString() === key);
131+
}
132+
133+
function storeHasExactExpandedPaths(store: Store, expectedKeys: Array<string>) {
134+
return expectedKeys.length === store.getState().expandedPaths.size
135+
&& expectedKeys.every(key => storeHasExpandedPath(store, key));
136+
}
137+
70138
module.exports = {
71139
formatObjectInspector,
140+
storeHasExpandedPaths,
141+
storeHasExpandedPath,
142+
storeHasExactExpandedPaths,
143+
storeHasLoadedPropertiesKeys,
144+
storeHasLoadedProperty,
145+
storeHasExactLoadedProperties,
146+
waitFor,
72147
waitForDispatch,
148+
waitForLoadedProperties,
73149
};

0 commit comments

Comments
 (0)