Skip to content

Commit

Permalink
🔧 Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasGilg committed Feb 22, 2024
1 parent 7ccbbfc commit f569188
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 38 deletions.
15 changes: 2 additions & 13 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import './App.scss';
import TopBar from './components/TopBar';
import Sidebar from './components/Sidebar';
import MainContent from './components/MainContent';
import {Store, Persistor} from './store';
import {Persistor, Store} from './store';
import Box from '@mui/material/Box';
import {ThemeProvider} from '@mui/material/styles';
import Theme from './util/Theme';
import {PersistGate} from 'redux-persist/integration/react';
import {useAppDispatch, useAppSelector} from './store/hooks';
import {selectDistrict} from './store/DataSelectionSlice';
import {LocalizationProvider} from '@mui/x-date-pickers/LocalizationProvider';
import {AdapterDayjs} from '@mui/x-date-pickers/AdapterDayjs';
import {I18nextProvider, useTranslation} from 'react-i18next';
import i18n from './util/i18n';
import {MUILocalization} from './components/shared/MUILocalization';

/**
* This is the root element of the React application. It divides the main screen area into the three main components.
Expand Down Expand Up @@ -78,13 +77,3 @@ function Initializer() {

return null;
}

function MUILocalization(props: {children: string | JSX.Element | JSX.Element[]}): JSX.Element {
const {i18n} = useTranslation();

return (
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale={i18n.language}>
{props.children}
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
// SPDX-License-Identifier: Apache-2.0

// SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import {describe, test, vi, afterEach, beforeEach} from 'vitest';
import {cleanup, render, screen} from '@testing-library/react';

import i18n from '../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import {Provider} from 'react-redux';
import {Store} from '../../../../store';
import {setStartDate} from '../../../../store/DataSelectionSlice';
import CompartmentList from '../../../../components/Scenario/CompartmentList';
import {MUILocalization} from '../../../../components/shared/MUILocalization';

describe('CompartmentList', () => {
vi.stubGlobal('fetch', async () => Promise.all([]));

// Mock the ResizeObserver
const ResizeObserverMock = vi.fn(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));

vi.stubGlobal('ResizeObserver', ResizeObserverMock);

beforeEach(() => {
Store.dispatch(setStartDate('2020-02-20'));
});

test('Date Loaded Correctly', () => {
render(
<I18nextProvider i18n={i18n}>
<MUILocalization>
<Provider store={Store}>
<CompartmentList />
</Provider>
</MUILocalization>
</I18nextProvider>
);

screen.getAllByText('scenario.reference-day');
screen.getByPlaceholderText('MM/DD/YYYY');
screen.getByDisplayValue('02/20/2020');
});

afterEach(() => {
cleanup();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import React from 'react';
import {describe, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import {Provider} from 'react-redux';
import {Store} from '../../../store';
import DistrictMap from '../../../components/Sidebar/DistrictMap';
import Theme from '../../../util/Theme';
import {Store} from '../../../../store';
import DistrictMap from '../../../../components/Sidebar/DistrictMap';
import Theme from '../../../../util/Theme';
import {ThemeProvider} from '@mui/material/styles';

describe('District Map', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {describe, test, expect, vi, afterEach} from 'vitest';
import {act, cleanup, render, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import SearchBar from '../../../components/Sidebar/SearchBar';
import SearchBar from '../../../../components/Sidebar/SearchBar';
import {I18nextProvider} from 'react-i18next';
import {Provider} from 'react-redux';
import {Store} from '../../../store';
import {selectDistrict} from '../../../store/DataSelectionSlice';
import {Store} from '../../../../store';
import {selectDistrict} from '../../../../store/DataSelectionSlice';

describe('SearchBar', () => {
// We mock fetch to return two entries for searchable districts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import React from 'react';
import {describe, test} from 'vitest';
import {render} from '@testing-library/react';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import SidebarHistory from '../../../components/Sidebar/SidebarHistory';
import SidebarHistory from '../../../../components/Sidebar/SidebarHistory';
import {I18nextProvider} from 'react-i18next';

describe('History', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {describe, test} from 'vitest';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import ApplicationMenu from '../../../components/TopBar/ApplicationMenu';
import ApplicationMenu from '../../../../components/TopBar/ApplicationMenu';

describe('TopBarMenu', () => {
test('open', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import React from 'react';
import {describe, test, expect, vi} from 'vitest';
import {act, render, screen} from '@testing-library/react';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import LanguagePicker from '../../../components/TopBar/LanguagePicker';
import LanguagePicker from '../../../../components/TopBar/LanguagePicker';

describe('LanguagePicker', () => {
vi.stubGlobal('fetch', vi.fn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {describe, test} from 'vitest';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../../util/i18nForTests';
import i18n from '../../../../../util/i18nForTests';
import {I18nextProvider} from 'react-i18next';
import ApplicationMenu from '../../../../components/TopBar/ApplicationMenu';
import ApplicationMenu from '../../../../../components/TopBar/ApplicationMenu';

describe('AccessibilityDialog', () => {
test('PopUp', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {describe, test, vi, expect} from 'vitest';
import {act, render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../../util/i18nForTests';
import i18n from '../../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import {forceVisible} from 'react-lazyload';
import ApplicationMenu from '../../../../components/TopBar/ApplicationMenu';
import ApplicationMenu from '../../../../../components/TopBar/ApplicationMenu';

describe('AttributionDialog', () => {
test('PopUp', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {describe, test} from 'vitest';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../../util/i18nForTests';
import i18n from '../../../../../util/i18nForTests';

import {I18nextProvider} from 'react-i18next';
import ApplicationMenu from '../../../../components/TopBar/ApplicationMenu';
import ApplicationMenu from '../../../../../components/TopBar/ApplicationMenu';

describe('ImprintDialog', () => {
test('PopUp', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {describe, test} from 'vitest';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../../util/i18nForTests';
import i18n from '../../../../../util/i18nForTests';
import {I18nextProvider} from 'react-i18next';
import ApplicationMenu from '../../../../components/TopBar/ApplicationMenu';
import ApplicationMenu from '../../../../../components/TopBar/ApplicationMenu';

describe('PrivacyPolicyDialog', () => {
test('PopUp', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import React from 'react';
import {describe, test} from 'vitest';
import {render, screen} from '@testing-library/react';

import i18n from '../../../util/i18nForTests';
import i18n from '../../../../util/i18nForTests';

import TopBar from '../../../components/TopBar';
import TopBar from '../../../../components/TopBar';
import {I18nextProvider} from 'react-i18next';

describe('TopBar', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import {describe, expect, it, vi} from 'vitest';
import {render, fireEvent, screen} from '@testing-library/react';

import ConfirmDialog from '../../../components/shared/ConfirmDialog';
import ConfirmDialog from '../../../../components/shared/ConfirmDialog';

describe('ConfirmDialog', () => {
it('renders the correct title and text', () => {
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions frontend/src/components/shared/MUILocalization.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
// SPDX-License-Identifier: Apache-2.0

import {useTranslation} from 'react-i18next';
import {LocalizationProvider} from '@mui/x-date-pickers/LocalizationProvider';
import {AdapterDayjs} from '@mui/x-date-pickers/AdapterDayjs';
import React from 'react';

export function MUILocalization(props: {children: string | JSX.Element | JSX.Element[]}): JSX.Element {
const {i18n} = useTranslation();

return (
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale={i18n.language}>
{props.children}
</LocalizationProvider>
);
}

0 comments on commit f569188

Please sign in to comment.