Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove mutationobserver shim #457

Merged
merged 2 commits into from
Mar 4, 2020
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
],
"dependencies": {
"@babel/runtime": "^7.8.4",
"@sheerun/mutationobserver-shim": "^0.3.2",
"@types/testing-library__dom": "^6.12.1",
"aria-query": "^4.0.2",
"dom-accessibility-api": "^0.3.0",
Expand Down
27 changes: 1 addition & 26 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
import {getDocument, newMutationObserver} from '../helpers'
import {getDocument} from '../helpers'

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: 1 addition & 19 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getWindowFromNode} from './helpers'
const eventMap = {
// Clipboard Events
copy: {
Expand Down Expand Up @@ -410,25 +411,6 @@ Object.keys(eventMap).forEach(key => {
fireEvent[key] = (node, init) => fireEvent(node, createEvent[key](node, init))
})

function getWindowFromNode(node) {
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
if (node.defaultView) {
// node is document
return node.defaultView
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
// node is a DOM node
return node.ownerDocument.defaultView
} else if (node.window) {
// node is window
return node.window
} else {
// no idea...
throw new Error(
`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`,
)
}
}

// function written after some investigation here:
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
function setNativeValue(element, value) {
Expand Down
32 changes: 19 additions & 13 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import MutationObserver from '@sheerun/mutationobserver-shim'

const globalObj = typeof window === 'undefined' ? global : window

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

function newMutationObserver(onMutation) {
const MutationObserverConstructor =
typeof window !== 'undefined' &&
typeof window.MutationObserver !== 'undefined'
? window.MutationObserver
: MutationObserver

return new MutationObserverConstructor(onMutation)
}

function getDocument() {
/* istanbul ignore if */
if (typeof window === 'undefined') {
throw new Error('Could not find default container')
}
return window.document
}
function getWindowFromNode(node) {
// istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
if (node.defaultView) {
// node is document
return node.defaultView
} else if (node.ownerDocument && node.ownerDocument.defaultView) {
// node is a DOM node
return node.ownerDocument.defaultView
} else if (node.window) {
// node is window
return node.window
} else {
// no idea...
throw new Error(
`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`,
)
}
}

export {
getWindowFromNode,
getDocument,
newMutationObserver,
clearTimeoutFn as clearTimeout,
setImmediateFn as setImmediate,
setTimeoutFn as setTimeout,
Expand Down
5 changes: 3 additions & 2 deletions src/wait-for-dom-change.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
newMutationObserver,
getWindowFromNode,
getDocument,
setImmediate,
setTimeout,
Expand Down Expand Up @@ -31,7 +31,8 @@ function waitForDomChange({
}
return new Promise((resolve, reject) => {
const timer = setTimeout(onTimeout, timeout)
const observer = newMutationObserver(onMutation)
const {MutationObserver} = getWindowFromNode(container)
const observer = new MutationObserver(onMutation)
runWithRealTimers(() =>
observer.observe(container, mutationObserverOptions),
)
Expand Down
5 changes: 3 additions & 2 deletions src/wait.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
newMutationObserver,
getWindowFromNode,
getDocument,
setImmediate,
setTimeout,
Expand Down Expand Up @@ -28,7 +28,8 @@ function wait(
const overallTimeoutTimer = setTimeout(onTimeout, timeout)
const intervalId = setInterval(checkCallback, interval)

const observer = newMutationObserver(checkCallback)
const {MutationObserver} = getWindowFromNode(container)
const observer = new MutationObserver(checkCallback)
runWithRealTimers(() =>
observer.observe(container, mutationObserverOptions),
)
Expand Down