Skip to content

Commit

Permalink
Fix LocalizedString common model helper (#711)
Browse files Browse the repository at this point in the history
* fix(localized-string): cover helper use case

* chore: add changeset

* refactor(localized-string): add tests for helpers
  • Loading branch information
CarlosCortizasCT authored Nov 8, 2024
1 parent 3c7cee3 commit 073ec7d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-hats-joke.md
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.
62 changes: 62 additions & 0 deletions models/commons/src/localized-string/helpers.spec.ts
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');
});
});
});
2 changes: 1 addition & 1 deletion models/commons/src/localized-string/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DEFAULT_LOCALE = 'en';
const resolveGraphqlDefaultLocaleValue = (
allLocales: TLocalizedStringGraphql | null
) => {
if (!allLocales) {
if (!allLocales || allLocales.length < 1) {
return null;
}
const defaultLocaleName = allLocales.find(
Expand Down

0 comments on commit 073ec7d

Please sign in to comment.