import { expect, fixture } from '@open-wc/testing';
import { html } from 'lit/static-html.js';
import sinon from 'sinon';
// eslint-disable-next-line no-unused-vars
import { LionOption } from '@lion/ui/listbox.js';
import '@lion/ui/define/lion-option.js';
describe('lion-option', () => {
describe('Values', () => {
it('has a modelValue', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.modelValue).to.deep.equal({ value: 10, checked: false });
});
it('fires model-value-changed on click', async () => {
let isTriggeredByUser;
const el = /** @type {LionOption} */ (
await fixture(html`
{
isTriggeredByUser = event.detail.isTriggeredByUser;
}}"
>
`)
);
el.dispatchEvent(new CustomEvent('click', { bubbles: true }));
expect(isTriggeredByUser).to.be.true;
});
it('fires model-value-changed on programmatic "checked" change', async () => {
let count = 0;
let isTriggeredByUser;
const el = /** @type {LionOption} */ (
await fixture(html`
{
count += 1;
isTriggeredByUser = event.detail.isTriggeredByUser;
}}"
>
`)
);
el.checked = true;
expect(count).to.equal(1);
expect(isTriggeredByUser).to.be.false;
});
it('can be checked', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.modelValue).to.deep.equal({ value: 10, checked: true });
});
it('is hidden when attribute hidden is true', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el).not.to.be.displayed;
});
});
describe('Accessibility', () => {
it('has the "option" role', async () => {
const el = /** @type {LionOption} */ (await fixture(html``));
expect(el.getAttribute('role')).to.equal('option');
});
it('has "aria-selected" attribute when checked', async () => {
const el = /** @type {LionOption} */ (
await fixture(html` Item 1 `)
);
expect(el.getAttribute('aria-selected')).to.equal('true');
el.checked = false;
// check that dom update is async
expect(el.getAttribute('aria-selected')).to.equal('true');
await el.updateComplete;
expect(el.getAttribute('aria-selected')).to.equal('false');
});
it('asynchronously adds the attributes "aria-disabled" and "disabled" when disabled', async () => {
const el = /** @type {LionOption} */ (
await fixture(html` Item 1 `)
);
expect(el.getAttribute('aria-disabled')).to.equal('true');
expect(el.hasAttribute('disabled')).to.be.true;
el.disabled = false;
expect(el.getAttribute('aria-disabled')).to.equal('true');
expect(el.hasAttribute('disabled')).to.be.true;
await el.updateComplete;
expect(el.getAttribute('aria-disabled')).to.equal('false');
expect(el.hasAttribute('disabled')).to.be.false;
});
});
describe('State reflection', () => {
it('asynchronously adds the attribute "active" when active', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.active).to.equal(false);
expect(el.hasAttribute('active')).to.be.false;
el.active = true;
expect(el.active).to.be.true;
expect(el.hasAttribute('active')).to.be.false;
await el.updateComplete;
expect(el.hasAttribute('active')).to.be.true;
el.active = false;
expect(el.active).to.be.false;
expect(el.hasAttribute('active')).to.be.true;
await el.updateComplete;
expect(el.hasAttribute('active')).to.be.false;
});
it('does become checked and active on [click]', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.checked).to.be.false;
expect(el.active).to.be.false;
el.click();
await el.updateComplete;
expect(el.checked).to.be.true;
expect(el.active).to.be.true;
});
it('fires active-changed event', async () => {
const activeSpy = sinon.spy();
const el = /** @type {LionOption} */ (
await fixture(html`
`)
);
expect(activeSpy.callCount).to.equal(0);
el.active = true;
expect(activeSpy.callCount).to.equal(1);
});
});
describe('Disabled', () => {
it('does not becomes active on [mouseenter]', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.active).to.be.false;
el.dispatchEvent(new Event('mouseenter'));
expect(el.active).to.be.false;
});
it('does not become checked on [click]', async () => {
const el = /** @type {LionOption} */ (
await fixture(html``)
);
expect(el.checked).to.be.false;
el.click();
await el.updateComplete;
expect(el.checked).to.be.false;
});
it('does not become un-active on [mouseleave]', async () => {
const el = /** @type {LionOption} */ (
await fixture(html` `)
);
expect(el.active).to.be.true;
el.dispatchEvent(new Event('mouseleave'));
expect(el.active).to.be.true;
});
});
});