Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Benedetti committed Jan 24, 2024
1 parent f494c08 commit 9250a3d
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 367 deletions.
51 changes: 41 additions & 10 deletions src/base/__tests__/bk-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,62 @@ describe('bk-base tests', () => {
const base = new BkBase()
expect(base.defaultLocale).toBeUndefined()
expect(base.customLocale).toBeUndefined()
expect(base._locale).toBeUndefined()
expect(base.locale).toBeUndefined()
})

it('should localize', async () => {
const base = new BkBase()
base.customLocale = {
en: {title: 'title'},
it: {title: 'titolo', subtitle: 'sottotitolo'}
title: {en: 'Title', it: 'Titolo'},
subtitle: {en: 'Subtitle'}
}
expect(base.locale).toStrictEqual({title: 'title'})
expect(base.locale).toStrictEqual({title: 'Title', subtitle: 'Subtitle'})
})

it('should localize with default', () => {
const base = new BkBase()
base.defaultLocale = {title: 'title', subtitle: 'subtitle'}
base.customLocale = {en: {title: 'title2'}, it: {title: 'titolo'}}
expect(base.locale).toStrictEqual({title: 'title2', subtitle: 'subtitle'})
base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'}
base.customLocale = {
title: {en: 'Title-custom', it: 'Titolo-custom'},
subtitle: {it: 'Sottotitolo-custom'}
}
expect(base.locale).toStrictEqual({title: 'Title-custom', subtitle: 'Subtitle'})
})

it('should initialize localize', () => {
const base = new BkBase()
base.defaultLocale = {title: 'title', subtitle: 'subtitle'}
base.locale = {title: 'title2'}
expect(base.locale).toStrictEqual({title: 'title2'})
base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'}
base.locale = {title: 'Title2'}
expect(base.locale).toStrictEqual({title: 'Title2'})
})

it('should localize (it)', async () => {
const {navigator} = window
Object.defineProperty(window, 'navigator', {writable: true, value: {language: 'it'}})

const base = new BkBase()
base.customLocale = {
title: {en: 'Title', it: 'Titolo'},
subtitle: {en: 'Subtitle'}
}
expect(base.locale).toStrictEqual({title: 'Titolo', subtitle: 'Subtitle'})

Object.defineProperty(window, 'navigator', {writable: true, value: navigator})
})

it('should localize with default (it)', () => {
const {navigator} = window
Object.defineProperty(window, 'navigator', {writable: true, value: {language: 'it'}})

const base = new BkBase()
base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'}
base.customLocale = {
title: {en: 'Title-custom'},
subtitle: {it: 'Sottotitolo-custom'}
}
expect(base.locale).toStrictEqual({title: 'Title-custom', subtitle: 'Sottotitolo-custom'})

Object.defineProperty(window, 'navigator', {writable: true, value: navigator})
})
})
})
285 changes: 76 additions & 209 deletions src/base/__tests__/localized-components.tets.ts
Original file line number Diff line number Diff line change
@@ -1,238 +1,105 @@
import {Labels, localizeObj, mergeLocale} from '../localized-components'
import {mergeLabels, solveLocale} from '../localized-components'

