|
| 1 | +const { JSDOM } = require('jsdom'); |
| 2 | +const { TextDecoder, TextEncoder } = require('util'); |
| 3 | + |
| 4 | +const jsdom = new JSDOM(`<div id="main"></div>`, { |
| 5 | + // https://github.com/jsdom/jsdom#basic-options |
| 6 | + // 禁用掉 resources: usable, 采用 jsdom 默认策略不加载 subresources |
| 7 | + // 避免测试用例加载 external subresource, 如 iconfont 的 css 挂掉 |
| 8 | + // resources: 'usable', |
| 9 | + runScripts: 'dangerously', |
| 10 | + url: 'http://localhost/?id=1', |
| 11 | +}); |
| 12 | +global.document = jsdom.window.document; |
| 13 | +let text = ''; |
| 14 | +global.navigator = Object.assign(jsdom.window.navigator, { |
| 15 | + clipboard: { |
| 16 | + writeText(value) { |
| 17 | + text = value; |
| 18 | + }, |
| 19 | + readText() { |
| 20 | + return text; |
| 21 | + }, |
| 22 | + }, |
| 23 | +}); |
| 24 | +global.Element = jsdom.window.Element; |
| 25 | +global.HTMLDivElement = jsdom.window.HTMLDivElement; |
| 26 | +global.fetch = jsdom.window.fetch; |
| 27 | +global.location = jsdom.window.location; |
| 28 | +global.getComputedStyle = jsdom.window.getComputedStyle; |
| 29 | +global.window = jsdom.window; |
| 30 | +global.DOMParser = jsdom.window.DOMParser; |
| 31 | +global.HTMLDivElement = jsdom.window.HTMLDivElement; |
| 32 | +global.MutationObserver = jsdom.window.MutationObserver; |
| 33 | +global.requestAnimationFrame = (fn) => setTimeout(fn, 16); |
| 34 | +jsdom.window.requestAnimationFrame = (fn) => setTimeout(fn, 16); |
| 35 | +jsdom.window.cancelAnimationFrame = () => {}; |
| 36 | +global.document.queryCommandSupported = () => {}; |
| 37 | +global.document.execCommand = () => {}; |
| 38 | +global.HTMLElement = jsdom.window.HTMLElement; |
| 39 | +global.self = global; |
| 40 | +global.TextEncoder = TextEncoder; |
| 41 | +global.TextDecoder = TextDecoder; |
| 42 | + |
| 43 | +class MockLocalStorage { |
| 44 | + constructor() { |
| 45 | + this.store = {}; |
| 46 | + } |
| 47 | + |
| 48 | + clear() { |
| 49 | + this.store = {}; |
| 50 | + } |
| 51 | + |
| 52 | + getItem(key) { |
| 53 | + return this.store[key] || null; |
| 54 | + } |
| 55 | + |
| 56 | + setItem(key, value) { |
| 57 | + this.store[key] = value.toString(); |
| 58 | + } |
| 59 | + |
| 60 | + removeItem(key) { |
| 61 | + delete this.store[key]; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +global.localStorage = new MockLocalStorage(); |
0 commit comments