diff options
author | Luke Bennett <lbennett@gitlab.com> | 2018-11-26 11:57:21 +0300 |
---|---|---|
committer | Fatih Acet <acetfatih@gmail.com> | 2018-11-26 11:57:21 +0300 |
commit | 1877eb49c0bfe855eca74209441f954c4550ffef (patch) | |
tree | 7bc47b4d007f48f8504ee1d6a4c2329d2324adf5 /spec/javascripts/helpers | |
parent | ceae9f5211a77333810dfd704823381730eb1791 (diff) |
Attempt to fix lazy loader spec transient failure
Diffstat (limited to 'spec/javascripts/helpers')
-rw-r--r-- | spec/javascripts/helpers/scroll_into_view_promise.js | 28 | ||||
-rw-r--r-- | spec/javascripts/helpers/wait_for_attribute_change.js | 16 |
2 files changed, 44 insertions, 0 deletions
diff --git a/spec/javascripts/helpers/scroll_into_view_promise.js b/spec/javascripts/helpers/scroll_into_view_promise.js new file mode 100644 index 00000000000..0edea2103da --- /dev/null +++ b/spec/javascripts/helpers/scroll_into_view_promise.js @@ -0,0 +1,28 @@ +export default function scrollIntoViewPromise(intersectionTarget, timeout = 100, maxTries = 5) { + return new Promise((resolve, reject) => { + let intersectionObserver; + let retry = 0; + + const intervalId = setInterval(() => { + if (retry >= maxTries) { + intersectionObserver.disconnect(); + clearInterval(intervalId); + reject(new Error(`Could not scroll target into viewPort within ${timeout * maxTries} ms`)); + } + retry += 1; + intersectionTarget.scrollIntoView(); + }, timeout); + + intersectionObserver = new IntersectionObserver(entries => { + if (entries[0].isIntersecting) { + intersectionObserver.disconnect(); + clearInterval(intervalId); + resolve(); + } + }); + + intersectionObserver.observe(intersectionTarget); + + intersectionTarget.scrollIntoView(); + }); +} diff --git a/spec/javascripts/helpers/wait_for_attribute_change.js b/spec/javascripts/helpers/wait_for_attribute_change.js new file mode 100644 index 00000000000..8f22d569222 --- /dev/null +++ b/spec/javascripts/helpers/wait_for_attribute_change.js @@ -0,0 +1,16 @@ +export default (domElement, attributes, timeout = 1500) => + new Promise((resolve, reject) => { + let observer; + const timeoutId = setTimeout(() => { + observer.disconnect(); + reject(new Error(`Could not see an attribute update within ${timeout} ms`)); + }, timeout); + + observer = new MutationObserver(() => { + clearTimeout(timeoutId); + observer.disconnect(); + resolve(); + }); + + observer.observe(domElement, { attributes: true, attributeFilter: attributes }); + }); |