aboutsummaryrefslogtreecommitdiffstats
path: root/test/lib/util.js
diff options
context:
space:
mode:
authorFrankie <frankie.diamond@gmail.com>2018-03-14 06:23:46 +0800
committerGitHub <noreply@github.com>2018-03-14 06:23:46 +0800
commitc83a9ceb04a485149fe65fbb2b44f0adeda696b1 (patch)
treec655ef0d88734a55ade8451ffe5e3032582f3ed3 /test/lib/util.js
parentd7bf6e36b1f40068eec1d6c3d30207f3fae5cfde (diff)
parent578139be4a048d1709de5c2b4b8db0a8c631d31c (diff)
downloadtangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar.gz
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar.bz2
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar.lz
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar.xz
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.tar.zst
tangerine-wallet-browser-c83a9ceb04a485149fe65fbb2b44f0adeda696b1.zip
Merge branch 'master' into i#3509
Diffstat (limited to 'test/lib/util.js')
-rw-r--r--test/lib/util.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/lib/util.js b/test/lib/util.js
new file mode 100644
index 000000000..626280745
--- /dev/null
+++ b/test/lib/util.js
@@ -0,0 +1,53 @@
+module.exports = {
+ timeout,
+ queryAsync,
+ findAsync,
+ pollUntilTruthy,
+}
+
+function timeout (time) {
+ return new Promise((resolve, reject) => {
+ setTimeout(resolve, time || 1500)
+ })
+}
+
+async function findAsync(container, selector, opts) {
+ try {
+ return await pollUntilTruthy(() => {
+ const result = container.find(selector)
+ if (result.length > 0) return result
+ }, opts)
+ } catch (err) {
+ throw new Error(`Failed to find element within interval: "${selector}"`)
+ }
+}
+
+async function queryAsync(jQuery, selector, opts) {
+ try {
+ return await pollUntilTruthy(() => {
+ const result = jQuery(selector)
+ if (result.length > 0) return result
+ }, opts)
+ } catch (err) {
+ throw new Error(`Failed to find element within interval: "${selector}"`)
+ }
+}
+
+async function pollUntilTruthy(fn, opts = {}){
+ const pollingInterval = opts.pollingInterval || 100
+ const timeoutInterval = opts.timeoutInterval || 5000
+ const start = Date.now()
+ let result
+ while (!result) {
+ // check if timedout
+ const now = Date.now()
+ if ((now - start) > timeoutInterval) {
+ throw new Error(`pollUntilTruthy - failed to return truthy within interval`)
+ }
+ // check for result
+ result = fn()
+ // run again after timeout
+ await timeout(pollingInterval, timeoutInterval)
+ }
+ return result
+}