Skip to content

Commit

Permalink
test: update ResizeMixin tests to use nextResize helper
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Feb 10, 2025
1 parent 2511edd commit 0f54578
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions packages/component-base/test/resize-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import sinon from 'sinon';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { ResizeMixin } from '../src/resize-mixin.js';

const nextResize = (target) => {
return new Promise((resolve) => {
new ResizeObserver((_, observer) => {
observer.disconnect();
setTimeout(resolve);
}).observe(target);
});
};

describe('resize-mixin', () => {
let element, observeParent;

Expand All @@ -25,29 +34,21 @@ describe('resize-mixin', () => {
get _observeParent() {
return observeParent;
}

_onResize() {
this.__resolveResize?.();
}

nextResize() {
return new Promise((resolve) => {
this.__resolveResize = resolve;
});
}
},
);
});

beforeEach(async () => {
element = fixtureSync(`<resize-mixin-element></resize-mixin-element>`);
// Wait for the initial resize.
await element.nextResize();
await nextResize(element);
});

it('should notify resize', async () => {
const spy = sinon.spy(element, '_onResize');
element.style.width = '100px';
await element.nextResize();
await nextResize(element);
expect(spy.calledOnce).to.be.true;
});

it('should not notify resize for detached element', async () => {
Expand Down Expand Up @@ -86,12 +87,14 @@ describe('resize-mixin', () => {
beforeEach(async () => {
parent.appendChild(element);
// Wait for the initial resize.
await element.nextResize();
await nextResize(parent);
});

it('should notify parent element resize', async () => {
const spy = sinon.spy(element, '_onResize');
parent.style.width = '100px';
await element.nextResize();
await nextResize(parent);
expect(spy.calledOnce).to.be.true;
});

describe('multiple children', () => {
Expand Down Expand Up @@ -131,12 +134,14 @@ describe('resize-mixin', () => {
parent.attachShadow({ mode: 'open' });
parent.shadowRoot.appendChild(element);
// Wait for the initial resize.
await element.nextResize();
await nextResize(parent);
});

it('should notify shadow host resize', async () => {
const spy = sinon.spy(element, '_onResize');
parent.style.width = '100px';
await element.nextResize();
await nextResize(parent);
expect(spy.calledOnce).to.be.true;
});
});
});
Expand Down

0 comments on commit 0f54578

Please sign in to comment.