2022-06-28 21:59:52 +00:00
|
|
|
import { expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing';
|
2021-06-18 12:34:42 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import type SlColorPicker from './color-picker';
|
|
|
|
|
|
|
|
describe('<sl-color-picker>', () => {
|
|
|
|
it('should emit change and show correct color when the value changes', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker></sl-color-picker> `);
|
2022-11-17 14:35:44 +00:00
|
|
|
const trigger = el.shadowRoot!.querySelector<HTMLElement>('[part~="trigger"]')!;
|
2021-06-18 12:34:42 +00:00
|
|
|
const changeHandler = sinon.spy();
|
|
|
|
const color = 'rgb(255, 204, 0)';
|
|
|
|
|
|
|
|
el.addEventListener('sl-change', changeHandler);
|
|
|
|
el.value = color;
|
|
|
|
|
|
|
|
await waitUntil(() => changeHandler.calledOnce);
|
|
|
|
|
|
|
|
expect(changeHandler).to.have.been.calledOnce;
|
|
|
|
expect(trigger.style.color).to.equal(color);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render in a dropdown', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker></sl-color-picker> `);
|
2022-01-16 05:47:14 +00:00
|
|
|
const dropdown = el.shadowRoot!.querySelector('sl-dropdown');
|
2021-06-18 12:34:42 +00:00
|
|
|
|
|
|
|
expect(dropdown).to.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not render in a dropdown when inline is enabled', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker inline></sl-color-picker> `);
|
2022-01-16 05:47:14 +00:00
|
|
|
const dropdown = el.shadowRoot!.querySelector('sl-dropdown');
|
2021-06-18 12:34:42 +00:00
|
|
|
|
|
|
|
expect(dropdown).to.not.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show opacity slider when opacity is enabled', async () => {
|
2021-08-10 21:11:07 +00:00
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker opacity></sl-color-picker> `);
|
2022-01-16 05:47:14 +00:00
|
|
|
const opacitySlider = el.shadowRoot!.querySelector('[part*="opacity-slider"]')!;
|
2021-06-18 12:34:42 +00:00
|
|
|
|
|
|
|
expect(opacitySlider).to.exist;
|
|
|
|
});
|
2022-03-14 14:11:55 +00:00
|
|
|
|
|
|
|
it('should display a color when an initial value is provided', async () => {
|
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker value="#000"></sl-color-picker> `);
|
2022-11-17 14:35:44 +00:00
|
|
|
const trigger = el.shadowRoot!.querySelector<HTMLButtonElement>('[part~="trigger"]');
|
2022-03-14 14:11:55 +00:00
|
|
|
|
|
|
|
expect(trigger?.style.color).to.equal('rgb(0, 0, 0)');
|
|
|
|
});
|
2022-06-28 21:59:52 +00:00
|
|
|
|
2022-08-25 13:00:52 +00:00
|
|
|
it('should display a color with opacity when an initial value with opacity is provided', async () => {
|
|
|
|
const el = await fixture<SlColorPicker>(html` <sl-color-picker opacity value="#ff000050"></sl-color-picker> `);
|
2022-11-17 14:35:44 +00:00
|
|
|
const trigger = el.shadowRoot!.querySelector<HTMLButtonElement>('[part~="trigger"]');
|
|
|
|
const previewButton = el.shadowRoot!.querySelector<HTMLButtonElement>('[part~="preview"]');
|
2022-08-25 13:00:52 +00:00
|
|
|
const previewColor = getComputedStyle(previewButton!).getPropertyValue('--preview-color');
|
|
|
|
|
|
|
|
expect(trigger!.style.color).to.equal('rgba(255, 0, 0, 0.314)');
|
|
|
|
expect(previewColor.startsWith('hsla(0deg, 100%, 50%, 0.31')).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-06-28 21:59:52 +00:00
|
|
|
describe('when resetting a form', () => {
|
|
|
|
it('should reset the element to its initial value', async () => {
|
|
|
|
const form = await fixture<HTMLFormElement>(html`
|
|
|
|
<form>
|
|
|
|
<sl-color-picker name="a" value="#FFFFFF"></sl-color-picker>
|
|
|
|
<sl-button type="reset">Reset</sl-button>
|
|
|
|
</form>
|
|
|
|
`);
|
|
|
|
const button = form.querySelector('sl-button')!;
|
|
|
|
const colorPicker = form.querySelector('sl-color-picker')!;
|
|
|
|
colorPicker.value = '#000000';
|
|
|
|
|
|
|
|
await colorPicker.updateComplete;
|
|
|
|
|
|
|
|
setTimeout(() => button.click());
|
|
|
|
await oneEvent(form, 'reset');
|
|
|
|
await colorPicker.updateComplete;
|
|
|
|
|
|
|
|
expect(colorPicker.value).to.equal('#FFFFFF');
|
|
|
|
|
|
|
|
colorPicker.defaultValue = '';
|
|
|
|
|
|
|
|
setTimeout(() => button.click());
|
|
|
|
await oneEvent(form, 'reset');
|
|
|
|
await colorPicker.updateComplete;
|
|
|
|
|
|
|
|
expect(colorPicker.value).to.equal('');
|
|
|
|
});
|
|
|
|
});
|
2021-06-18 12:34:42 +00:00
|
|
|
});
|