Skip to content

Commit

Permalink
feat(hycu): add usePackLabel hook
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Barske <[email protected]>
  • Loading branch information
tibs245 committed Oct 8, 2024
1 parent 8f4c5aa commit e5d775b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/manager/apps/hycu/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const packTypeLabel = {
'hycu-cloud-vm-pack-25': '25 VMs',
'hycu-cloud-vm-pack-50': '50 VMs',
'hycu-cloud-vm-pack-100': '100 VMs',
'hycu-cloud-vm-pack-150': '150 VMs',
'hycu-cloud-vm-pack-200': '200 VMs',
'hycu-cloud-vm-pack-250': '250 VMs',
'hycu-cloud-vm-pack-500': '500 VMs',
} as const;
47 changes: 47 additions & 0 deletions packages/manager/apps/hycu/src/hooks/service/usePackLabel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it, test, vi } from 'vitest';
import { usePackTypeLabel } from './usePackLabel';

vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (translationKey: string) => `${translationKey}_translated`,
i18n: {
// eslint-disable-next-line @typescript-eslint/no-empty-function
changeLanguage: () => new Promise(() => {}),
},
}),
}));

describe('get service key type translation ', () => {
const useCases: {
type: string;
label: string;
}[] = [
{
type: 'hycu-cloud-vm-pack-25',
label: '25 VMs',
},
{
type: 'hycu-cloud-vm-pack-250',
label: '250 VMs',
},
];
test.each(useCases)(
'should return the right translation key for $type',
({ type, label }) => {
// given type and translationKey
// when
const result = usePackTypeLabel(type);
// then
expect(result).toBe(label);
},
);
it('should return type if unexpected value', () => {
// given
const type = 'hycu-cloud-vm-pack-123';
// when
const result = usePackTypeLabel(type);

// then
expect(result).toBe('hycu-cloud-vm-pack-unknown_translated');
});
});
11 changes: 11 additions & 0 deletions packages/manager/apps/hycu/src/hooks/service/usePackLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useTranslation } from 'react-i18next';
import { packTypeLabel } from '@/constants';

export const usePackTypeLabel = (packType: string) => {
const { t } = useTranslation('common');

return (
packTypeLabel[packType as keyof typeof packTypeLabel] ??
t('hycu-cloud-vm-pack-unknown')
);
};

0 comments on commit e5d775b

Please sign in to comment.