describe('localized-components tests - mergeLocale', () => {
const defaultLocale = {
describe('localized-components tests - solveLocale', () => {
const locale = {
title: {
en: 'title-en',
it: 'title-it',
es: 'title-es',
},
subtitle: {
subtitle: {
en: 'subtitle-en',
it: 'subtitle-it',
es: 'subtitle-es',
},
badge: {
en: 'badge-en',
it: 'badge-it',
es: 'badge-es',
},
}
}
const en = {
title: 'title-en',
subtitle: {
subtitle: 'subtitle-en',
badge: 'badge-en'
badge: 'badge-en',
}
}
const locale = {
es: {
title: 'title-es',
subtitle: {
subtitle: 'subtitle-es',
badge: 'badge-es'
}
},
it: {
title: 'title-it',
subtitle: {
subtitle: 'subtitle-it'
}
},
en: {
title: 'title-en-2',
subtitle: {
subtitle: 'subtitle-en-2',
},
footer: {
name: 'footer-en-2'
}
const it_ = {
title: 'title-it',
subtitle: {
subtitle: 'subtitle-it',
badge: 'badge-it',
}
}
const merged = {
es: {
title: 'title-es',
subtitle: {
subtitle: 'subtitle-es',
badge: 'badge-es'
}
},
it: {
title: 'title-it',
subtitle: {
subtitle: 'subtitle-it',
badge: 'badge-en'
}
},
en: {
title: 'title-en-2',
subtitle: {
subtitle: 'subtitle-en-2',
badge: 'badge-en'
},
footer: {
name: 'footer-en-2'
}
const es = {
title: 'title-es',
subtitle: {
subtitle: 'subtitle-es',
badge: 'badge-es',
}
}

it.each([
[defaultLocale, locale.en, merged.en],
[defaultLocale, locale.it, merged.it],
[defaultLocale, locale.es, merged.es],
[defaultLocale, undefined, defaultLocale],
[undefined, locale.en, locale.en],
[undefined, locale.it, locale.it],
[undefined, locale.es, locale.es],
[undefined,undefined, undefined],
])('should merge labels', (defaultLocale, locale, expected) => {
const merged = mergeLocale<Labels>(locale, defaultLocale)
expect(merged).toStrictEqual(expected)
[locale, 'en', en],
[locale, 'it', it_],
[locale, 'es', es],
[locale, 'fr', en],
[locale, undefined, en],
])('should solve localized texts', (locale, lang, expected) => {
expect(solveLocale(locale, lang)).toStrictEqual(expected)
})

})

describe('localized-components tests - mergeLocale with non primitive types', () => {
const defaultLocale = {
today: 'Today-default',
yesterday: 'Yesterday',
monthBeforeYear: true,
decimals: 2
}
const locale = {
en: {
today: 'Today',
tomorrow: 'Tomorrow',
rangePlaceholder: ['Start date', 'End date'],
monthBeforeYear: false,
decimals: 'None'
},
it: {
today: 'Oggi',
tomorrow: 'Domani',
rangePlaceholder: ['Inizio', 'Fine'],
monthBeforeYear: true,
decimals: 1
}
}
const expected = {
en: {
today: 'Today',
yesterday: 'Yesterday',
tomorrow: 'Tomorrow',
monthBeforeYear: false,
rangePlaceholder: ['Start date', 'End date'],
decimals: 'None'
},
it: {
today: 'Oggi',
tomorrow: 'Domani',
yesterday: 'Yesterday',
rangePlaceholder: ['Inizio', 'Fine'],
monthBeforeYear: true,
decimals: 1
}
}
it.each([
[defaultLocale, locale.en, expected.en],
[defaultLocale, locale.it, expected.it],
])('should merge labels with non-string primitive types', (defaultLocale, locale, expected) => {
const merged = mergeLocale<Record<string, unknown>>(locale, defaultLocale)
expect(merged).toStrictEqual(expected)
describe('localized-components tests - solveLocale edge cases', () => {
it('should solve localized texts', () => {
const locale = {name: {en: 'Name'}, badge: {id: 0, monthBefore: true, range: ['start', 'end'], text: {en: 'Text'}}}
const expected = {name: 'Name', badge: {id: 0, monthBefore: true, range: ['start', 'end'], text: 'Text'}}

// @ts-expect-error force unorthodox locale
expect(solveLocale(locale)).toStrictEqual(expected)
})

it('localized-components tests - solveLocale default language kicks in', () => {
const {navigator} = window
Object.defineProperty(window, 'navigator', {writable: true, value: {language: undefined}})
expect(solveLocale({name: {en: 'Name', it: 'Nome'}})).toStrictEqual({name: 'Name'})
Object.defineProperty(window, 'navigator', {writable: true, value: navigator})
})
})

describe('localized-components tests - mergeLocale edge cases', () => {
describe('localized-components tests - mergeLables', () => {
it.each([
[
{title: 'Default'},
{footer: 'Footer'},
{title: 'Default', footer: 'Footer'}
],
[
{footer: 'Footer'},
{footer: {name: 'Footer'}},
{footer: {name: 'Footer'}}
],
[
{footer: {name: 'Default'}},
{footer: {name: 'Footer'}},
{footer: {name: 'Footer'}}
],
[
{footer: {name: 'Footer'}},
{footer: {badge: 'Badge'}},
{footer: {name: 'Footer', badge: 'Badge'}}
{title: 'Title', subtitle: {name: 'Subtitle-new'}},
{subtitle: {name: 'Subtitle'}},
{title: 'Title', subtitle: {name: 'Subtitle-new'}}
],
[
{footer: {}},
{footer: {name: 'Footer'}},
{footer: {name: 'Footer'}}
{subtitle: {name: 'Subtitle-new'}},
{title: 'Title', subtitle: {name: 'Subtitle'}},
{title: 'Title', subtitle: {name: 'Subtitle-new'}}
],
[
{footer: {name: 'Footer'}},
{footer: {}},
{footer: {name: 'Footer'}}
{title: 'Title', subtitle: {name: 'Subtitle-new'}},
{subtitle: 'Subtitle'},
{title: 'Title', subtitle: {name: 'Subtitle-new'}}
],
[
{footer: {}},
{footer: {}},
{footer: {}}
{title: 'Title', subtitle: {name: 'Subtitle-new'}},
{title: 'Title', subtitle: {badge: 'Badge'}},
{title: 'Title', subtitle: {name: 'Subtitle-new', badge: 'Badge'}}
],
[
{footer: undefined},
{footer: {name: 'Footer'}},
{footer: {name: 'Footer'}}
],
[
{footer: {name: 'Footer'}},
{footer: undefined},
{footer: {name: 'Footer'}}
],
[
{footer: {name: undefined}},
{footer: {name: undefined}},
{footer: {name: undefined}}
],
[
{footer: null},
{footer: {name: 'Footer'}},
{footer: {name: 'Footer'}}
],
[
{footer: {name: 'Footer'}},
{footer: null},
{footer: null}
],
[
{footer: {name: null}},
{footer: {name: undefined}},
{footer: {name: null}}
],
[
{footer: {name: undefined}},
{footer: {name: null}},
{footer: {name: null}}
],
[
{footer: {name: 'Name'}},
{footer: {name: false}},
{footer: {name: false}}
]

])('should merge labels with non-string primitive types', (defaultLocale, locale, expected) => {
const merged = mergeLocale<Record<string, unknown>>(locale, defaultLocale)
expect(merged).toStrictEqual(expected)
[{title: 'Title', subtitle: 'Subtitle-new'}, {subtitle: 'Subtitle'}, {title: 'Title', subtitle: 'Subtitle-new'}],
[{title: 'Title'}, {subtitle: 'Subtitle'}, {title: 'Title', subtitle: 'Subtitle'}],
[{}, {title: 'Title'}, {title: 'Title'}],
[{title: 'Title'}, {}, {title: 'Title'}],
[{title: 'Title'}, undefined, {title: 'Title'}],
[undefined, {title: 'Title'}, {title: 'Title'}],
[undefined, undefined, undefined],
])('should merge labels', (labels, defaultLabels, expected) => {
expect(mergeLabels(labels, defaultLabels)).toStrictEqual(expected)
})
})

describe('localized-components tests - localizeObject', () => {
it.each([
[{en: {title: 'Title'}}, {title: 'Title'}],
[{en: {title: 'Title'}, it: {title: 'Titolo'}}, {title: 'Title'}],
[{it: {title: 'Titolo'}}, undefined],
[{}, undefined],
[undefined, undefined],
])('should localize object', (object, expected) => {
expect(localizeObj(object)).toStrictEqual(expected)
})

it.each([
[{en: {title: 'Title'}}, {title: 'Title'}],
[{en: {title: 'Title'}, it: {title: 'Titolo'}}, {title: 'Titolo'}],
[{it: {title: 'Titolo'}}, {title: 'Titolo'}],
[{es: {title: 'Titulo'}}, undefined],
[{}, undefined],
[undefined, undefined],
])('should localize object (it)', (object, expected) => {
const {navigator} = window
Object.defineProperty(window, 'navigator', {writable: true, value: {language: 'it'}})

expect(localizeObj(object)).toStrictEqual(expected)

Object.defineProperty(window, 'navigator', {writable: true, value: navigator})
})
})
Loading

0 comments on commit 9250a3d

Please sign in to comment.