2019-08-31 17:21:29 +08:00
|
|
|
import { JSDOM } from 'jsdom'
|
2019-02-16 21:01:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the test environment by simulating browser globals.
|
2019-03-05 05:09:52 +08:00
|
|
|
* @param {string} [locale=en] A locale to set in navigator.language
|
2019-02-16 21:01:30 +08:00
|
|
|
* @return {void}
|
|
|
|
*/
|
2019-08-31 17:21:29 +08:00
|
|
|
function setUpTestEnvironment (locale) {
|
2019-08-28 19:21:14 +08:00
|
|
|
if (!locale) {
|
|
|
|
locale = 'en'
|
|
|
|
}
|
2019-03-05 05:09:52 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
const dom = new JSDOM('...')
|
2019-08-28 19:21:14 +08:00
|
|
|
global.window = dom.window
|
|
|
|
global.document = dom.window.document
|
|
|
|
global.navigator = dom.window.navigator
|
2019-03-05 05:09:52 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
// JSDom has no setter defined for navigator.language, so defineProperty is necessary in order to override it
|
|
|
|
Object.defineProperty(navigator, 'language', { value: locale })
|
2019-08-31 17:21:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
setUpTestEnvironment()
|