Skip to content

Commit 148bcae

Browse files
committed
test: test for language switcher
1 parent 662ed27 commit 148bcae

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

src/sections/dataset/dataset-metadata/export-metadata-dropdown/ExportMetadataDropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { DataverseInfoRepository } from '@/info/domain/repositories/DataverseInf
66
import { QueryParamKey } from '@/sections/Route.enum'
77
import { requireAppConfig } from '@/config'
88

9-
const appConfig = requireAppConfig()
10-
119
interface ExportMetadataDropdownProps {
1210
datasetPersistentId: string
1311
datasetIsReleased: boolean
@@ -25,6 +23,8 @@ export const ExportMetadataDropdown = ({
2523
anonymizedView,
2624
dataverseInfoRepository
2725
}: ExportMetadataDropdownProps) => {
26+
const appConfig = requireAppConfig()
27+
2828
const { t } = useTranslation('shared')
2929
const { datasetMetadataExportFormats, isLoadingExportFormats, errorGetExportFormats } =
3030
useGetAvailableDatasetMetadataExportFormats({ dataverseInfoRepository })

src/sections/file/file-metadata/FileMetadata.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import { File } from '@/files/domain/models/File'
1414
import styles from './FileMetadata.module.scss'
1515
import { DatasetPublishingStatus } from '@/dataset/domain/models/Dataset'
1616

17-
const appConfig = requireAppConfig()
18-
1917
interface FileMetadataProps {
2018
name: string
2119
metadata: FileMetadataModel
@@ -39,6 +37,7 @@ export function FileMetadata({
3937
dataverseInfoRepository
4038
}: FileMetadataProps) {
4139
const { t } = useTranslation('file')
40+
const appConfig = requireAppConfig()
4241

4342
return (
4443
<>

src/sections/layout/header/LanguageSwitcher.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import { Translate as TranslateIcon } from 'react-bootstrap-icons'
44
import { LANGUAGE_LOCAL_STORAGE_KEY } from '@/i18n'
55
import { requireAppConfig } from '@/config'
66

7-
// TODO:ME - Show screenshots of variants of dropdown title (with icon, only icon, only text, both) and get feedback from team
8-
97
export const LanguageSwitcher = () => {
108
const { i18n, t } = useTranslation('shared')
119

1210
const appConfig = requireAppConfig()
1311

14-
// We dont render the language switcher if there is only one language configured
1512
if (appConfig.languages.length < 2) return null
1613

1714
const handleSelectLanguage = (lang: string) => {
@@ -27,7 +24,7 @@ export const LanguageSwitcher = () => {
2724
title={
2825
<div className="d-inline-flex align-items-center gap-1">
2926
<TranslateIcon />
30-
<span>{t('language')}</span>
27+
<span data-testid="language-switcher-dropdown-title">{t('language')}</span>
3128
</div>
3229
}
3330
id="language-switcher-dropdown">
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { LANGUAGE_LOCAL_STORAGE_KEY } from '@/i18n'
2+
import { LanguageSwitcher } from '@/sections/layout/header/LanguageSwitcher'
3+
4+
describe('LanguageSwitcher', () => {
5+
afterEach(() => {
6+
cy.clearAllLocalStorage()
7+
})
8+
9+
it(
10+
'does not render when only one language is configured',
11+
{
12+
env: {
13+
LANGUAGES: [{ code: 'en', name: 'English' }]
14+
}
15+
},
16+
() => {
17+
cy.customMount(<LanguageSwitcher />)
18+
19+
cy.get('#language-switcher-dropdown').should('not.exist')
20+
}
21+
)
22+
23+
it(
24+
'renders language options correctly when more than one language is configured',
25+
{
26+
env: {
27+
LANGUAGES: [
28+
{ code: 'en', name: 'English' },
29+
{ code: 'es', name: 'Español' },
30+
{ code: 'it', name: 'Italiano' }
31+
]
32+
}
33+
},
34+
() => {
35+
cy.customMount(<LanguageSwitcher />)
36+
37+
cy.get('#language-switcher-dropdown').should('exist')
38+
39+
cy.get('#language-switcher-dropdown').click()
40+
41+
cy.findByText('English').should('exist')
42+
cy.findByText('Español').should('exist')
43+
cy.findByText('Italiano').should('exist')
44+
}
45+
)
46+
47+
it('changes language when a language option is selected', () => {
48+
cy.customMount(<LanguageSwitcher />)
49+
50+
cy.findByTestId('language-switcher-dropdown-title').should('have.text', 'Language')
51+
52+
cy.get('#language-switcher-dropdown').should('exist').click()
53+
54+
cy.findByText('English').should('have.class', 'active')
55+
56+
cy.findByText('Español').click()
57+
58+
cy.findByText('Español').should('have.class', 'active')
59+
cy.findByText('English').should('not.have.class', 'active')
60+
61+
// Dropdown title should update to the selected language
62+
cy.findByTestId('language-switcher-dropdown-title').should('have.text', 'Idioma')
63+
64+
// Verify that the selected language is stored in localStorage
65+
cy.window().then((win) => {
66+
const storedLang = win.localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY)
67+
expect(storedLang).to.equal('es')
68+
})
69+
70+
// Change back to English
71+
cy.get('#language-switcher-dropdown').click()
72+
cy.findByText('English').click()
73+
74+
cy.findByTestId('language-switcher-dropdown-title').should('have.text', 'Language')
75+
76+
cy.findByText('English').should('have.class', 'active')
77+
cy.findByText('Español').should('not.have.class', 'active')
78+
79+
cy.window().then((win) => {
80+
const storedLang = win.localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY)
81+
expect(storedLang).to.equal('en')
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)