Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dist/dom-testing-library.umd.js": {
"bundled": 146890,
"minified": 53947,
"gzipped": 15849
"bundled": 146354,
"minified": 53637,
"gzipped": 15842
}
}
40 changes: 40 additions & 0 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -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
}
})
}
})
20 changes: 20 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -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}
18 changes: 2 additions & 16 deletions src/wait-for-dom-change.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MutationObserver from '@sheerun/mutationobserver-shim'
import {newMutationObserver, getDocument} from './helpers'

function waitForDomChange({
container = getDocument(),
Expand All @@ -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) {
Expand All @@ -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}
18 changes: 2 additions & 16 deletions src/wait-for-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MutationObserver from '@sheerun/mutationobserver-shim'
import {newMutationObserver, getDocument} from './helpers'

function waitForElement(
callback,
Expand All @@ -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)
Expand Down Expand Up @@ -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}