Skip to content

Commit 26f4ae4

Browse files
committed
refactor(progressbar): fix label format not working, rewrite some of the tests and add new ones.
1 parent c15dfb4 commit 26f4ae4

File tree

7 files changed

+194
-436
lines changed

7 files changed

+194
-436
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
});

src/components/progress/base.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ export abstract class IgcProgressBaseComponent extends LitElement {
188188
const basePart = 'label value';
189189
const part = `${basePart}${this.hasFraction ? ' fraction' : ''}`.trim();
190190

191-
return html`<span part=${part}></span>`;
191+
if (this.labelFormat) {
192+
return html` <span part=${part}>${this.renderLabelFormat()}</span> `;
193+
}
194+
195+
// Default behavior: Render empty span (CSS handles everything)
196+
return html`<span part="${part} counter"></span>`;
192197
}
193198

194199
protected renderLabelFormat() {

0 commit comments

Comments
 (0)