Provide Navigator and allow locale override in test environment setup

This commit is contained in:
Adam Vigneaux 2019-03-04 16:09:52 -05:00
parent 8fc8e50bf1
commit 03d019a554
No known key found for this signature in database
GPG Key ID: 6DE177C0426B2E80
1 changed files with 10 additions and 1 deletions

View File

@ -2,10 +2,19 @@ var JSDOM = require('jsdom').JSDOM;
/**
* Set up the test environment by simulating browser globals.
* @param {string} [locale=en] A locale to set in navigator.language
* @return {void}
*/
module.exports = function setUpTestEnvironment() {
module.exports = function setUpTestEnvironment(locale) {
if (!locale) {
locale = 'en'
}
var dom = new JSDOM('...');
global.window = dom.window;
global.document = dom.window.document;
global.navigator = dom.window.navigator;
// JSDom has no setter defined for navigator.language, so defineProperty is necessary in order to override it
Object.defineProperty(navigator, 'language', {value: locale})
};