Updating try/catch and adding tests

This commit is contained in:
Grace Park 2021-05-27 15:02:33 -07:00
Родитель 3d3788e600
Коммит d6b3d0764d
2 изменённых файлов: 21 добавлений и 5 удалений

Просмотреть файл

@ -20,10 +20,14 @@ module.exports = function detectLanguage (req, res, next) {
const firstPartOfPath = req.path.split('/')[1]
req.language = languageCodes.includes(firstPartOfPath) ? firstPartOfPath : 'en'
// Detecting browser language by user preference + value
// Detecting browser language by user preference
if (req.headers['accept-language']) {
const browserLanguage = req.headers['accept-language'].split(';')[0]
req.userLanguage = convertLanguageCode(browserLanguage)
const browserLanguage = req.headers['accept-language'].split(/\s*[,;]\s*/, 1)[0]
try {
req.userLanguage = convertLanguageCode(browserLanguage)
} catch (err) {
console.log(err)
}
}
return next()

Просмотреть файл

@ -87,12 +87,25 @@ describe('header', () => {
const $ = await getDOM('/en', headers)
expect($('.header-notifications').length).toBe(0)
})
test('renders a link to the same page in user\'s preferred language from multiple, if available', async () => {
const headers = { 'accept-language': 'ja, *;q=0.9' }
const $ = await getDOM('/en', headers)
expect($('.header-notifications.translation_notice').length).toBe(1)
expect($('.header-notifications a[href*="/ja"]').length).toBe(1)
})
test('renders a link to the same page in user\'s preferred language with weights, if available', async () => {
const headers = { 'accept-language': 'ja;q=1.0, *;q=0.9' }
const $ = await getDOM('/en', headers)
expect($('.header-notifications.translation_notice').length).toBe(1)
expect($('.header-notifications a[href*="/ja"]').length).toBe(1)
})
})
describe('mobile-only product dropdown links', () => {
test('include github and admin, and emphasize the current product', async () => {
const $ = await getDOM('/en/articles/enabling-required-status-checks')
const github = $('#homepages a.active[href="/en/github"]')
expect(github.length).toBe(1)
expect(github.text().trim()).toBe('GitHub.com')
@ -106,7 +119,6 @@ describe('header', () => {
test('point to homepages in the current page\'s language', async () => {
const $ = await getDOM('/ja/github/administering-a-repository/defining-the-mergeability-of-pull-requests')
expect($('#homepages a.active[href="/ja/github"]').length).toBe(1)
expect($(`#homepages a[href="/ja/enterprise-server@${latest}/admin"]`).length).toBe(1)
})