-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b59df4b
commit c6d5c5a
Showing
4 changed files
with
57 additions
and
3 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
37 changes: 37 additions & 0 deletions
37
frontend/packages/ux-editor/src/hooks/useComponentPropertyHelpText.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,37 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useComponentPropertyHelpText } from './useComponentPropertyHelpText'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import type { KeyValuePairs } from 'app-shared/types/KeyValuePairs'; | ||
|
||
const somePropertyName = 'undefinedKey'; | ||
|
||
const customTextMockToHandleUndefined = ( | ||
keys: string | string[], | ||
variables?: KeyValuePairs<string>, | ||
) => { | ||
const key = Array.isArray(keys) ? keys[0] : keys; | ||
if (key === `ux_editor.component_properties_help_text.${somePropertyName}`) return key; | ||
return variables | ||
? '[mockedText(' + key + ', ' + JSON.stringify(variables) + ')]' | ||
: '[mockedText(' + key + ')]'; | ||
}; | ||
|
||
jest.mock('react-i18next', () => ({ | ||
useTranslation: () => ({ | ||
t: customTextMockToHandleUndefined, | ||
}), | ||
})); | ||
|
||
describe('useComponentPropertyHelpText', () => { | ||
it('Returns a function that returns the help text', () => { | ||
const result = renderHook(() => useComponentPropertyHelpText()).result.current; | ||
const propertyHelpText = result('test'); | ||
expect(propertyHelpText).toEqual(textMock('ux_editor.component_properties_help_text.test')); | ||
}); | ||
|
||
it('Returns a function that returns undefined if there was no text key for the help text', () => { | ||
const result = renderHook(() => useComponentPropertyHelpText()).result.current; | ||
const propertyHelpText = result(somePropertyName); | ||
expect(propertyHelpText).toEqual(undefined); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
frontend/packages/ux-editor/src/hooks/useComponentPropertyHelpText.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,16 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useCallback } from 'react'; | ||
|
||
export const useComponentPropertyHelpText = () => { | ||
const { t } = useTranslation(); | ||
|
||
return useCallback( | ||
(propertyKey: string): string | undefined => { | ||
const translationKey: string = `ux_editor.component_properties_help_text.${propertyKey}`; | ||
const translation = t(translationKey); | ||
console.log(translation, translationKey); | ||
return translation !== translationKey ? translation : undefined; | ||
}, | ||
[t], | ||
); | ||
}; |