|
1 | | -import { expect, fixture, html, aTimeout } from '@open-wc/testing'; |
| 1 | +import { expect, fixture, html, aTimeout, oneEvent } from '@open-wc/testing'; |
2 | 2 | import sinon from 'sinon'; |
3 | | -import { pressEnter, pressSpace } from '@polymer/iron-test-helpers/mock-interactions.js'; |
| 3 | +import { |
| 4 | + makeMouseEvent, |
| 5 | + pressEnter, |
| 6 | + pressSpace, |
| 7 | +} from '@polymer/iron-test-helpers/mock-interactions.js'; |
4 | 8 |
|
5 | 9 | import '../lion-button.js'; |
6 | 10 |
|
| 11 | +function getTopElement(el) { |
| 12 | + const { left, top } = el.getBoundingClientRect(); |
| 13 | + // to support elementFromPoint() in polyfilled browsers we have to use document |
| 14 | + const crossBrowserRoot = el.shadowRoot.elementFromPoint ? el.shadowRoot : document; |
| 15 | + return crossBrowserRoot.elementFromPoint(left, top); |
| 16 | +} |
| 17 | + |
7 | 18 | describe('lion-button', () => { |
8 | 19 | it('behaves like native `button` in terms of a11y', async () => { |
9 | 20 | const el = await fixture(`<lion-button>foo</lion-button>`); |
@@ -99,11 +110,7 @@ describe('lion-button', () => { |
99 | 110 | `); |
100 | 111 |
|
101 | 112 | const button = form.querySelector('lion-button'); |
102 | | - const { left, top } = button.getBoundingClientRect(); |
103 | | - // to support elementFromPoint() in polyfilled browsers we have to use document |
104 | | - const crossBrowserRoot = button.shadowRoot.elementFromPoint ? button.shadowRoot : document; |
105 | | - const shadowClickAreaElement = crossBrowserRoot.elementFromPoint(left, top); |
106 | | - shadowClickAreaElement.click(); |
| 113 | + getTopElement(button).click(); |
107 | 114 |
|
108 | 115 | expect(formSubmitSpy.called).to.be.true; |
109 | 116 | }); |
@@ -138,4 +145,62 @@ describe('lion-button', () => { |
138 | 145 | expect(formSubmitSpy.called).to.be.true; |
139 | 146 | }); |
140 | 147 | }); |
| 148 | + |
| 149 | + describe('click event', () => { |
| 150 | + it('is fired once', async () => { |
| 151 | + const clickSpy = sinon.spy(); |
| 152 | + const el = await fixture( |
| 153 | + html` |
| 154 | + <lion-button @click="${clickSpy}"></lion-button> |
| 155 | + `, |
| 156 | + ); |
| 157 | + |
| 158 | + getTopElement(el).click(); |
| 159 | + |
| 160 | + // trying to wait for other possible redispatched events |
| 161 | + await aTimeout(); |
| 162 | + await aTimeout(); |
| 163 | + |
| 164 | + expect(clickSpy.callCount).to.equal(1); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('event after redispatching', async () => { |
| 168 | + async function prepareClickEvent(el, host) { |
| 169 | + setTimeout(() => { |
| 170 | + if (host) { |
| 171 | + // click on host like in native button |
| 172 | + makeMouseEvent('click', { x: 11, y: 11 }, el); |
| 173 | + } else { |
| 174 | + // click on click-area which is then redispatched |
| 175 | + makeMouseEvent('click', { x: 11, y: 11 }, getTopElement(el)); |
| 176 | + } |
| 177 | + }); |
| 178 | + return oneEvent(el, 'click'); |
| 179 | + } |
| 180 | + |
| 181 | + let hostEvent; |
| 182 | + let redispatchedEvent; |
| 183 | + |
| 184 | + before(async () => { |
| 185 | + const el = await fixture('<lion-button></lion-button>'); |
| 186 | + hostEvent = await prepareClickEvent(el, true); |
| 187 | + redispatchedEvent = await prepareClickEvent(el, false); |
| 188 | + }); |
| 189 | + |
| 190 | + const sameProperties = [ |
| 191 | + 'constructor', |
| 192 | + 'composed', |
| 193 | + 'bubbles', |
| 194 | + 'cancelable', |
| 195 | + 'clientX', |
| 196 | + 'clientY', |
| 197 | + ]; |
| 198 | + |
| 199 | + sameProperties.forEach(property => { |
| 200 | + it(`has same value of the property "${property}"`, async () => { |
| 201 | + expect(redispatchedEvent[property]).to.equal(hostEvent[property]); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }); |
141 | 206 | }); |
0 commit comments