diff --git a/.size-snapshot.json b/.size-snapshot.json index 363648a9..61f524b6 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -1,7 +1,7 @@ { "dist/dom-testing-library.umd.js": { - "bundled": 146890, - "minified": 53947, - "gzipped": 15849 + "bundled": 146354, + "minified": 53637, + "gzipped": 15842 } } diff --git a/src/__tests__/helpers.js b/src/__tests__/helpers.js new file mode 100644 index 00000000..9e80cdb8 --- /dev/null +++ b/src/__tests__/helpers.js @@ -0,0 +1,40 @@ +import {getDocument, newMutationObserver} from '../helpers' + +describe('getDocument', () => { + if (typeof document === 'undefined') { + test('throws an error if window does not exist', () => { + expect(() => getDocument()).toThrowError( + /Could not find default container/, + ) + }) + } else { + test('returns global document if exists', () => { + expect(getDocument()).toBe(document) + }) + } +}) + +class DummyClass { + constructor(args) { + this.args = args + } +} + +describe('newMutationObserver', () => { + if (typeof window === 'undefined') { + it('instantiates mock MutationObserver if not availble on window', () => { + expect(newMutationObserver(() => {}).observe).toBeDefined() + }) + } else { + it('instantiates from global MutationObserver if available', () => { + const oldMutationObserver = window.MutationObserver + window.MutationObserver = DummyClass + + try { + expect(newMutationObserver('foobar').args).toEqual('foobar') + } finally { + window.MutationObserver = oldMutationObserver + } + }) + } +}) diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 00000000..afafe115 --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,20 @@ +import MutationObserver from '@sheerun/mutationobserver-shim' + +function newMutationObserver(onMutation) { + const MutationObserverConstructor = + typeof window !== 'undefined' && + typeof window.MutationObserver !== 'undefined' + ? window.MutationObserver + : MutationObserver + + return new MutationObserverConstructor(onMutation) +} + +function getDocument() { + if (typeof window === 'undefined') { + throw new Error('Could not find default container') + } + return window.document +} + +export {getDocument, newMutationObserver} diff --git a/src/wait-for-dom-change.js b/src/wait-for-dom-change.js index ead39b2c..a4ef4841 100644 --- a/src/wait-for-dom-change.js +++ b/src/wait-for-dom-change.js @@ -1,4 +1,4 @@ -import MutationObserver from '@sheerun/mutationobserver-shim' +import {newMutationObserver, getDocument} from './helpers' function waitForDomChange({ container = getDocument(), @@ -12,13 +12,7 @@ function waitForDomChange({ } = {}) { return new Promise((resolve, reject) => { const timer = setTimeout(onTimeout, timeout) - /* istanbul ignore next */ - const MutationObserverConstructor = - typeof window !== 'undefined' && - typeof window.MutationObserver !== 'undefined' - ? window.MutationObserver - : MutationObserver - const observer = new MutationObserverConstructor(onMutation) + const observer = newMutationObserver(onMutation) observer.observe(container, mutationObserverOptions) function onDone(error, result) { @@ -39,12 +33,4 @@ function waitForDomChange({ }) } -function getDocument() { - /* istanbul ignore if */ - if (typeof window === 'undefined') { - throw new Error('Could not find default container') - } - return window.document -} - export {waitForDomChange} diff --git a/src/wait-for-element.js b/src/wait-for-element.js index 01195445..41e92092 100644 --- a/src/wait-for-element.js +++ b/src/wait-for-element.js @@ -1,4 +1,4 @@ -import MutationObserver from '@sheerun/mutationobserver-shim' +import {newMutationObserver, getDocument} from './helpers' function waitForElement( callback, @@ -19,13 +19,7 @@ function waitForElement( } let lastError const timer = setTimeout(onTimeout, timeout) - /* istanbul ignore next */ - const MutationObserverConstructor = - typeof window !== 'undefined' && - typeof window.MutationObserver !== 'undefined' - ? window.MutationObserver - : MutationObserver - const observer = new MutationObserverConstructor(onMutation) + const observer = newMutationObserver(onMutation) observer.observe(container, mutationObserverOptions) function onDone(error, result) { clearTimeout(timer) @@ -56,12 +50,4 @@ function waitForElement( }) } -function getDocument() { - /* istanbul ignore if */ - if (typeof window === 'undefined') { - throw new Error('Could not find default container') - } - return window.document -} - export {waitForElement}