Skip to content

Commit 51ad30e

Browse files
committed
Add tests
1 parent 8f9fb09 commit 51ad30e

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

packages/react/src/progress/root/ProgressRoot.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expect } from 'chai';
21
import * as React from 'react';
2+
import { expect } from 'chai';
33
import { Progress } from '@base-ui-components/react/progress';
44
import { createRenderer, describeConformance } from '#test-utils';
55
import type { ProgressRoot } from './ProgressRoot';
@@ -61,4 +61,28 @@ describe('<Progress.Root />', () => {
6161
expect(progressbar).to.have.attribute('aria-valuenow', '77');
6262
});
6363
});
64+
65+
describe('prop: format', () => {
66+
it('formats the value', async () => {
67+
const format: Intl.NumberFormatOptions = {
68+
style: 'currency',
69+
currency: 'USD',
70+
};
71+
function formatValue(v: number) {
72+
return new Intl.NumberFormat(undefined, format).format(v);
73+
}
74+
const { getByRole, getByTestId } = await render(
75+
<Progress.Root value={30} format={format}>
76+
<Progress.Value data-testid="value" />
77+
<Progress.Track>
78+
<Progress.Indicator />
79+
</Progress.Track>
80+
</Progress.Root>,
81+
);
82+
const value = getByTestId('value');
83+
const progressbar = getByRole('progressbar');
84+
expect(value).to.have.text(formatValue(30));
85+
expect(progressbar).to.have.attribute('aria-valuetext', formatValue(30));
86+
});
87+
});
6488
});

packages/react/src/progress/value/ProgressValue.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as React from 'react';
2+
import { expect } from 'chai';
3+
import { spy } from 'sinon';
24
import { Progress } from '@base-ui-components/react/progress';
35
import { createRenderer, describeConformance } from '#test-utils';
46
import { ProgressRootContext } from '../root/ProgressRootContext';
@@ -27,4 +29,51 @@ describe('<Progress.Value />', () => {
2729
},
2830
refInstanceof: window.HTMLSpanElement,
2931
}));
32+
33+
describe('prop: children', () => {
34+
it('renders the value when children is not provided', async () => {
35+
const { getByTestId } = await render(
36+
<Progress.Root value={30}>
37+
<Progress.Value data-testid="value" />
38+
</Progress.Root>,
39+
);
40+
const value = getByTestId('value');
41+
expect(value).to.have.text('30%');
42+
});
43+
44+
it('renders a formatted value when a format is provided', async () => {
45+
const format: Intl.NumberFormatOptions = {
46+
style: 'currency',
47+
currency: 'USD',
48+
};
49+
function formatValue(v: number) {
50+
return new Intl.NumberFormat(undefined, format).format(v);
51+
}
52+
const { getByTestId } = await render(
53+
<Progress.Root value={30} format={format}>
54+
<Progress.Value data-testid="value" />
55+
</Progress.Root>,
56+
);
57+
const value = getByTestId('value');
58+
expect(value).to.have.text(formatValue(30));
59+
});
60+
61+
it('accepts a render function', async () => {
62+
const renderSpy = spy();
63+
const format: Intl.NumberFormatOptions = {
64+
style: 'currency',
65+
currency: 'USD',
66+
};
67+
function formatValue(v: number) {
68+
return new Intl.NumberFormat(undefined, format).format(v);
69+
}
70+
await render(
71+
<Progress.Root value={30} format={format}>
72+
<Progress.Value data-testid="value">{renderSpy}</Progress.Value>
73+
</Progress.Root>,
74+
);
75+
expect(renderSpy.lastCall.args[0]).to.deep.equal(formatValue(30));
76+
expect(renderSpy.lastCall.args[1]).to.deep.equal(30);
77+
});
78+
});
3079
});

0 commit comments

Comments
 (0)