Skip to content

Commit

Permalink
fix: make event bus prop optional
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Mar 29, 2022
1 parent f7c1190 commit 5a21e9d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/PropertiesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const bufferedEvents = [
* @param {Function} [props.layoutChanged]
* @param {DescriptionConfig} [props.descriptionConfig]
* @param {Function} [props.descriptionLoaded]
* @param {Object} [props.eventBus]
*/
export default function PropertiesPanel(props) {
const {
Expand Down
6 changes: 1 addition & 5 deletions src/context/EventContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@

import { createContext } from 'preact';

import EventBus from 'diagram-js/lib/core/EventBus';

const eventBus = new EventBus();

const EventContext = createContext({ eventBus });
const EventContext = createContext({ eventBus: null });

export default EventContext;
6 changes: 5 additions & 1 deletion src/hooks/useEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export function useEvent(event, callback, priority = DEFAULT_PRIORITY) {
const { eventBus } = useContext(EventContext);

useEffect(() => {
if (!eventBus) {
return;
}

eventBus.on(event, priority, callback);

return () => eventBus.off(event, callback);
}, [ event, eventBus, callback, priority ]);
}, [ callback, event, eventBus, priority ]);
}
4 changes: 3 additions & 1 deletion src/hooks/useShowErrorEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function useShowErrorEvent(show) {
setTemporaryError(null);

if (show(event)) {
eventBus.fire('propertiesPanel.showEntry', event);
if (eventBus) {
eventBus.fire('propertiesPanel.showEntry', event);
}

setTemporaryError(event.message);
}
Expand Down
13 changes: 13 additions & 0 deletions test/spec/hooks/useEvent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ describe('hooks/useEvent', function() {
});


it('should not subscribe (no event bus)', function() {

// given
const onSpy = sinon.spy(eventBus, 'on');

// when
renderHook(() => useEvent('foo', noop));

// then
expect(onSpy).not.to.have.been.called;
});


it('should call callback', function() {

// given
Expand Down
22 changes: 22 additions & 0 deletions test/spec/hooks/useShowEntryEvent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ describe('hooks/useShowEntryEvent', function() {
});


it('should not call onShow (no event bus)', function() {

// given
const show = () => true;

const onShowSpy = sinon.spy();

renderHook(() => {
const ref = useShowEntryEvent(show);

return <input id="foo" ref={ ref } />;
});

// when

eventBus.fire('propertiesPanel.showEntry');

// then
expect(onShowSpy).not.to.have.been.called;
});


it('should call focus', function() {

// given
Expand Down
29 changes: 22 additions & 7 deletions test/spec/hooks/useShowErrorEvent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,36 @@ describe('hooks/useShowErrorEvent', function() {
// given
const show = () => true;

const onShowSpy = sinon.spy();
let temporaryError;

renderHook(() => {
temporaryError = useShowErrorEvent(show);
}, { wrapper: WithEventContext(eventBus) });

// when
act(() => eventBus.fire('propertiesPanel.showError', { message: 'foo' }));

// then
expect(temporaryError).to.equal('foo');
});


it('should not set temporary error (no event bus)', function() {

// given
const show = () => true;

let temporaryError;

renderHook(() => {
temporaryError = useShowErrorEvent(show);
}, { wrapper: WithEventContext(eventBus, onShowSpy) });
});

// when
act(() => eventBus.fire('propertiesPanel.showError', { message: 'foo' }));

// then
expect(temporaryError).to.have.equal('foo');
expect(temporaryError).to.be.null;
});


Expand All @@ -52,7 +69,7 @@ describe('hooks/useShowErrorEvent', function() {

renderHook(() => {
useShowErrorEvent(show);
}, { wrapper: WithEventContext(eventBus, noop) });
}, { wrapper: WithEventContext(eventBus) });

// when
act(() => eventBus.fire('propertiesPanel.showError', { message: 'foo' }));
Expand All @@ -68,13 +85,11 @@ describe('hooks/useShowErrorEvent', function() {
// given
const show = () => true;

const onShowSpy = sinon.spy();

let temporaryError;

renderHook(() => {
temporaryError = useShowErrorEvent(show);
}, { wrapper: WithEventContext(eventBus, onShowSpy) });
}, { wrapper: WithEventContext(eventBus) });

act(() => eventBus.fire('propertiesPanel.showError', { message: 'foo' }));

Expand Down

0 comments on commit 5a21e9d

Please sign in to comment.