Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sweep Rules] Add unit tests for frontend/src/components/RegionMap.jsx #191

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions frontend/src/components/ExistingTestFile.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React from 'react';
import { render } from '@testing-library/react';

Check failure on line 2 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

Unable to resolve path to module '@testing-library/react'
import { act } from 'react-dom/test-utils';
import MapComponent from '../components/RegionMap';

Check failure on line 4 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

Useless path segments for "../components/RegionMap", should be "./RegionMap"
import { fetchSubregions, fetchSiblings, fetchRegionGeometry } from '../api';

jest.mock('../api', () => ({

Check failure on line 7 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'jest' is not defined
fetchSubregions: jest.fn(),

Check failure on line 8 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'jest' is not defined
fetchSiblings: jest.fn(),

Check failure on line 9 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'jest' is not defined
fetchRegionGeometry: jest.fn(),

Check failure on line 10 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'jest' is not defined
}));

describe('MapComponent', () => {

Check failure on line 13 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'describe' is not defined
beforeEach(() => {

Check failure on line 14 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'beforeEach' is not defined
jest.clearAllMocks();

Check failure on line 15 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'jest' is not defined
});

it('should fetch subregions and update map when selected region has subregions', async () => {

Check failure on line 18 in frontend/src/components/ExistingTestFile.test.jsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'it' is not defined
const subregions = [{ id: 1, name: 'Subregion 1' }, { id: 2, name: 'Subregion 2' }];
fetchSubregions.mockResolvedValue(subregions);

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region with subregions
// ...

// Assert fetchSubregions is called with correct arguments
// ...

// Assert map is updated with subregions
// ...
});
});

it('should fetch siblings and update map when selected region does not have subregions', async () => {
const siblings = [{ id: 3, name: 'Sibling 1' }, { id: 4, name: 'Sibling 2' }];
fetchSiblings.mockResolvedValue(siblings);

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region without subregions
// ...

// Assert fetchSiblings is called with correct arguments
// ...

// Assert map is updated with siblings
// ...
});
});

it('should handle error when fetching visible regions', async () => {
fetchSubregions.mockRejectedValue(new Error('Fetch error'));

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region with subregions
// ...

// Assert fetchSubregions is called with correct arguments
// ...

// Assert error message is displayed
// ...
});
});

it('should update selected region style when new selected region id matches a feature', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Simulate rendered features and new selected region id
// ...

// Assert selected region style is updated
// ...
});
});

it('should not update selected region style when new selected region id does not match any feature', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Simulate rendered features and new selected region id
// ...

// Assert selected region style is not updated
// ...
});
});

it('should not initialize map when map container is not set', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Assert map is not initialized
// ...
});
});

it('should initialize map and update map when data is valid', async () => {
const visibleRegions = [{ id: 5, name: 'Region 1' }, { id: 6, name: 'Region 2' }];
fetchSubregions.mockResolvedValue(visibleRegions);

const regionGeometry = { type: 'Polygon', coordinates: [[0, 0], [1, 1], [2, 2], [0, 0]] };
fetchRegionGeometry.mockResolvedValue({ geometry: regionGeometry });

const { container } = render(<MapComponent />);
await act(async () => {
// Assert visible regions are fetched
// ...

// Assert region geometry is fetched for each visible region
// ...

// Assert rendered features are set
// ...

// Assert map source and style are updated
// ...

// Assert map bounds are set
// ...
});
});

it('should not initialize map when fetched data is invalid or empty', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Assert map is not initialized
// ...
});
});
});
135 changes: 135 additions & 0 deletions frontend/src/components/RegionMap.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import React from 'react';
import { render } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import MapComponent from './RegionMap';

jest.mock('../api', () => ({
fetchSubregions: jest.fn(),
fetchSiblings: jest.fn(),
fetchRegionGeometry: jest.fn(),
}));

describe('MapComponent', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fetch subregions and update map when selected region has subregions', async () => {
const subregions = [{ id: 1, name: 'Subregion 1' }, { id: 2, name: 'Subregion 2' }];
const fetchSubregions = require('../api').fetchSubregions;
fetchSubregions.mockResolvedValue(subregions);

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region with subregions
// ...

// Assert fetchSubregions is called with correct arguments
// ...

// Assert map is updated with subregions
// ...
});
});

it('should fetch siblings and update map when selected region does not have subregions', async () => {
const siblings = [{ id: 3, name: 'Sibling 1' }, { id: 4, name: 'Sibling 2' }];
const fetchSiblings = require('../api').fetchSiblings;
fetchSiblings.mockResolvedValue(siblings);

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region without subregions
// ...

// Assert fetchSiblings is called with correct arguments
// ...

// Assert map is updated with siblings
// ...
});
});

it('should handle error when fetching visible regions', async () => {
const fetchSubregions = require('../api').fetchSubregions;
fetchSubregions.mockRejectedValue(new Error('Fetch error'));

const { container } = render(<MapComponent />);
await act(async () => {
// Simulate selected region with subregions
// ...

// Assert fetchSubregions is called with correct arguments
// ...

// Assert error message is displayed
// ...
});
});

it('should update selected region style when new selected region id matches a feature', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Simulate rendered features and new selected region id
// ...

// Assert selected region style is updated
// ...
});
});

it('should not update selected region style when new selected region id does not match any feature', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Simulate rendered features and new selected region id
// ...

// Assert selected region style is not updated
// ...
});
});

it('should not initialize map when map container is not set', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Assert map is not initialized
// ...
});
});

it('should initialize map and update map when data is valid', async () => {
const visibleRegions = [{ id: 5, name: 'Region 1' }, { id: 6, name: 'Region 2' }];
const fetchSubregions = require('../api').fetchSubregions;
fetchSubregions.mockResolvedValue(visibleRegions);

const regionGeometry = { type: 'Polygon', coordinates: [[0, 0], [1, 1], [2, 2], [0, 0]] };
const fetchRegionGeometry = require('../api').fetchRegionGeometry;
fetchRegionGeometry.mockResolvedValue({ geometry: regionGeometry });

const { container } = render(<MapComponent />);
await act(async () => {
// Assert visible regions are fetched
// ...

// Assert region geometry is fetched for each visible region
// ...

// Assert rendered features are set
// ...

// Assert map source and style are updated
// ...

// Assert map bounds are set
// ...
});
});

it('should not initialize map when fetched data is invalid or empty', async () => {
const { container } = render(<MapComponent />);
await act(async () => {
// Assert map is not initialized
// ...
});
});
});
Loading