Skip to content

Commit bd2261f

Browse files
Kent C. Doddskentcdodds
Kent C. Dodds
authored andcommitted
feat: remove mutationobserver shim (#457)
Closes #413 Closes #357 BREAKING CHANGE: MutationObserver is supported by all major browsers and recent versions of JSDOM. If you need, you can create your own shim (using @sheerun/mutationobserver-shim) and attach it to the window.
1 parent 9b1725e commit bd2261f

File tree

6 files changed

+27
-63
lines changed

6 files changed

+27
-63
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
],
4242
"dependencies": {
4343
"@babel/runtime": "^7.8.4",
44-
"@sheerun/mutationobserver-shim": "^0.3.2",
4544
"@types/testing-library__dom": "^6.12.1",
4645
"aria-query": "^4.0.2",
4746
"dom-accessibility-api": "^0.3.0",

src/__tests__/helpers.js

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
1-
import {getDocument, newMutationObserver} from '../helpers'
1+
import {getDocument} from '../helpers'
22

33
test('returns global document if exists', () => {
44
expect(getDocument()).toBe(document)
55
})
6-
7-
class DummyClass {
8-
constructor(args) {
9-
this.args = args
10-
}
11-
}
12-
13-
describe('newMutationObserver', () => {
14-
if (typeof window === 'undefined') {
15-
it('instantiates mock MutationObserver if not availble on window', () => {
16-
expect(newMutationObserver(() => {}).observe).toBeDefined()
17-
})
18-
} else {
19-
it('instantiates from global MutationObserver if available', () => {
20-
const oldMutationObserver = window.MutationObserver
21-
window.MutationObserver = DummyClass
22-
23-
try {
24-
expect(newMutationObserver('foobar').args).toEqual('foobar')
25-
} finally {
26-
window.MutationObserver = oldMutationObserver
27-
}
28-
})
29-
}
30-
})

src/events.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {getWindowFromNode} from './helpers'
12
const eventMap = {
23
// Clipboard Events
34
copy: {
@@ -410,25 +411,6 @@ Object.keys(eventMap).forEach(key => {
410411
fireEvent[key] = (node, init) => fireEvent(node, createEvent[key](node, init))
411412
})
412413

413-
function getWindowFromNode(node) {
414-
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
415-
if (node.defaultView) {
416-
// node is document
417-
return node.defaultView
418-
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
419-
// node is a DOM node
420-
return node.ownerDocument.defaultView
421-
} else if (node.window) {
422-
// node is window
423-
return node.window
424-
} else {
425-
// no idea...
426-
throw new Error(
427-
`Unable to find the "window" object for the given node. fireEvent currently supports firing events on DOM nodes, document, and window. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
428-
)
429-
}
430-
}
431-
432414
// function written after some investigation here:
433415
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
434416
function setNativeValue(element, value) {

src/helpers.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import MutationObserver from '@sheerun/mutationobserver-shim'
2-
31
const globalObj = typeof window === 'undefined' ? global : window
42

53
// Currently this fn only supports jest timers, but it could support other test runners in the future.
@@ -41,27 +39,35 @@ const {clearTimeoutFn, setImmediateFn, setTimeoutFn} = runWithRealTimers(
4139
getTimeFunctions,
4240
)
4341

44-
function newMutationObserver(onMutation) {
45-
const MutationObserverConstructor =
46-
typeof window !== 'undefined' &&
47-
typeof window.MutationObserver !== 'undefined'
48-
? window.MutationObserver
49-
: MutationObserver
50-
51-
return new MutationObserverConstructor(onMutation)
52-
}
53-
5442
function getDocument() {
5543
/* istanbul ignore if */
5644
if (typeof window === 'undefined') {
5745
throw new Error('Could not find default container')
5846
}
5947
return window.document
6048
}
49+
function getWindowFromNode(node) {
50+
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
51+
if (node.defaultView) {
52+
// node is document
53+
return node.defaultView
54+
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
55+
// node is a DOM node
56+
return node.ownerDocument.defaultView
57+
} else if (node.window) {
58+
// node is window
59+
return node.window
60+
} else {
61+
// no idea...
62+
throw new Error(
63+
`Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
64+
)
65+
}
66+
}
6167

6268
export {
69+
getWindowFromNode,
6370
getDocument,
64-
newMutationObserver,
6571
clearTimeoutFn as clearTimeout,
6672
setImmediateFn as setImmediate,
6773
setTimeoutFn as setTimeout,

src/wait-for-dom-change.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
newMutationObserver,
2+
getWindowFromNode,
33
getDocument,
44
setImmediate,
55
setTimeout,
@@ -31,7 +31,8 @@ function waitForDomChange({
3131
}
3232
return new Promise((resolve, reject) => {
3333
const timer = setTimeout(onTimeout, timeout)
34-
const observer = newMutationObserver(onMutation)
34+
const {MutationObserver} = getWindowFromNode(container)
35+
const observer = new MutationObserver(onMutation)
3536
runWithRealTimers(() =>
3637
observer.observe(container, mutationObserverOptions),
3738
)

src/wait.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
newMutationObserver,
2+
getWindowFromNode,
33
getDocument,
44
setImmediate,
55
setTimeout,
@@ -28,7 +28,8 @@ function wait(
2828
const overallTimeoutTimer = setTimeout(onTimeout, timeout)
2929
const intervalId = setInterval(checkCallback, interval)
3030

31-
const observer = newMutationObserver(checkCallback)
31+
const {MutationObserver} = getWindowFromNode(container)
32+
const observer = new MutationObserver(checkCallback)
3233
runWithRealTimers(() =>
3334
observer.observe(container, mutationObserverOptions),
3435
)

0 commit comments

Comments
 (0)