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

BREAKING: Migrate to react-leaflet v3 #51

Merged
merged 8 commits into from
Jan 24, 2021
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
Prev Previous commit
Next Next commit
Cleanup, removing getOptions and extraneous testing code.
  • Loading branch information
EliteMasterEric committed Jan 16, 2021
commit 3e48fda1b2ea93b03c55a69e065cf8b3bddafd9b
33 changes: 3 additions & 30 deletions __tests__/TextPath.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { cloneElement } from 'react';
import { MapContainer, Polyline } from 'react-leaflet';
import { MapContainer } from 'react-leaflet';
import L from 'leaflet';
import TextPath from '../src/index';

Expand Down Expand Up @@ -42,17 +42,6 @@ function updateProps(wrapper, props) {
});
}

const getMethods = (obj) => {
let properties = new Set();
let currentObj = obj;
do {
Object.getOwnPropertyNames(currentObj).map((item) =>
properties.add(item)
);
} while ((currentObj = Object.getPrototypeOf(currentObj)));
return [...properties.keys()];
};

describe('<TextPath />', () => {
beforeEach(() => {
PolylineSpy.mockClear();
Expand Down Expand Up @@ -82,22 +71,6 @@ describe('<TextPath />', () => {
color: 'white',
});
});
/*
TODO: Revise this test. I can't seem to access the TextPath instance.
I think there's been a change to how the MapContainer works.

it('should extend the react-leaflet Path component', () => {
const wrapper = mount(
<MapContainer>
<TextPath />
</MapContainer>
);
expect(wrapper.find(TextPath).children().instance()).toBeInstanceOf(
Polyline
);
});

*/
});

describe('setText()', () => {
Expand Down Expand Up @@ -131,8 +104,8 @@ describe('<TextPath />', () => {
</MapContainer>
);
updateProps(wrapper, { orientation: 'flip' });
// TODO: There appears to be a bug where react-leaflet is recreating the element
// when its props update, but I couldn't reproduce it in a sandbox.
// TODO: react-leaflet is recreating the element instead of updating it.
// @see: https://github.com/PaulLeCam/react-leaflet/issues/830
expect(setTextSpy).toHaveBeenNthCalledWith(/* 2 */ 3, null);
});
it('should call setText() with the new text and options', () => {
Expand Down
77 changes: 47 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,68 @@ import { createPathComponent } from '@react-leaflet/core';
import { Polyline as LeafletPolyline } from 'leaflet';
import 'leaflet-textpath';

// props.leaflet is not used but destructured out so it's not passed to L.Polyline
const getOptions = ({
// Options for the specific TextPath...
positions,
text,
repeat,
center,
below,
offset,
orientation,
attributes,
leaflet, // eslint-disable-line no-unused-vars
// Options for the base Polyline...
...options
}) => {
return {
positions,
options,
// eslint-disable-next-line no-unused-vars
const createLeafletElement = (
{
// Required TextPath attributes.
text,
positions,
// TextPath options.
repeat,
center,
below,
offset,
orientation,
attributes,
};
};

// eslint-disable-next-line no-unused-vars
const createLeafletElement = (props, ctx) => {
const { positions, options, text, ...pathOptions } = getOptions(props);
// PolyLine options.
...options
},
ctx
) => {
// const { positions, options, text, ...pathOptions } = getOptions(props);
const instance = new LeafletPolyline(positions, options);
instance.setText(text, pathOptions);
instance.setText(text, {
repeat,
center,
below,
offset,
orientation,
attributes,
});
return { instance, context: { ...ctx, overlayContainer: instance } };
};

// eslint-disable-next-line no-unused-vars
const updateLeafletElement = (layer, props, prevProps) => {
const { positions, options, text, ...pathOptions } = getOptions(props);

const updateLeafletElement = (
layer,
{
// Required TextPath attributes.
text,
positions,
// TextPath options.
repeat,
center,
below,
offset,
orientation,
attributes,
// PolyLine options.
...options
},
prevProps
) => {
// Set null first, to reset the text displayed.
layer.setText(null);
if (props.positions !== prevProps.positions) layer.setLatLngs(positions);
if (positions !== prevProps.positions) layer.setLatLngs(positions);
layer.setStyle(options);
layer.setText(text, pathOptions);
layer.setText(text, {
repeat,
center,
below,
offset,
orientation,
attributes,
});
};

const TextPath = createPathComponent(
Expand Down
5 changes: 0 additions & 5 deletions test.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { configure, mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { JSDOM } from 'jsdom';

const dom = new JSDOM('<!doctype html><html><body></body></html>');
global.window = dom.window;
global.document = dom.window.document;

configure({ adapter: new Adapter() });

Expand Down