|
1 | 1 | import { expect } from '@esm-bundle/chai'; |
2 | | -import { fixtureSync, nextFrame } from '@vaadin/testing-helpers'; |
| 2 | +import { fixtureSync, focusin, focusout, nextFrame } from '@vaadin/testing-helpers'; |
3 | 3 | import sinon from 'sinon'; |
4 | 4 | import { html, PolymerElement } from '@polymer/polymer/polymer-element.js'; |
5 | 5 | import { DelegateFocusMixin } from '../src/delegate-focus-mixin.js'; |
6 | 6 |
|
7 | | -customElements.define( |
8 | | - 'delegate-focus-mixin-element', |
9 | | - class extends DelegateFocusMixin(PolymerElement) { |
10 | | - static get template() { |
11 | | - return html`<slot name="input"></slot>`; |
12 | | - } |
13 | | - |
14 | | - /** @protected */ |
15 | | - ready() { |
16 | | - super.ready(); |
17 | | - |
18 | | - const input = this.querySelector('input'); |
19 | | - this._setFocusElement(input); |
20 | | - } |
21 | | - }, |
22 | | -); |
23 | | - |
24 | 7 | describe('delegate-focus-mixin', () => { |
25 | | - let element, input; |
| 8 | + let element, input, setFocusedSpy; |
| 9 | + |
| 10 | + customElements.define( |
| 11 | + 'delegate-focus-mixin-element', |
| 12 | + class extends DelegateFocusMixin(PolymerElement) { |
| 13 | + static get template() { |
| 14 | + return html` |
| 15 | + <slot name="input"></slot> |
| 16 | + <slot name="suffix"></slot> |
| 17 | + `; |
| 18 | + } |
| 19 | + |
| 20 | + /** @protected */ |
| 21 | + ready() { |
| 22 | + super.ready(); |
| 23 | + |
| 24 | + const input = this.querySelector('input'); |
| 25 | + this._setFocusElement(input); |
| 26 | + } |
| 27 | + |
| 28 | + _setFocused(focused) { |
| 29 | + super._setFocused(focused); |
| 30 | + setFocusedSpy?.(focused); |
| 31 | + } |
| 32 | + }, |
| 33 | + ); |
26 | 34 |
|
27 | 35 | describe('default', () => { |
| 36 | + let button; |
| 37 | + |
28 | 38 | beforeEach(() => { |
| 39 | + setFocusedSpy = sinon.spy(); |
29 | 40 | element = fixtureSync(` |
30 | 41 | <delegate-focus-mixin-element> |
31 | 42 | <input slot="input" /> |
| 43 | + <button slot="suffix"></button> |
32 | 44 | </delegate-focus-mixin-element> |
33 | 45 | `); |
34 | 46 | input = element.querySelector('input'); |
| 47 | + button = element.querySelector('button'); |
35 | 48 | }); |
36 | 49 |
|
37 | 50 | it('should focus the input on focus call', () => { |
@@ -113,6 +126,16 @@ describe('delegate-focus-mixin', () => { |
113 | 126 | element._setFocusElement(target); |
114 | 127 | expect(target.disabled).to.be.false; |
115 | 128 | }); |
| 129 | + |
| 130 | + it('should not invoke setFocused when focusin is not triggered by focusElement', () => { |
| 131 | + focusin(button); |
| 132 | + expect(setFocusedSpy.called).to.be.false; |
| 133 | + }); |
| 134 | + |
| 135 | + it('should not invoke setFocused when focusout is not triggered by focusElement', () => { |
| 136 | + focusout(button); |
| 137 | + expect(setFocusedSpy.called).to.be.false; |
| 138 | + }); |
116 | 139 | }); |
117 | 140 |
|
118 | 141 | describe('events', () => { |
|
0 commit comments