Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 1a1df51

Browse files
committed
test: don't run 'Intl.Locale supported' tests on Node versions that do not support Intl.Locale
Produces false errors (as tests expect the test runner to support Intl.Locale) with Node 10.
1 parent 6e3de3a commit 1a1df51

7 files changed

Lines changed: 202 additions & 188 deletions

File tree

packages/preferred-locale/src/index.spec.browser.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@ import preferredLocale from './'
22
import * as isLocaleSupported from './utils/isLocaleSupported'
33

44
describe('preferredLocale', () => {
5-
describe('with Intl.Locale supported', () => {
6-
it('handles region-less locales', () => {
7-
expect.assertions(1)
8-
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en', 'en-US' ])
9-
const translations = [ 'fr-FR', 'id-ID', 'en-US' ]
10-
expect(preferredLocale(translations, 'en-US')).toBe('en-US')
11-
})
5+
if (Intl && 'Locale' in Intl) {
6+
describe('with Intl.Locale supported', () => {
7+
it('handles region-less locales', () => {
8+
expect.assertions(1)
9+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en', 'en-US' ])
10+
const translations = [ 'fr-FR', 'id-ID', 'en-US' ]
11+
expect(preferredLocale(translations, 'en-US')).toBe('en-US')
12+
})
1213

13-
it('returns \'en-us\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca not translated)', () => {
14-
expect.assertions(1)
15-
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
16-
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
17-
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
18-
})
14+
it('returns \'en-us\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca not translated)', () => {
15+
expect.assertions(1)
16+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
17+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
18+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
19+
})
1920

20-
it('returns \'en-ca\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca translated)', () => {
21-
expect.assertions(1)
22-
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
23-
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
24-
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
21+
it('returns \'en-ca\' with \'en-CA, en-US and en\' and \'en-us\' fallback (en-ca translated)', () => {
22+
expect.assertions(1)
23+
jest.spyOn(navigator, 'languages', 'get').mockReturnValue([ 'en-CA', 'en-US', 'en' ])
24+
const translations = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
25+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true })).toBe('en-ca')
26+
})
2527
})
26-
})
28+
}
2729

2830
describe('without Intl.Locale supported', () => {
2931
beforeEach(() => {

packages/preferred-locale/src/index.spec.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,51 @@ import preferredLocale from './'
22
import * as isLocaleSupported from './utils/isLocaleSupported'
33

44
describe('preferredLocale', () => {
5-
describe('with Intl.Locale supported', () => {
6-
it('returns the fallback locale if no matches found', () => {
7-
expect.assertions(1)
8-
const translations = [ 'fr-FR', 'nl-NL', 'id-ID', 'en-US' ]
9-
expect(preferredLocale(translations, 'en-US'))
10-
.toBe('en-US')
11-
})
5+
if (Intl && 'Locale' in Intl) {
6+
describe('with Intl.Locale supported', () => {
7+
it('returns the fallback locale if no matches found', () => {
8+
expect.assertions(1)
9+
const translations = [ 'fr-FR', 'nl-NL', 'id-ID', 'en-US' ]
10+
expect(preferredLocale(translations, 'en-US'))
11+
.toBe('en-US')
12+
})
1213

13-
it('returns the fallback locale if no matches found (regionLowerCase)', () => {
14-
expect.assertions(1)
15-
const translations = [ 'fr-fr', 'nl-nl', 'id-id', 'en-us' ]
16-
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true }))
17-
.toBe('en-us')
18-
})
14+
it('returns the fallback locale if no matches found (regionLowerCase)', () => {
15+
expect.assertions(1)
16+
const translations = [ 'fr-fr', 'nl-nl', 'id-id', 'en-us' ]
17+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true }))
18+
.toBe('en-us')
19+
})
1920

20-
it('returns the fallback locale if no matches found (language only)', () => {
21-
expect.assertions(1)
22-
const translations = [ 'fr', 'nl', 'id', 'en' ]
23-
expect(preferredLocale(translations, 'en', { languageOnly: true }))
24-
.toBe('en')
25-
})
21+
it('returns the fallback locale if no matches found (language only)', () => {
22+
expect.assertions(1)
23+
const translations = [ 'fr', 'nl', 'id', 'en' ]
24+
expect(preferredLocale(translations, 'en', { languageOnly: true }))
25+
.toBe('en')
26+
})
2627

27-
it('returns the the preferred locale if match found', () => {
28-
expect.assertions(1)
29-
const translations = [ 'fr-FR', 'ja-JP', 'id-ID', 'en-US' ]
30-
expect(preferredLocale(translations, 'en-US'))
31-
.toBe('ja-JP')
32-
})
28+
it('returns the the preferred locale if match found', () => {
29+
expect.assertions(1)
30+
const translations = [ 'fr-FR', 'ja-JP', 'id-ID', 'en-US' ]
31+
expect(preferredLocale(translations, 'en-US'))
32+
.toBe('ja-JP')
33+
})
3334

34-
it('returns the the preferred locale if match found (regionLowerCase)', () => {
35-
expect.assertions(1)
36-
const translations = [ 'fr-fr', 'ja-jp', 'id-id', 'en-us' ]
37-
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true }))
38-
.toBe('ja-jp')
39-
})
35+
it('returns the the preferred locale if match found (regionLowerCase)', () => {
36+
expect.assertions(1)
37+
const translations = [ 'fr-fr', 'ja-jp', 'id-id', 'en-us' ]
38+
expect(preferredLocale(translations, 'en-us', { regionLowerCase: true }))
39+
.toBe('ja-jp')
40+
})
4041

41-
it('returns the the preferred locale if match found (language only)', () => {
42-
expect.assertions(1)
43-
const translations = [ 'fr', 'ja', 'id', 'en' ]
44-
expect(preferredLocale(translations, 'en', { languageOnly: true }))
45-
.toBe('ja')
42+
it('returns the the preferred locale if match found (language only)', () => {
43+
expect.assertions(1)
44+
const translations = [ 'fr', 'ja', 'id', 'en' ]
45+
expect(preferredLocale(translations, 'en', { languageOnly: true }))
46+
.toBe('ja')
47+
})
4648
})
47-
})
49+
}
4850

