-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix LocalizedString common model helper (#711)
* fix(localized-string): cover helper use case * chore: add changeset * refactor(localized-string): add tests for helpers
- Loading branch information
1 parent
3c7cee3
commit 073ec7d
Showing
3 changed files
with
68 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-test-data/commons': patch | ||
--- | ||
|
||
Fix an error in the `LocalizedString.resolveGraphqlDefaultLocaleValue` helper function when an empty array is passed as a parameter. |
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,62 @@ | ||
import { LocalizedString } from '..'; | ||
import { toLocalizedField, resolveGraphqlDefaultLocaleValue } from './helpers'; | ||
import { TLocalizedStringGraphql } from './types'; | ||
|
||
describe('LocalizedString helpers', () => { | ||
describe('toLocalizedField', () => { | ||
it('should return null if no value is provided', () => { | ||
const result = toLocalizedField(null); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return a localized field if value is provided', () => { | ||
const mockValue = LocalizedString.random().en('Hello').de('Hallo'); | ||
const result = toLocalizedField(mockValue); | ||
|
||
expect(result).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
locale: 'en', | ||
value: 'Hello', | ||
__typename: 'LocalizedString', | ||
}), | ||
expect.objectContaining({ | ||
locale: 'de', | ||
value: 'Hallo', | ||
__typename: 'LocalizedString', | ||
}), | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
describe('resolveGraphqlDefaultLocaleValue', () => { | ||
it('should return null if no locales are provided', () => { | ||
const result = resolveGraphqlDefaultLocaleValue(null); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return null if locales array is empty', () => { | ||
const result = resolveGraphqlDefaultLocaleValue([]); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return the value of the default locale if it exists', () => { | ||
const mockLocales: TLocalizedStringGraphql = [ | ||
{ locale: 'en', value: 'Hello', __typename: 'LocalizedString' }, | ||
{ locale: 'de', value: 'Hallo', __typename: 'LocalizedString' }, | ||
]; | ||
const result = resolveGraphqlDefaultLocaleValue(mockLocales); | ||
expect(result).toBe('Hello'); | ||
}); | ||
|
||
it('should return the value of the first locale if default locale does not exist', () => { | ||
const mockLocales: TLocalizedStringGraphql = [ | ||
{ locale: 'fr', value: 'Bonjour', __typename: 'LocalizedString' }, | ||
{ locale: 'de', value: 'Hallo', __typename: 'LocalizedString' }, | ||
]; | ||
const result = resolveGraphqlDefaultLocaleValue(mockLocales); | ||
expect(result).toBe('Bonjour'); | ||
}); | ||
}); | ||
}); |
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