Skip to content

Commit a9a09f8

Browse files
authoredSep 8, 2020
Set bubbles: true as default option (#127)
1 parent 8dd71e9 commit a9a09f8

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed
 

‎packages/melody-streams/__tests__/dispatchCustomEventSpec.js

+109-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { elementOpen, elementClose } from 'melody-idom';
1+
import { elementOpen, elementClose, component } from 'melody-idom';
22

33
import { createComponent, render } from '../src';
44

@@ -41,4 +41,112 @@ describe('dispatchCustomEvent', () => {
4141

4242
expect(eventListenerCallback).toHaveBeenCalledTimes(1);
4343
});
44+
45+
describe('bubbles', () => {
46+
const createElements = (done, options) => {
47+
const innerTemplate = {
48+
render() {
49+
elementOpen('div');
50+
elementClose('div');
51+
},
52+
};
53+
54+
const InnerComponentFunction = jest.fn(
55+
({ props, updates, subscribe, dispatchCustomEvent }) => {
56+
subscribe(
57+
updates.pipe(
58+
first(),
59+
tap(() => {
60+
dispatchCustomEvent(
61+
'myBubblingEvent',
62+
{},
63+
options
64+
);
65+
66+
setTimeout(done, 100);
67+
}),
68+
catchError(err => {
69+
done(err);
70+
return of(err);
71+
})
72+
)
73+
);
74+
75+
return props;
76+
}
77+
);
78+
79+
const InnerComponent = createComponent(
80+
InnerComponentFunction,
81+
innerTemplate
82+
);
83+
84+
const outerTemplate = {
85+
render() {
86+
elementOpen('div');
87+
component(InnerComponent, 'innerComponent');
88+
elementClose('div');
89+
},
90+
};
91+
92+
const OuterComponentFunction = jest.fn(({ props }) => props);
93+
94+
const OuterComponent = createComponent(
95+
OuterComponentFunction,
96+
outerTemplate
97+
);
98+
99+
const root = document.createElement('div');
100+
const eventListenerCallback = jest.fn(() => done());
101+
root.addEventListener('myBubblingEvent', eventListenerCallback);
102+
103+
render(root, OuterComponent);
104+
105+
return {
106+
InnerComponentFunction,
107+
OuterComponentFunction,
108+
eventListenerCallback,
109+
};
110+
};
111+
112+
describe('by default', () => {
113+
it('if only the eventName was passed', done => {
114+
const {
115+
InnerComponentFunction,
116+
OuterComponentFunction,
117+
eventListenerCallback,
118+
} = createElements(done);
119+
120+
expect(InnerComponentFunction).toHaveBeenCalledTimes(1);
121+
expect(OuterComponentFunction).toHaveBeenCalledTimes(1);
122+
expect(eventListenerCallback).toHaveBeenCalledTimes(1);
123+
});
124+
125+
it('if the eventName and some additional options are passed', done => {
126+
const {
127+
InnerComponentFunction,
128+
OuterComponentFunction,
129+
eventListenerCallback,
130+
} = createElements(done, { cancelable: true });
131+
132+
expect(InnerComponentFunction).toHaveBeenCalledTimes(1);
133+
expect(OuterComponentFunction).toHaveBeenCalledTimes(1);
134+
expect(eventListenerCallback).toHaveBeenCalledTimes(1);
135+
});
136+
});
137+
138+
describe('by default is overwritten and prevented', () => {
139+
it('if `bubbles: false` is set manually', done => {
140+
const {
141+
InnerComponentFunction,
142+
OuterComponentFunction,
143+
eventListenerCallback,
144+
} = createElements(done, { bubbles: false });
145+
146+
expect(InnerComponentFunction).toHaveBeenCalledTimes(1);
147+
expect(OuterComponentFunction).toHaveBeenCalledTimes(1);
148+
expect(eventListenerCallback).not.toHaveBeenCalled();
149+
});
150+
});
151+
});
44152
});

‎packages/melody-streams/src/component.js

+2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Object.assign(Component.prototype, {
5555
if (this.subscriptions.length === 0) {
5656
const t = this.getTransform({
5757
dispatchCustomEvent: (eventName, detail, options = {}) => {
58+
const defaultOptions = { bubbles: true };
5859
const event = new CustomEvent(eventName, {
60+
...defaultOptions,
5961
...options,
6062
detail,
6163
});

0 commit comments

Comments
 (0)
Please sign in to comment.