4951
describe('without Intl.Locale supported', () => {
5052
beforeEach(() => {

packages/preferred-locale/src/utils/availableLocales/index.spec.browser.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@ import { availableLocales } from './'
22
import * as isLocaleSupported from '../isLocaleSupported'
33

44
describe('availableLocales', () => {
5-
describe('with Intl.Locale supported', () => {
6-
it('returns \'en-us\' when \'en-us\' is translated but \'en-ca\' is not', () => {
7-
expect.assertions(1)
8-
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
9-
const browser = [
10-
{ locale: 'en-ca', priority: 0 },
11-
{ locale: 'en-us', priority: 1 }
12-
]
13-
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
14-
{ locale: 'en-us', priority: 0 }
15-
])
16-
})
5+
if (Intl && 'Locale' in Intl) {
6+
describe('with Intl.Locale supported', () => {
7+
it('returns \'en-us\' when \'en-us\' is translated but \'en-ca\' is not', () => {
8+
expect.assertions(1)
9+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
10+
const browser = [
11+
{ locale: 'en-ca', priority: 0 },
12+
{ locale: 'en-us', priority: 1 }
13+
]
14+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
15+
{ locale: 'en-us', priority: 0 }
16+
])
17+
})
1718

18-
it('returns \'en-ca\' when \'en-ca\' and \'en-us\' are translated', () => {
19-
expect.assertions(1)
20-
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
21-
const browser = [
22-
{ locale: 'en-ca', priority: 0 },
23-
{ locale: 'en-us', priority: 1 }
24-
]
25-
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
26-
{ locale: 'en-ca', priority: 0 },
27-
{ locale: 'en-us', priority: 1 }
28-
])
19+
it('returns \'en-ca\' when \'en-ca\' and \'en-us\' are translated', () => {
20+
expect.assertions(1)
21+
const translated = [ 'ar-001', 'ca-es', 'de-de', 'en-gb', 'en-us', 'en-ca', 'eo-uy', 'es-ar', 'es-es', 'es-mx', 'fr-ca', 'fr-fr', 'hu-hu', 'id-id', 'it-it', 'nl-nl', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'sv-se', 'tl-ph', 'tr-tr' ]
22+
const browser = [
23+
{ locale: 'en-ca', priority: 0 },
24+
{ locale: 'en-us', priority: 1 }
25+
]
26+
expect(availableLocales(browser, translated, { regionLowerCase: true })).toStrictEqual([
27+
{ locale: 'en-ca', priority: 0 },
28+
{ locale: 'en-us', priority: 1 }
29+
])
30+
})
2931
})
30-
})
32+
}
3133

3234
describe('without Intl.Locale supported', () => {
3335
beforeEach(() => {

packages/preferred-locale/src/utils/isLanguageAvailable/index.spec.js

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,81 @@ import { isLanguageAvailable } from './'
22
import * as isLocaleSupported from '../isLocaleSupported'
33

44
describe('isLanguageAvailable', () => {
5-
describe('with Intl.Locale supported', () => {
6-
it('returns true if language is available but region wasn\'t', () => {
7-
expect.assertions(1)
8-
9-
const translated = [ 'en-US' ]
10-
const browser = 'en-GB'
11-
const index = 0
12-
const array = [ { locale: 'en-US', priority: 0 } ]
13-
14-
expect(isLanguageAvailable(browser, translated, index, array))
15-
.toBe(true)
16-
})
17-
18-
it('returns true if language is available but region wasn\'t (lowercase region inputs)', () => {
19-
expect.assertions(1)
20-
21-
const translated = [ 'en-us' ]
22-
const browser = 'en-gb'
23-
const index = 0
24-
const array = [ { locale: 'en-us', priority: 0 } ]
25-
26-
expect(isLanguageAvailable(browser, translated, index, array))
27-
.toBe(true)
28-
})
29-
30-
it('returns true if language is available (language only inputs)', () => {
31-
expect.assertions(1)
32-
33-
const translated = [ 'en' ]
34-
const browser = 'en-gb'
35-
const index = 0
36-
const array = [ { locale: 'en', priority: 0 } ]
37-
38-
expect(isLanguageAvailable(browser, translated, index, array))
39-
.toBe(true)
5+
if (Intl && 'Locale' in Intl) {
6+
describe('with Intl.Locale supported', () => {
7+
it('returns true if language is available but region wasn\'t', () => {
8+
expect.assertions(1)
9+
10+
const translated = [ 'en-US' ]
11+
const browser = 'en-GB'
12+
const index = 0
13+
const array = [ { locale: 'en-US', priority: 0 } ]
14+
15+
expect(isLanguageAvailable(browser, translated, index, array))
16+
.toBe(true)
17+
})
18+
19+
it('returns true if language is available but region wasn\'t (lowercase region inputs)', () => {
20+
expect.assertions(1)
21+
22+
const translated = [ 'en-us' ]
23+
const browser = 'en-gb'
24+
const index = 0
25+
const array = [ { locale: 'en-us', priority: 0 } ]
26+
27+
expect(isLanguageAvailable(browser, translated, index, array))
28+
.toBe(true)
29+
})
30+
31+
it('returns true if language is available (language only inputs)', () => {
32+
expect.assertions(1)
33+
34+
const translated = [ 'en' ]
35+
const browser = 'en-gb'
36+
const index = 0
37+
const array = [ { locale: 'en', priority: 0 } ]
38+
39+
expect(isLanguageAvailable(browser, translated, index, array))
40+
.toBe(true)
41+
})
42+
43+
it('returns false if language and region aren\'t available', () => {
44+
expect.assertions(1)
45+
46+
const translated = [ 'en-US' ]
47+
const browser = 'fr-FR'
48+
const index = 0
49+
const array = [ { locale: 'en-US', priority: 0 } ]
50+
51+
expect(isLanguageAvailable(browser, translated, index, array))
52+
.toBe(false)
53+
})
54+
55+
it('returns false if language and region aren\'t available (lowercase region inputs)', () => {
56+
expect.assertions(1)
57+
58+
const translated = [ 'en-us' ]
59+
const browser = 'fr-fr'
60+
const index = 0
61+
const array = [ { locale: 'en-us', priority: 0 } ]
62+
63+
expect(isLanguageAvailable(browser, translated, index, array))
64+
.toBe(false)
65+
})
66+
67+
it('returns false if language isn\'t available (language only inputs)', () => {
68+
expect.assertions(1)
69+
70+
const translated = [ 'en' ]
71+
const browser = 'fr-fr'
72+
const index = 0
73+
const array = [ { locale: 'en', priority: 0 } ]
74+
75+
expect(isLanguageAvailable(browser, translated, index, array))
76+
.toBe(false)
77+
})
4078
})
41-
42-
it('returns false if language and region aren\'t available', () => {
43-
expect.assertions(1)
44-
45-
const translated = [ 'en-US' ]
46-
const browser = 'fr-FR'
47-
const index = 0
48-
const array = [ { locale: 'en-US', priority: 0 } ]
49-
50-
expect(isLanguageAvailable(browser, translated, index, array))
51-
.toBe(false)
52-
})
53-
54-
it('returns false if language and region aren\'t available (lowercase region inputs)', () => {
55-
expect.assertions(1)
56-
57-
const translated = [ 'en-us' ]
58-
const browser = 'fr-fr'
59-
const index = 0
60-
const array = [ { locale: 'en-us', priority: 0 } ]
61-
62-
expect(isLanguageAvailable(browser, translated, index, array))
63-
.toBe(false)
64-
})
65-
66-
it('returns false if language isn\'t available (language only inputs)', () => {
67-
expect.assertions(1)
68-
69-
const translated = [ 'en' ]
70-
const browser = 'fr-fr'
71-
const index = 0
72-
const array = [ { locale: 'en', priority: 0 } ]
73-
74-
expect(isLanguageAvailable(browser, translated, index, array))
75-
.toBe(false)
76-
})
77-
})
79+
}
7880

7981
describe('without Intl.Locale supported', () => {
8082
beforeEach(() => {

packages/preferred-locale/src/utils/isLocaleSupported/index.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ describe('isLocaleSupported', () => {
1515
Intl = storedIntl
1616
})
1717

18-
it('returns true if Intl.Locale is spec compliant', () => {
19-
expect.assertions(1)
20-
expect(isLocaleSupported()).toBe(true)
21-
})
18+
if (Intl && 'Locale' in Intl) {
19+
it('returns true if Intl.Locale is spec compliant', () => {
20+
expect.assertions(1)
21+
expect(isLocaleSupported()).toBe(true)
22+
})
23+
}
2224
})

packages/preferred-locale/src/utils/unifyUserLocales/index.spec.browser.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import { unifyUserLocales } from './'
22
import * as isLocaleSupported from '../isLocaleSupported'
33

44
describe('preferredLocale', () => {
5-
describe('with Intl.Locale supported', () => {
6-
it('returns \'en-ca and en-us\' with \'en-CA, en-US, en and \'en-us\'', () => {
7-
expect.assertions(1)
8-
expect(unifyUserLocales([ 'en-CA', 'en-US', 'en', 'en-us' ], { regionLowerCase: true })).toStrictEqual([
9-
{ locale: 'en-ca', priority: 0 },
10-
{ locale: 'en-us', priority: 1 }
11-
])
5+
if (Intl && 'Locale' in Intl) {
6+
describe('with Intl.Locale supported', () => {
7+
it('returns \'en-ca and en-us\' with \'en-CA, en-US, en and \'en-us\'', () => {
8+
expect.assertions(1)
9+
expect(unifyUserLocales([ 'en-CA', 'en-US', 'en', 'en-us' ], { regionLowerCase: true })).toStrictEqual([
10+
{ locale: 'en-ca', priority: 0 },
11+
{ locale: 'en-us', priority: 1 }
12+
])
13+
})
1214
})
13-
})
15+
}
1416

1517
describe('without Intl.Locale supported', () => {
1618
beforeEach(() => {

0 commit comments

Comments
 (0)