Skip to content

Commit 261fc4c

Browse files
committed
Convert class components to function components
1 parent eb42ce8 commit 261fc4c

2 files changed

Lines changed: 191 additions & 186 deletions

File tree

src/__tests__/react-plotly.test.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ import once from 'onetime';
77
describe('<Plotly/>', () => {
88
let Plotly, PlotComponent;
99

10-
// Mirrors enzyme's `mount(...).setProps(...)` / `.instance()` interface so the
11-
// existing tests can keep their shape. `setProps` re-renders via a hook-driven
12-
// wrapper; `instance` exposes the class component via a ref.
10+
// `setProps` re-renders via a hook-driven wrapper; `gd` exposes the rendered
11+
// DOM element (which the mocked Plotly.react augments to an EventEmitter,
12+
// so tests can simulate plotly events directly).
1313
function createPlot(props) {
1414
return new Promise((resolve, reject) => {
1515
let setProps;
16-
let instance;
16+
let gd;
1717
const Wrapper = () => {
1818
const [currentProps, setCurrentProps] = useState(props);
1919
setProps = (next) => act(() => setCurrentProps((prev) => ({...prev, ...next})));
2020
return (
2121
<PlotComponent
2222
{...currentProps}
23-
ref={(r) => {
24-
instance = r;
23+
ref={(el) => {
24+
gd = el;
2525
}}
2626
onInitialized={() =>
2727
resolve({
2828
setProps,
29-
get instance() {
30-
return instance;
29+
get gd() {
30+
return gd;
3131
},
3232
get props() {
3333
return currentProps;
@@ -61,12 +61,6 @@ describe('<Plotly/>', () => {
6161
beforeEach(() => {
6262
Plotly = jest.requireMock('../__mocks__/plotly.js').default;
6363
PlotComponent = createComponent(Plotly);
64-
65-
// Override the parent element size:
66-
PlotComponent.prototype.getParentSize = () => ({
67-
width: 123,
68-
height: 456,
69-
});
7064
});
7165

7266
describe('initialization', function () {
@@ -193,16 +187,21 @@ describe('<Plotly/>', () => {
193187

194188
describe('manging event handlers', () => {
195189
test('should add an event handler when one does not already exist', (done) => {
196-
const onRelayout = () => {};
197-
198-
createPlot({onRelayout}).then((plot) => {
199-
const {handlers} = plot.instance;
200-
201-
expect(plot.props.onRelayout).toBe(onRelayout);
202-
expect(handlers.Relayout).toBe(onRelayout);
190+
let received;
191+
const onRelayout = (evt) => {
192+
received = evt;
193+
};
203194

204-
done();
205-
});
195+
createPlot({onRelayout})
196+
.then((plot) => {
197+
expect(plot.props.onRelayout).toBe(onRelayout);
198+
// The mocked Plotly.react makes gd an EventEmitter. Fire the
199+
// event and verify the handler was wired through.
200+
plot.gd.emit('plotly_relayout', {hello: 'world'});
201+
expect(received).toEqual({hello: 'world'});
202+
done();
203+
})
204+
.catch((err) => done(err));
206205
});
207206
});
208207
});

0 commit comments

Comments
 (0)