forked from geelen/react-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot.js
42 lines (40 loc) · 1.32 KB
/
snapshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Wraps a jsdom call and returns the full page */
import jsdom from 'jsdom'
export default (protocol, host, path, delay) => {
return new Promise((resolve, reject) => {
let reactSnapshotRenderCalled = false
const url = `${protocol}//${host}${path}`
jsdom.env({
url,
headers: { Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" },
resourceLoader(resource, callback) {
if (resource.url.host === host) {
resource.defaultFetch(callback);
} else {
callback()
}
},
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
SkipExternalResources: false
},
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
created: (err, window) => {
if (err) return reject(err)
if (!window) return reject(`Looks like no page exists at ${url}`)
window.reactSnapshotRender = () => {
reactSnapshotRenderCalled = true
setTimeout(() => {
resolve(window)
}, delay)
}
},
done: (err, window) => {
if (!reactSnapshotRenderCalled) {
reject("'render' from react-snapshot was never called. Did you replace the call to ReactDOM.render()?")
}
}
})
})
}