From 441f2af532c354b2c94fcdb90ba6908674cd169f Mon Sep 17 00:00:00 2001 From: Stanislav Lysak Date: Wed, 4 Dec 2024 12:39:28 +0200 Subject: [PATCH] VerificationBadge & LanguageMenu --- .../Hamburger/LanguageMenu.test.tsx | 62 +++++++++++++++++++ .../@molecules/Hamburger/LanguageMenu.tsx | 13 ++-- .../VerificationBadge.test.tsx | 52 ++++++++++++++++ src/test-utils.tsx | 9 +++ 4 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 src/components/@molecules/Hamburger/LanguageMenu.test.tsx create mode 100644 src/components/@molecules/VerificationBadge/VerificationBadge.test.tsx diff --git a/src/components/@molecules/Hamburger/LanguageMenu.test.tsx b/src/components/@molecules/Hamburger/LanguageMenu.test.tsx new file mode 100644 index 000000000..0e2eede13 --- /dev/null +++ b/src/components/@molecules/Hamburger/LanguageMenu.test.tsx @@ -0,0 +1,62 @@ +import { changeLanguageMock, fireEvent, mockSupportedLngs, render, screen } from '@app/test-utils' + +import { describe, expect, it, vi } from 'vitest' + +import LanguageMenu from './LanguageMenu' + +vi.mock('iso-639-1', () => ({ + default: { + getNativeName: (lang: string) => `Native Name of ${lang}`, // Return a mocked native name + }, +})) + +describe('LanguageMenu', () => { + it('renders the supported languages in sorted order', () => { + const setCurrentViewMock = vi.fn() + + render() + + mockSupportedLngs + .filter((lang) => lang && lang !== 'cimode') + .forEach((lang) => { + expect(screen.getByTestId(`lang-${lang}`)).toBeInTheDocument() + expect(screen.getByText(`Native Name of ${lang}`)).toBeInTheDocument() + expect(screen.getByText(lang.toLocaleUpperCase())).toBeInTheDocument() + }) + + expect(screen.queryByTestId('lang-cimode')).not.toBeInTheDocument() + }) + + it('changes the language when a language item is clicked', () => { + const setCurrentViewMock = vi.fn() + + render() + + const frenchLanguageItem = screen.getByTestId('lang-fr') + fireEvent.click(frenchLanguageItem) + + expect(changeLanguageMock).toHaveBeenCalledWith('fr') + }) + + it('calls setCurrentView with "main" when the heading is clicked', () => { + const setCurrentViewMock = vi.fn() + + render() + const frenchLanguageItem = screen.getByTestId('lang-header') + fireEvent.click(frenchLanguageItem) + + expect(setCurrentViewMock).toHaveBeenCalledWith('main') + }) + + it('shows the active language with a check icon', () => { + const setCurrentViewMock = vi.fn() + + render() + + const activeIcon = screen.getByTestId('check-lang-en') + expect(activeIcon).toHaveStyle({ display: 'block' }) + + const inactiveIcon = screen.getByTestId('check-lang-fr') + expect(inactiveIcon).toHaveStyle({ display: 'none' }) + }) +}) diff --git a/src/components/@molecules/Hamburger/LanguageMenu.tsx b/src/components/@molecules/Hamburger/LanguageMenu.tsx index 47e99552e..8a12d556b 100644 --- a/src/components/@molecules/Hamburger/LanguageMenu.tsx +++ b/src/components/@molecules/Hamburger/LanguageMenu.tsx @@ -143,7 +143,7 @@ const LanguageMenu = ({ // eslint-disable-next-line react-hooks/exhaustive-deps const initialLanguage = useMemo(() => i18n.resolvedLanguage, []) const languages = (i18n.options.supportedLngs || []) - .filter((lang: string) => lang && lang !== 'cimode') + .filter((lang) => lang && lang !== 'cimode') .sort((a, b) => { if (a === initialLanguage) { return -1 @@ -157,7 +157,7 @@ const LanguageMenu = ({ return ( - setCurrentView('main')}> + setCurrentView('main')}> @@ -166,11 +166,16 @@ const LanguageMenu = ({ - {languages.map((lang: string) => ( - i18n.changeLanguage(lang)}> + {languages.map((lang) => ( + i18n.changeLanguage(lang)} + >
{ISO6391.getNativeName(lang)} diff --git a/src/components/@molecules/VerificationBadge/VerificationBadge.test.tsx b/src/components/@molecules/VerificationBadge/VerificationBadge.test.tsx new file mode 100644 index 000000000..d349aef13 --- /dev/null +++ b/src/components/@molecules/VerificationBadge/VerificationBadge.test.tsx @@ -0,0 +1,52 @@ +import { render, screen } from '@app/test-utils' + +import { describe, expect, it } from 'vitest' + +import { VerificationBadge } from './VerificationBadge' + +describe('VerificationBadge', () => { + it('should render with with badge', () => { + render( + +
+
, + ) + expect(screen.getByTestId('verification-badge-record')).toBeInTheDocument() + }) + + it('should render with without badge', () => { + render( + +
+
, + ) + expect(screen.queryByTestId('verification-badge-record')).not.toBeInTheDocument() + }) + + it('should render with alert svg', () => { + render( + +
+
, + ) + expect(screen.getByTestId('verification-badge-error-icon')).toBeInTheDocument() + }) + + it('should render with verified svg', () => { + render( + +
+
, + ) + expect(screen.getByTestId('verification-badge-record-icon')).toBeInTheDocument() + }) + + it('should render with personhood svg', () => { + render( + +
+
, + ) + expect(screen.getByTestId('verification-badge-person-icon')).toBeInTheDocument() + }) +}) diff --git a/src/test-utils.tsx b/src/test-utils.tsx index 7f932c8f4..3760466c4 100644 --- a/src/test-utils.tsx +++ b/src/test-utils.tsx @@ -26,6 +26,10 @@ vi.mock('@app/hooks/useRegistrationReducer', () => vi.fn(() => ({ item: { stepIn export const mockUseAccountReturnValue = { address: '0x123' } +export const changeLanguageMock = vi.fn() + +export const mockSupportedLngs = ['en', 'fr', 'de', 'es', 'cimode'] + vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (value: string, opts: any) => { @@ -34,6 +38,11 @@ vi.mock('react-i18next', () => ({ }, i18n: { isInitialized: true, + resolvedLanguage: 'en', + options: { + supportedLngs: mockSupportedLngs, + }, + changeLanguage: changeLanguageMock, }, ready: true, }),