Skip to content

Commit

Permalink
fix: check empty bounds when fitting map to landscape list (#1579)
Browse files Browse the repository at this point in the history
* fix: check empty bounds when fitting map to landscape list

* fix: landscape list tests correctly handle empty and non-empty bounds
  • Loading branch information
tm-ruxandra authored Mar 1, 2024
1 parent 08f7902 commit a289f97
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
39 changes: 35 additions & 4 deletions src/landscape/components/LandscapeList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ beforeEach(() => {
mapboxgl.LngLatBounds = jest.fn();
mapboxgl.LngLatBounds.prototype = {
extend: jest.fn().mockReturnThis(),
isEmpty: jest.fn().mockReturnValue(false),
};
mapboxgl.Map = jest.fn();
mapboxgl.Map.mockReturnValue({
Expand Down Expand Up @@ -74,7 +75,7 @@ const setup = async initialState => {
});
};

const baseListTest = async () => {
const setupTestMap = async () => {
const events = {};
const map = {
on: jest.fn().mockImplementation((...args) => {
Expand Down Expand Up @@ -108,12 +109,19 @@ const baseListTest = async () => {
remove: jest.fn(),
};
mapboxgl.Popup.mockReturnValue(Popup);
const isMember = {
3: true,
return {
map: map,
events: events,
Popup: Popup,
};
};

const baseListTest = async () => {
const { map, events, Popup } = await setupTestMap();
const membersCounts = [0, 23, 59, 2, 1, 28, 6, 23, 9, 11, 1, 2, 3, 4, 5];

const isMember = {
3: true,
};
const landscapes = Array(15)
.fill(0)
.map((i, landscapeIndex) => ({
Expand Down Expand Up @@ -197,7 +205,11 @@ test('LandscapeList: Display loader', async () => {
});
expect(loader).toBeInTheDocument();
});

test('LandscapeList: Empty', async () => {
const { map } = await setupTestMap();
mapboxgl.LngLatBounds.prototype.isEmpty = jest.fn().mockReturnValue(true);

terrasoApi.requestGraphQL.mockReturnValue(
Promise.resolve({
landscapes: {
Expand All @@ -209,7 +221,26 @@ test('LandscapeList: Empty', async () => {
expect(
screen.getByText('First, double check the spelling of the landscape name.')
).toBeInTheDocument();

expect(map.fitBounds.mock.calls).toHaveLength(0);
});

test('LandscapeList: Non-empty', async () => {
const { map } = await setupTestMap();
mapboxgl.LngLatBounds.prototype.isEmpty = jest.fn().mockReturnValue(false);

terrasoApi.requestGraphQL.mockReturnValue(
Promise.resolve({
landscapes: {
edges: [],
},
})
);
await setup();

expect(map.fitBounds.mock.calls).toHaveLength(1);
});

test('LandscapeList: Display list', async () => {
await baseListTest();

Expand Down
9 changes: 6 additions & 3 deletions src/landscape/components/LandscapeListMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ const Clusters = props => {
new mapboxgl.LngLatBounds()
);

map.fitBounds(bounds, {
padding: 50,
});
if (!bounds.isEmpty()) {
map.fitBounds(bounds, {
padding: 50,
});
}

return () => {
map.off('click', 'clusters', onClusterClick);
map.off('click', 'unclustered-point', onUnclusteredPointClick);
Expand Down

0 comments on commit a289f97

Please sign in to comment.