-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt prop type validation method to first check for enum field (#13155)
* Adapt method that check if property type is valid to first check for enum field * Fix PR comments --------- Co-authored-by: William Thorenfeldt <[email protected]>
- Loading branch information
Showing
9 changed files
with
277 additions
and
162 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
3 changes: 3 additions & 0 deletions
3
frontend/packages/ux-editor/src/components/config/FormComponentConfig.module.css
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,3 @@ | ||
.objectPropertyContainer { | ||
gap: 0.5rem; | ||
} |
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/useComponentPropertyEnumValue.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 { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { useComponentPropertyEnumValue } from '@altinn/ux-editor/hooks/useComponentPropertyEnumValue'; | ||
import type { KeyValuePairs } from 'app-shared/types/KeyValuePairs'; | ||
|
||
const enumValueWithoutText = 'enumValueWithoutText'; | ||
const customTextMockToHandleUndefined = ( | ||
keys: string | string[], | ||
variables?: KeyValuePairs<string>, | ||
) => { | ||
const key = Array.isArray(keys) ? keys[0] : keys; | ||
if (key === `ux_editor.component_properties.enum_${enumValueWithoutText}`) return key; | ||
return variables | ||
? '[mockedText(' + key + ', ' + JSON.stringify(variables) + ')]' | ||
: '[mockedText(' + key + ')]'; | ||
}; | ||
|
||
jest.mock('react-i18next', () => ({ | ||
useTranslation: () => ({ | ||
t: customTextMockToHandleUndefined, | ||
}), | ||
})); | ||
|
||
describe('useComponentPropertyEnumValue', () => { | ||
it('Returns a function that returns the enum value', () => { | ||
const result = renderHook(() => useComponentPropertyEnumValue()).result.current; | ||
const propertyEnumValue = result('testEnumValue'); | ||
expect(propertyEnumValue).toEqual( | ||
textMock('ux_editor.component_properties.enum_testEnumValue'), | ||
); | ||
}); | ||
it('Returns a function that returns the enum value if there was no text key for the description', () => { | ||
const result = renderHook(() => useComponentPropertyEnumValue()).result.current; | ||
const propertyEnumValue = result(enumValueWithoutText); | ||
expect(propertyEnumValue).toEqual(enumValueWithoutText); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
frontend/packages/ux-editor/src/hooks/useComponentPropertyEnumValue.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,14 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useCallback } from 'react'; | ||
|
||
export const useComponentPropertyEnumValue = () => { | ||
const { t } = useTranslation(); | ||
return useCallback( | ||
(value: string) => { | ||
const translationKey: string = `ux_editor.component_properties.enum_${value}`; | ||
const translation = t(translationKey); | ||
return translation === translationKey ? value : translation; | ||
}, | ||
[t], | ||
); | ||
}; |
Oops, something went wrong.