|
1 |
| -import MutationObserver from '@sheerun/mutationobserver-shim' |
2 |
| - |
3 | 1 | const globalObj = typeof window === 'undefined' ? global : window
|
4 | 2 |
|
5 | 3 | // 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(
|
41 | 39 | getTimeFunctions,
|
42 | 40 | )
|
43 | 41 |
|
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 |
| - |
54 | 42 | function getDocument() {
|
55 | 43 | /* istanbul ignore if */
|
56 | 44 | if (typeof window === 'undefined') {
|
57 | 45 | throw new Error('Could not find default container')
|
58 | 46 | }
|
59 | 47 | return window.document
|
60 | 48 | }
|
| 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 | +} |
61 | 67 |
|
62 | 68 | export {
|
| 69 | + getWindowFromNode, |
63 | 70 | getDocument,
|
64 |
| - newMutationObserver, |
65 | 71 | clearTimeoutFn as clearTimeout,
|
66 | 72 | setImmediateFn as setImmediate,
|
67 | 73 | setTimeoutFn as setTimeout,
|
|
0 commit comments