|
| 1 | +import { elementUpdated, expect, fixture, html } from '@open-wc/testing'; |
| 2 | +import { IgcProgressBaseComponent } from './base.js'; |
| 3 | + |
| 4 | +class TestProgressBaseComponent extends IgcProgressBaseComponent { |
| 5 | + protected override render() { |
| 6 | + return html`<div part="test">${this.renderDefaultSlot()}</div>`; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +customElements.define('test-progress-base', TestProgressBaseComponent); |
| 11 | + |
| 12 | +describe('IgcProgressBaseComponent', () => { |
| 13 | + let component: TestProgressBaseComponent; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + component = await fixture<TestProgressBaseComponent>( |
| 17 | + html`<test-progress-base></test-progress-base>` |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('Shared Logic', () => { |
| 22 | + it('clamps value to max and min correctly', async () => { |
| 23 | + component.value = 200; // Exceeds max |
| 24 | + component.max = 100; |
| 25 | + await elementUpdated(component); |
| 26 | + |
| 27 | + expect(component.value).to.equal(100); // Value clamped to max |
| 28 | + |
| 29 | + component.value = -50; // Below min |
| 30 | + await elementUpdated(component); |
| 31 | + |
| 32 | + expect(component.value).to.equal(0); // Value clamped to min |
| 33 | + }); |
| 34 | + |
| 35 | + it('updates value when max is reduced below current value', async () => { |
| 36 | + component.value = 75; |
| 37 | + component.max = 50; |
| 38 | + await elementUpdated(component); |
| 39 | + |
| 40 | + expect(component.value).to.equal(50); // Adjusted to max |
| 41 | + }); |
| 42 | + |
| 43 | + it('does not update value when max is increased', async () => { |
| 44 | + component.value = 50; |
| 45 | + component.max = 150; |
| 46 | + await elementUpdated(component); |
| 47 | + |
| 48 | + expect(component.value).to.equal(50); // Remains unchanged |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not update ARIA attributes when indeterminate is true', async () => { |
| 52 | + component.indeterminate = true; |
| 53 | + await elementUpdated(component); |
| 54 | + |
| 55 | + expect(component.getAttribute('aria-valuenow')).to.be.null; |
| 56 | + expect(component.getAttribute('aria-valuetext')).to.be.null; |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('Lifecycle Behavior', () => { |
| 61 | + it('correctly sets ARIA attributes on connectedCallback', async () => { |
| 62 | + const element = document.createElement('test-progress-base'); |
| 63 | + document.body.appendChild(element); |
| 64 | + |
| 65 | + await elementUpdated(element); |
| 66 | + |
| 67 | + expect(element.getAttribute('aria-valuenow')).to.equal('0'); |
| 68 | + expect(element.getAttribute('aria-valuemax')).to.equal('100'); |
| 69 | + |
| 70 | + document.body.removeChild(element); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('CSS Variables', () => { |
| 75 | + it('updates CSS variables correctly based on value and max', async () => { |
| 76 | + component.value = 50; |
| 77 | + component.max = 200; |
| 78 | + await elementUpdated(component); |
| 79 | + |
| 80 | + const styles = getComputedStyle(component); |
| 81 | + expect(styles.getPropertyValue('--_progress-whole').trim()).to.equal( |
| 82 | + '25' |
| 83 | + ); // 50 / 200 * 100 |
| 84 | + expect(styles.getPropertyValue('--_progress-integer').trim()).to.equal( |
| 85 | + '25' |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('Slots and Rendering', () => { |
| 91 | + it('renders the default slot', async () => { |
| 92 | + const slot = component.shadowRoot?.querySelector('slot'); |
| 93 | + expect(slot).to.exist; |
| 94 | + }); |
| 95 | + |
| 96 | + it('hides the label when hideLabel is true', async () => { |
| 97 | + component.hideLabel = true; |
| 98 | + await elementUpdated(component); |
| 99 | + |
| 100 | + const label = component.shadowRoot?.querySelector('[part~="label"]'); |
| 101 | + expect(label).to.be.null; |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments