|
1 | 1 | import { type ScopedElementsMap, ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js'; |
2 | 2 | import { expect, fixture } from '@open-wc/testing'; |
3 | 3 | import { Button } from '@sl-design-system/button'; |
| 4 | +import { type PopoverPosition } from '@sl-design-system/shared'; |
4 | 5 | import { LitElement, type TemplateResult, html } from 'lit'; |
5 | 6 | import { spy, stub } from 'sinon'; |
6 | 7 | import { tooltip } from './tooltip-directive.js'; |
7 | 8 | import { Tooltip } from './tooltip.js'; |
8 | 9 |
|
9 | 10 | describe('tooltip()', () => { |
10 | 11 | it('should create a lazy tooltip on the host element', async () => { |
11 | | - spy(Tooltip, 'lazy'); |
| 12 | + const lazySpy = spy(Tooltip, 'lazy'); |
12 | 13 |
|
13 | 14 | const el = await fixture(html`<div ${tooltip('content')}>Host</div>`); |
14 | 15 |
|
15 | 16 | expect(Tooltip.lazy).to.have.been.calledOnce; |
16 | 17 | expect(Tooltip.lazy).to.have.been.calledWith(el); |
| 18 | + |
| 19 | + lazySpy.restore(); |
17 | 20 | }); |
18 | 21 |
|
19 | 22 | it('should log a warning if the custom element is not defined on the document', async () => { |
@@ -98,4 +101,62 @@ describe('tooltip()', () => { |
98 | 101 | // Cleanup |
99 | 102 | el.remove(); |
100 | 103 | }); |
| 104 | + |
| 105 | + it('should pass tooltip options via config to the tooltip', async () => { |
| 106 | + try { |
| 107 | + if (!customElements.get('sl-tooltip')) { |
| 108 | + customElements.define('sl-tooltip', Tooltip); |
| 109 | + } |
| 110 | + } catch { |
| 111 | + // empty |
| 112 | + } |
| 113 | + |
| 114 | + const lazySpy = spy(Tooltip, 'lazy'); |
| 115 | + |
| 116 | + const el: HTMLElement = await fixture( |
| 117 | + html`<div ${tooltip('content', { ariaRelation: 'label' })} tabindex="0">Host</div>` |
| 118 | + ); |
| 119 | + |
| 120 | + // Trigger the lazy tooltip creation |
| 121 | + el.focus(); |
| 122 | + |
| 123 | + expect(lazySpy).to.have.been.calledOnce; |
| 124 | + expect(lazySpy.getCall(0).args[2]).to.deep.equal({ ariaRelation: 'label' }); |
| 125 | + |
| 126 | + const tooltipEl = el.nextElementSibling as Tooltip | null; |
| 127 | + |
| 128 | + expect(tooltipEl).to.exist; |
| 129 | + expect(el).to.have.attribute('aria-labelledby', tooltipEl?.id); |
| 130 | + |
| 131 | + lazySpy.restore(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should apply tooltip properties (position, maxWidth) from config to the tooltip', async () => { |
| 135 | + try { |
| 136 | + if (!customElements.get('sl-tooltip')) { |
| 137 | + customElements.define('sl-tooltip', Tooltip); |
| 138 | + } |
| 139 | + } catch { |
| 140 | + // empty |
| 141 | + } |
| 142 | + |
| 143 | + const el: HTMLElement = await fixture(html` |
| 144 | + <sl-button ${tooltip('Tooltip example', { position: 'bottom' as PopoverPosition, maxWidth: 150 })} tabindex="0" |
| 145 | + >Button</sl-button |
| 146 | + > |
| 147 | + `); |
| 148 | + |
| 149 | + // Trigger the lazy tooltip creation |
| 150 | + el.focus(); |
| 151 | + |
| 152 | + const tooltipEl = el.nextElementSibling as Tooltip | null; |
| 153 | + |
| 154 | + expect(tooltipEl).to.exist; |
| 155 | + expect(tooltipEl?.position).to.equal('bottom'); |
| 156 | + expect(tooltipEl?.maxWidth).to.equal(150); |
| 157 | + expect(tooltipEl).to.have.text('Tooltip example'); |
| 158 | + |
| 159 | + // Cleanup |
| 160 | + el.remove(); |
| 161 | + }); |
101 | 162 | }); |
0 commit comments