Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions frontend/__tests__/unit/components/ChapterMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,42 @@ describe('ChapterMap', () => {
expect(L.marker).toHaveBeenCalledWith([51.5074, -0.1278], { icon: mockIcon })
})

it('filters out virtual chapters when latitude longitude undefined', () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const L = require('leaflet')
const virtualChapterData: Chapter[] = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use require in this project. This is why you were getting the linting error - please do not just disable the error with the comment, they are there for a reason.

So please update these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this. I wrote the test this way because the previous tests were also using require. I have now replaced all these tests using require with import

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kart-u yes, sorry! After I left this message I did a global search and found a few more spots where we had the require statement that clearly missed in some prev. PRs. I'll be cleaning those up. Thanks for updating your code!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello @kasya
just wanted to check if everything in current PR looks good or is there anything else you’d like me to update?

mockChapterData[0],
{
// A virtual chapter with no location data
...mockChapterData[1],
_geoloc: undefined,
geoLocation: undefined,
},
]

render(<ChapterMap {...defaultProps} geoLocData={virtualChapterData} />)
expect(L.marker).toHaveBeenCalledTimes(1)
expect(L.marker).not.toHaveBeenCalledWith([undefined, undefined], { icon: mockIcon })
})

it('filters out virtual chapters when latitude longitude null', () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const L = require('leaflet')
const virtualChapterData: Chapter[] = [
mockChapterData[0],
{
// A virtual chapter with no location data
...mockChapterData[1],
_geoloc: null,
geoLocation: null,
},
]

render(<ChapterMap {...defaultProps} geoLocData={virtualChapterData} />)
expect(L.marker).toHaveBeenCalledTimes(1)
expect(L.marker).not.toHaveBeenCalledWith([null, null], { icon: mockIcon })
})

it('creates marker icons with correct configuration', () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const L = require('leaflet')
Expand Down Expand Up @@ -198,6 +234,22 @@ describe('ChapterMap', () => {
render(<ChapterMap {...defaultProps} geoLocData={chapterWithoutGeoloc} />)
expect(L.marker).toHaveBeenCalledWith([35.6762, 139.6503], { icon: mockIcon })
})

it('handles chapters with 0 coordinates correctly', () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const L = require('leaflet')
const chapterWithZeroCoords: Chapter[] = [
{
...mockChapterData[0],
_geoloc: { lat: 0, lng: 0 },
geoLocation: { lat: 0, lng: 0 },
},
]

render(<ChapterMap {...defaultProps} geoLocData={chapterWithZeroCoords} />)
expect(L.marker).toHaveBeenCalledTimes(1)
expect(L.marker).toHaveBeenCalledWith([0, 0], { icon: mockIcon })
})
})

describe('Popups', () => {
Expand Down
26 changes: 16 additions & 10 deletions frontend/src/components/ChapterMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ const ChapterMap = ({

const markerClusterGroup = markerClusterRef.current

const markers = geoLocData.map((chapter) => {
const validGeoLocData = geoLocData.filter((chapter) => {
const lat = chapter._geoloc?.lat ?? chapter.geoLocation?.lat
const lng = chapter._geoloc?.lng ?? chapter.geoLocation?.lng
return typeof lat === 'number' && typeof lng === 'number'
})

const markers = validGeoLocData.map((chapter) => {
const markerIcon = new L.Icon({
iconAnchor: [12, 41],
iconRetinaUrl: '/img/marker-icon-2x.png',
Expand All @@ -61,8 +67,8 @@ const ChapterMap = ({

const marker = L.marker(
[
chapter._geoloc?.lat || chapter.geoLocation?.lat,
chapter._geoloc?.lng || chapter.geoLocation?.lng,
chapter._geoloc?.lat ?? chapter.geoLocation?.lat,
chapter._geoloc?.lng ?? chapter.geoLocation?.lng,
],
{ icon: markerIcon }
)
Expand All @@ -80,21 +86,21 @@ const ChapterMap = ({

markerClusterGroup.addLayers(markers)

if (showLocal && geoLocData.length > 0) {
if (showLocal && validGeoLocData.length > 0) {
const maxNearestChapters = 5
const localChapters = geoLocData.slice(0, maxNearestChapters - 1)
const localChapters = validGeoLocData.slice(0, maxNearestChapters - 1)
const localBounds = L.latLngBounds(
localChapters.map((chapter) => [
chapter._geoloc?.lat || chapter.geoLocation?.lat,
chapter._geoloc?.lng || chapter.geoLocation?.lng,
chapter._geoloc?.lat ?? chapter.geoLocation?.lat,
chapter._geoloc?.lng ?? chapter.geoLocation?.lng,
])
)
const maxZoom = 7
const nearestChapter = geoLocData[0]
const nearestChapter = validGeoLocData[0]
map.setView(
[
nearestChapter._geoloc?.lat || nearestChapter.geoLocation?.lat,
nearestChapter._geoloc?.lng || nearestChapter.geoLocation?.lng,
nearestChapter._geoloc?.lat ?? nearestChapter.geoLocation?.lat,
nearestChapter._geoloc?.lng ?? nearestChapter.geoLocation?.lng,
],
maxZoom
)
Expand Down