Skip to content

Commit fe08240

Browse files
sheerunKent C. Dodds
authored and
Kent C. Dodds
committed
chore: fix code coverage for node env changes (testing-library#126)
This fixes code coverage of testing-library#125 by extracting helper functions. There are still few istanbul ignores in code but they are not mine :)
1 parent 7be9a8b commit fe08240

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

.size-snapshot.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dist/dom-testing-library.umd.js": {
3-
"bundled": 146890,
4-
"minified": 53947,
5-
"gzipped": 15849
3+
"bundled": 146354,
4+
"minified": 53637,
5+
"gzipped": 15842
66
}
77
}

src/__tests__/helpers.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {getDocument, newMutationObserver} from '../helpers'
2+
3+
describe('getDocument', () => {
4+
if (typeof document === 'undefined') {
5+
test('throws an error if window does not exist', () => {
6+
expect(() => getDocument()).toThrowError(
7+
/Could not find default container/,
8+
)
9+
})
10+
} else {
11+
test('returns global document if exists', () => {
12+
expect(getDocument()).toBe(document)
13+
})
14+
}
15+
})
16+
17+
class DummyClass {
18+
constructor(args) {
19+
this.args = args
20+
}
21+
}
22+
23+
describe('newMutationObserver', () => {
24+
if (typeof window === 'undefined') {
25+
it('instantiates mock MutationObserver if not availble on window', () => {
26+
expect(newMutationObserver(() => {}).observe).toBeDefined()
27+
})
28+
} else {
29+
it('instantiates from global MutationObserver if available', () => {
30+
const oldMutationObserver = window.MutationObserver
31+
window.MutationObserver = DummyClass
32+
33+
try {
34+
expect(newMutationObserver('foobar').args).toEqual('foobar')
35+
} finally {
36+
window.MutationObserver = oldMutationObserver
37+
}
38+
})
39+
}
40+
})

src/helpers.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import MutationObserver from '@sheerun/mutationobserver-shim'
2+
3+
function newMutationObserver(onMutation) {
4+
const MutationObserverConstructor =
5+
typeof window !== 'undefined' &&
6+
typeof window.MutationObserver !== 'undefined'
7+
? window.MutationObserver
8+
: MutationObserver
9+
10+
return new MutationObserverConstructor(onMutation)
11+
}
12+
13+
function getDocument() {
14+
if (typeof window === 'undefined') {
15+
throw new Error('Could not find default container')
16+
}
17+
return window.document
18+
}
19+
20+
export {getDocument, newMutationObserver}

src/wait-for-dom-change.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MutationObserver from '@sheerun/mutationobserver-shim'
1+
import {newMutationObserver, getDocument} from './helpers'
22

33
function waitForDomChange({
44
container = getDocument(),
@@ -12,13 +12,7 @@ function waitForDomChange({
1212
} = {}) {
1313
return new Promise((resolve, reject) => {
1414
const timer = setTimeout(onTimeout, timeout)
15-
/* istanbul ignore next */
16-
const MutationObserverConstructor =
17-
typeof window !== 'undefined' &&
18-
typeof window.MutationObserver !== 'undefined'
19-
? window.MutationObserver
20-
: MutationObserver
21-
const observer = new MutationObserverConstructor(onMutation)
15+
const observer = newMutationObserver(onMutation)
2216
observer.observe(container, mutationObserverOptions)
2317

2418
function onDone(error, result) {
@@ -39,12 +33,4 @@ function waitForDomChange({
3933
})
4034
}
4135

42-
function getDocument() {
43-
/* istanbul ignore if */
44-
if (typeof window === 'undefined') {
45-
throw new Error('Could not find default container')
46-
}
47-
return window.document
48-
}
49-
5036
export {waitForDomChange}

src/wait-for-element.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MutationObserver from '@sheerun/mutationobserver-shim'
1+
import {newMutationObserver, getDocument} from './helpers'
22

33
function waitForElement(
44
callback,
@@ -19,13 +19,7 @@ function waitForElement(
1919
}
2020
let lastError
2121
const timer = setTimeout(onTimeout, timeout)
22-
/* istanbul ignore next */
23-
const MutationObserverConstructor =
24-
typeof window !== 'undefined' &&
25-
typeof window.MutationObserver !== 'undefined'
26-
? window.MutationObserver
27-
: MutationObserver
28-
const observer = new MutationObserverConstructor(onMutation)
22+
const observer = newMutationObserver(onMutation)
2923
observer.observe(container, mutationObserverOptions)
3024
function onDone(error, result) {
3125
clearTimeout(timer)
@@ -56,12 +50,4 @@ function waitForElement(
5650
})
5751
}
5852

59-
function getDocument() {
60-
/* istanbul ignore if */
61-
if (typeof window === 'undefined') {
62-
throw new Error('Could not find default container')
63-
}
64-
return window.document
65-
}
66-
6753
export {waitForElement}

0 commit comments

Comments
 (0)