This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1487 from Shopify/1473-i18n-merge
[@shopify/react-i18n-universal-provider] Better details merge
- Loading branch information
Showing
6 changed files
with
174 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/react-i18n-universal-provider/src/test/utilities.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import faker from 'faker'; | ||
|
||
import {combinedI18nDetails} from '../utilities'; | ||
|
||
describe('combinedI18nDetails()', () => { | ||
it('merges two details objects together', () => { | ||
const details = { | ||
locale: faker.random.locale(), | ||
country: faker.address.country(), | ||
}; | ||
const overrides = { | ||
currency: faker.finance.currencyCode(), | ||
}; | ||
|
||
const combinedDetails = combinedI18nDetails(details, overrides); | ||
|
||
expect(combinedDetails).toHaveProperty('locale'); | ||
expect(combinedDetails).toHaveProperty('country'); | ||
expect(combinedDetails).toHaveProperty('currency'); | ||
}); | ||
|
||
it('overrides fields with defined overrides', () => { | ||
const locale = faker.random.locale(); | ||
const country = faker.address.country(); | ||
const currency = faker.finance.currencyCode(); | ||
const details = { | ||
locale, | ||
country, | ||
currency, | ||
}; | ||
const overrideCurrency = faker.finance.currencyCode(); | ||
const overrides = { | ||
locale: undefined, | ||
country: undefined, | ||
currency: overrideCurrency, | ||
}; | ||
|
||
const combinedDetails = combinedI18nDetails(details, overrides); | ||
|
||
expect(combinedDetails).toHaveProperty('locale', locale); | ||
expect(combinedDetails).toHaveProperty('country', country); | ||
expect(combinedDetails).toHaveProperty('currency', overrideCurrency); | ||
}); | ||
|
||
describe('locale', () => { | ||
it('returns a details object with a locale field', () => { | ||
const locale = faker.random.locale(); | ||
const fallbackLocale = faker.random.locale(); | ||
const details = {locale}; | ||
const overrides = {locale: undefined, fallbackLocale}; | ||
|
||
expect(combinedI18nDetails(details, overrides)).toHaveProperty( | ||
'locale', | ||
locale, | ||
); | ||
}); | ||
|
||
it('favours an override locale if one is specified', () => { | ||
const locale = faker.random.locale(); | ||
const overrideLocale = faker.random.locale(); | ||
const details = {locale}; | ||
const overrides = {locale: overrideLocale}; | ||
|
||
expect(combinedI18nDetails(details, overrides)).toHaveProperty( | ||
'locale', | ||
overrideLocale, | ||
); | ||
}); | ||
|
||
it('returns a details object with a fallback locale field if one is specified', () => { | ||
const fallbackLocale = faker.random.locale(); | ||
const details = {}; | ||
const overrides = {fallbackLocale}; | ||
|
||
expect(combinedI18nDetails(details, overrides)).toHaveProperty( | ||
'locale', | ||
fallbackLocale, | ||
); | ||
}); | ||
|
||
it('returns a details object with a default locale field if none is specified', () => { | ||
const details = {}; | ||
const overrides = {}; | ||
|
||
expect(combinedI18nDetails(details, overrides)).toHaveProperty( | ||
'locale', | ||
'en', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function pruneUndefinedFields(obj = {}) { | ||
const objCopy = {...obj}; | ||
|
||
Object.keys(objCopy).forEach( | ||
key => objCopy[key] === undefined && delete objCopy[key], | ||
); | ||
|
||
return objCopy; | ||
} | ||
|
||
export function combinedI18nDetails(details, overrides) { | ||
return { | ||
locale: overrides.fallbackLocale || 'en', | ||
...pruneUndefinedFields(details), | ||
...pruneUndefinedFields(overrides), | ||
}; | ||
} |