|
1 | 1 | import { expect } from '@esm-bundle/chai'; |
2 | | -import { arrowRight, enter, fixtureSync, listenOnce, nextRender, space } from '@vaadin/testing-helpers'; |
| 2 | +import { arrowRight, aTimeout, enter, fixtureSync, listenOnce, nextFrame, space } from '@vaadin/testing-helpers'; |
3 | 3 | import sinon from 'sinon'; |
4 | 4 | import '../vaadin-tabs.js'; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Resolves once the function is invoked on the given object. |
| 8 | + */ |
| 9 | +function onceInvoked(object, functionName) { |
| 10 | + return new Promise((resolve) => { |
| 11 | + sinon.replace(object, functionName, (...args) => { |
| 12 | + sinon.restore(); |
| 13 | + object[functionName](...args); |
| 14 | + resolve(); |
| 15 | + }); |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Resolves once the ResizeObserver has processed a resize. |
| 21 | + */ |
| 22 | +async function onceResized(tabs) { |
| 23 | + await onceInvoked(tabs, '_updateOverflow'); |
| 24 | +} |
| 25 | + |
6 | 26 | describe('tabs', () => { |
7 | 27 | let tabs; |
8 | 28 |
|
@@ -44,6 +64,23 @@ describe('tabs', () => { |
44 | 64 | expect(item.tagName.toLowerCase()).to.equal('vaadin-tab'); |
45 | 65 | }); |
46 | 66 | }); |
| 67 | + |
| 68 | + it('should not resize on detached item resize', async () => { |
| 69 | + // Remove a tab |
| 70 | + const item = tabs.items[0]; |
| 71 | + document.body.append(item); |
| 72 | + await aTimeout(100); |
| 73 | + |
| 74 | + // Resize the removed tab |
| 75 | + const stub = sinon.stub(tabs, '_updateOverflow'); |
| 76 | + item.style.width = '100px'; |
| 77 | + await aTimeout(100); |
| 78 | + |
| 79 | + item.remove(); |
| 80 | + // Expect the resizeobserver not to have been invoked on the |
| 81 | + // removed tab resize |
| 82 | + expect(stub.called).to.be.false; |
| 83 | + }); |
47 | 84 | }); |
48 | 85 |
|
49 | 86 | ['horizontal', 'vertical'].forEach((orientation) => { |
@@ -73,8 +110,8 @@ describe('tabs', () => { |
73 | 110 | } else { |
74 | 111 | tabs.style.height = '100px'; |
75 | 112 | } |
76 | | - tabs._updateOverflow(); |
77 | | - await nextRender(tabs); |
| 113 | + await onceResized(tabs); |
| 114 | + await nextFrame(); |
78 | 115 | }); |
79 | 116 |
|
80 | 117 | it(`when orientation=${orientation} should have overflow="end" if scroll is at the beginning`, () => { |
@@ -121,6 +158,30 @@ describe('tabs', () => { |
121 | 158 |
|
122 | 159 | expect(tabs.getAttribute('overflow')).to.be.equal('end'); |
123 | 160 | }); |
| 161 | + |
| 162 | + it('should update overflow on resize', async () => { |
| 163 | + tabs.style.width = 'auto'; |
| 164 | + tabs.style.height = 'auto'; |
| 165 | + await onceResized(tabs); |
| 166 | + expect(tabs.hasAttribute('overflow')).to.be.false; |
| 167 | + }); |
| 168 | + |
| 169 | + it('should update overflow on item resize', async () => { |
| 170 | + tabs.items.forEach((item) => { |
| 171 | + item.style.height = '1px'; |
| 172 | + item.style.width = '1px'; |
| 173 | + item.style.minHeight = '1px'; |
| 174 | + item.style.minWidth = '1px'; |
| 175 | + }); |
| 176 | + await onceResized(tabs); |
| 177 | + expect(tabs.hasAttribute('overflow')).to.be.false; |
| 178 | + }); |
| 179 | + |
| 180 | + it('should update overflow on items change', async () => { |
| 181 | + tabs.items.forEach((item) => item.remove()); |
| 182 | + await nextFrame(); |
| 183 | + expect(tabs.hasAttribute('overflow')).to.be.false; |
| 184 | + }); |
124 | 185 | }); |
125 | 186 | }); |
126 | 187 | }); |
|
0 commit comments