Skip to content

Commit 53ec1bf

Browse files
committed
feat: remove mutationobserver shim
Closes #413 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 5aa6386 commit 53ec1bf

File tree

6 files changed

+28
-63
lines changed

6 files changed

+28
-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

+20-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,16 +39,6 @@ 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') {
@@ -59,9 +47,28 @@ function getDocument() {
5947
return window.document
6048
}
6149

50+
function getWindowFromNode(node) {
51+
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
52+
if (node.defaultView) {
53+
// node is document
54+
return node.defaultView
55+
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
56+
// node is a DOM node
57+
return node.ownerDocument.defaultView
58+
} else if (node.window) {
59+
// node is window
60+
return node.window
61+
} else {
62+
// no idea...
63+
throw new Error(
64+
`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`,
65+
)
66+
}
67+
}
68+
6269
export {
70+
getWindowFromNode,
6371
getDocument,
64-
newMutationObserver,
6572
clearTimeoutFn as clearTimeout,
6673
setImmediateFn as setImmediate,
6774
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)