|
1 |
| -import { elementOpen, elementClose } from 'melody-idom'; |
| 1 | +import { elementOpen, elementClose, component } from 'melody-idom'; |
2 | 2 |
|
3 | 3 | import { createComponent, render } from '../src';
|
4 | 4 |
|
@@ -41,4 +41,112 @@ describe('dispatchCustomEvent', () => {
|
41 | 41 |
|
42 | 42 | expect(eventListenerCallback).toHaveBeenCalledTimes(1);
|
43 | 43 | });
|
| 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 | + }); |
44 | 152 | });
|
0 commit comments