Skip to content

Commit

Permalink
🪲 Fix searchbar tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasGilg committed Mar 21, 2024
1 parent 4f751a9 commit c51303a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
34 changes: 2 additions & 32 deletions frontend/src/__tests__/components/Sidebar/SearchBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import {describe, test, expect, vi, afterEach} from 'vitest';
import {act, cleanup, render, screen, waitFor} from '@testing-library/react';
import {describe, test, expect, afterEach} from 'vitest';
import {act, cleanup, render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import i18n from '../../../util/i18nForTests';
Expand All @@ -15,28 +15,6 @@ import {Store} from '../../../store';
import {selectDistrict} from '../../../store/DataSelectionSlice';

describe('SearchBar', () => {
// We mock fetch to return two entries for searchable districts.
const fetch = vi.fn().mockImplementation(() => {
return Promise.resolve({
json: () => {
return Promise.resolve([
{
RS: '09771',
GEN: 'Aichach-Friedberg',
BEZ: 'LK',
},
{
RS: '12345',
GEN: 'Test District',
BEZ: 'Test Type',
},
]);
},
});
});

vi.stubGlobal('fetch', fetch);

test('countyList loaded correctly', async () => {
render(
<I18nextProvider i18n={i18n}>
Expand All @@ -46,8 +24,6 @@ describe('SearchBar', () => {
</I18nextProvider>
);

await waitFor(() => expect(fetch).toHaveBeenCalled());

await screen.findByPlaceholderText('germany');

await userEvent.click(screen.getByPlaceholderText('germany'));
Expand All @@ -69,8 +45,6 @@ describe('SearchBar', () => {
</I18nextProvider>
);

await waitFor(() => expect(fetch).toHaveBeenCalled());

act(() => {
Store.dispatch(selectDistrict({ags: '12345', name: 'Test District', type: 'Test Type'}));
});
Expand All @@ -87,8 +61,6 @@ describe('SearchBar', () => {
</I18nextProvider>
);

await waitFor(() => expect(fetch).toHaveBeenCalled());

await userEvent.type(screen.getByPlaceholderText('germany'), 'Aic{Enter}');

await screen.findByDisplayValue('Aichach-Friedberg (BEZ.LK)');
Expand All @@ -108,8 +80,6 @@ describe('SearchBar', () => {
</I18nextProvider>
);

await waitFor(() => expect(fetch).toHaveBeenCalled());

await userEvent.type(screen.getByPlaceholderText('germany'), '{ArrowDown}{Enter}');

await screen.findByDisplayValue('Test District (BEZ.Test Type)');
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/__tests__/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ export default [
},
});
}),
http.get('/assets/lk_germany_reduced_list.json', () => {
return HttpResponse.json([
{
RS: '09771',
GEN: 'Aichach-Friedberg',
BEZ: 'LK',
},
{
RS: '12345',
GEN: 'Test District',
BEZ: 'Test Type',
},
]);
}),
];

0 comments on commit c51303a

Please sign in to comment.