我有'react intl'并尝试用酶模拟window.navigator.language,该怎么做。我还使用js-dom
,其中global.navigator = window.navigator
。 beforeEach
language and languages
intl
import enLocaleData from './en.json'
import nlLocaleData from './nl.json'
const localeData = {en: enLocaleData, nl: nlLocaleData}
addLocaleData([...en, ...nl])
// Split locales with a region code
const language = window.navigator.languages && navigator.languages[0]
|| window.navigator.language - I can not coverage this case ?
|| window.navigator.userLanguage
// locale without region code, fallback to 'en'
const languageWithoutRegionCode = language.toLowerCase().split(/[_-]+/)[0]
const messages = localeData[languageWithoutRegionCode] || localeData[language] || localeData.en
export default {language, messages}
测试
import intl from '../../../../src/components/loginPage/intl'
const language = 'en'
const languages = ['en', 'nl']
const localeData = {
en: {'name': 'Username',
'password': 'Password'},
nl: {'name': 'Username2',
'password': 'Password2'},
}
describe.only('Intl', () => {
beforeEach(() => {
window.navigator.language = language
window.navigator.languages = languages
})
it('should return correct language', () => {
console.log(intl.language)
})
})
但是导航器语言在控制台中仍未定义。