import '../../../dist/zn.min.js';
import {expect, fixture, html, waitUntil} from '@open-wc/testing';
import type ZnThumbnail from '../thumbnail/thumbnail.component';
import type ZnThumbnailGroup from './thumbnail-group.component';
const thumbnails = (count: number) =>
Array.from({length: count}, (_, i) => ``).join('');
describe('', () => {
it('should render a component', async () => {
const el = await fixture(html`
`);
expect(el).to.exist;
});
it('should render the caption', async () => {
const el = await fixture(html`
`);
expect(el.shadowRoot!.querySelector('.thumbnail-group__caption')!.textContent).to.contain('Landscape');
});
it('should not render the toggle when the thumbnails fit on one row', async () => {
const el = await fixture(html`
`);
expect(el.shadowRoot!.querySelector('.thumbnail-group__toggle')).to.not.exist;
});
it('should render the toggle when the row overflows', async () => {
const el = await fixture(
`${thumbnails(12)}`);
await waitUntil(() => el.shadowRoot!.querySelector('.thumbnail-group__toggle'));
const toggle = el.shadowRoot!.querySelector('.thumbnail-group__toggle')!;
expect(toggle.textContent!.trim()).to.equal('Show All (12)');
});
it('should use the total attribute in the toggle label', async () => {
const el = await fixture(html`
`);
const toggle = el.shadowRoot!.querySelector('.thumbnail-group__toggle')!;
expect(toggle.textContent!.trim()).to.equal('Show All (79)');
});
it('should expand and collapse from the toggle', async () => {
const el = await fixture(html`
`);
const expanded: string[] = [];
el.addEventListener('zn-expand', () => expanded.push('expand'));
el.addEventListener('zn-collapse', () => expanded.push('collapse'));
el.shadowRoot!.querySelector('.thumbnail-group__toggle')!.click();
await el.updateComplete;
expect(el.expanded).to.be.true;
expect(el.shadowRoot!.querySelector('.thumbnail-group__toggle')!.textContent!.trim())
.to.equal('Show Less');
el.shadowRoot!.querySelector('.thumbnail-group__toggle')!.click();
await el.updateComplete;
expect(el.expanded).to.be.false;
expect(expanded).to.deep.equal(['expand', 'collapse']);
});
it('should move selection between thumbnails when selectable', async () => {
const el = await fixture(html`
`);
const [a, b] = Array.from(el.querySelectorAll('zn-thumbnail'));
expect(el.value).to.equal('a');
let changed = false;
el.addEventListener('zn-change', () => (changed = true));
b.shadowRoot!.querySelector('.thumbnail__link')!.click();
await el.updateComplete;
expect(el.value).to.equal('b');
expect(a.selected).to.be.false;
expect(b.selected).to.be.true;
expect(changed).to.be.true;
});
it('should mirror the shorthand attributes onto host custom properties', async () => {
const el = await fixture(html`
`);
expect(el.style.getPropertyValue('--zn-thumbnail-aspect-ratio')).to.equal('1 / 1');
expect(el.style.getPropertyValue('--zn-thumbnail-width')).to.equal('120px');
expect(el.style.getPropertyValue('--zn-thumbnail-gap')).to.equal('4px');
// Thumbnails are light-DOM children, so they inherit the properties from the host.
const thumbnail = el.querySelector('zn-thumbnail')!;
const rect = thumbnail.shadowRoot!.querySelector('.thumbnail__media')!.getBoundingClientRect();
expect(rect.width).to.be.closeTo(120, 1);
expect(rect.height).to.be.closeTo(120, 1);
});
it('should not clear a custom property set inline by the consumer', async () => {
const el = await fixture(html`
`);
expect(el.style.getPropertyValue('--zn-thumbnail-width')).to.equal('90px');
});
it('should clear a shorthand property once its attribute is removed', async () => {
const el = await fixture(html`
`);
el.thumbnailWidth = '';
await el.updateComplete;
expect(el.style.getPropertyValue('--zn-thumbnail-width')).to.equal('');
});
it('should not manage selection when selectable is not set', async () => {
const el = await fixture(html`
`);
const a = el.querySelector('zn-thumbnail')!;
a.shadowRoot!.querySelector('.thumbnail__link')!.click();
await el.updateComplete;
expect(a.selected).to.be.false;
expect(el.value).to.equal('');
});
});