Skip to content

Commit 5d1b860

Browse files
authored
test(list): unit tests and snapshots for ordered and unordered list (#19310)
* test(ordered-list): unit tests and snapshots * test(unordered-list): unit tests and snapshot
1 parent 75b4831 commit 5d1b860

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

packages/react/src/components/UnorderedList/UnorderedList-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ describe('UnorderedList', () => {
4646

4747
expect(screen.getByTestId('list')).toHaveClass('some-class');
4848
});
49+
50+
it('should render expressive lists', () => {
51+
const { container } = render(
52+
<UnorderedList isExpressive>
53+
<ListItem>Item</ListItem>
54+
</UnorderedList>
55+
);
56+
57+
expect(container.firstChild).toHaveClass(`${prefix}--list--unordered`);
58+
expect(container.firstChild).toHaveClass(`${prefix}--list--expressive`);
59+
});
4960
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots['cds-ordered-list Renders as expected should be an ol element'] =
5+
`<cds-ordered-list>
6+
<cds-list-item role="listitem">
7+
Item
8+
</cds-list-item>
9+
</cds-ordered-list>
10+
`;
11+
/* end snapshot cds-ordered-list Renders as expected should be an ol element */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots['cds-unordered-list should be an ul element'] = `<cds-unordered-list>
5+
<cds-list-item role="listitem">
6+
Item
7+
</cds-list-item>
8+
</cds-unordered-list>
9+
`;
10+
/* end snapshot cds-unordered-list should be an ul element */
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright IBM Corp. 2025, 2025
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
import { expect, fixture, html } from '@open-wc/testing';
8+
import '@carbon/web-components/es/components/list/index.js';
9+
10+
describe('cds-ordered-list', function () {
11+
describe('Renders as expected', () => {
12+
it('should be an ol element', async () => {
13+
const list = html` <cds-ordered-list>
14+
<cds-list-item>Item</cds-list-item>
15+
</cds-ordered-list>`;
16+
17+
const el = await fixture(list);
18+
const ol = el.shadowRoot.querySelector('ol');
19+
expect(ol).to.exist;
20+
21+
expect(el).dom.to.equalSnapshot();
22+
});
23+
24+
it('should render children as expected', async () => {
25+
const list = html` <cds-ordered-list>
26+
<cds-list-item>Item 1</cds-list-item>
27+
</cds-ordered-list>`;
28+
29+
const el = await fixture(list);
30+
31+
const slot = el.shadowRoot.querySelector('slot');
32+
const assigned = slot.assignedNodes({ flatten: true });
33+
34+
const listItem = assigned.find(
35+
(node) =>
36+
node.nodeType === Node.ELEMENT_NODE &&
37+
node.tagName.toLowerCase() === 'cds-list-item'
38+
);
39+
40+
expect(listItem).to.exist;
41+
expect(listItem.getAttribute('role')).to.equal('listitem');
42+
expect(listItem.textContent.trim()).to.equal('Item 1');
43+
});
44+
45+
it('should render nested lists', async () => {
46+
const list = html` <cds-ordered-list>
47+
<cds-list-item>
48+
Ordered List level 1
49+
<cds-ordered-list>
50+
<cds-list-item>Ordered List level 2</cds-list-item>
51+
<cds-list-item>Ordered List level 2</cds-list-item>
52+
</cds-ordered-list>
53+
</cds-list-item>
54+
</cds-ordered-list>`;
55+
56+
const el = await fixture(list);
57+
58+
const ol = el.shadowRoot.querySelector('ol');
59+
expect(ol.classList.contains('cds--list--nested')).to.be.false;
60+
61+
const nested = document.querySelector('cds-ordered-list[slot="nested"');
62+
const nestedOl = nested.shadowRoot.querySelector('ol');
63+
64+
expect(nestedOl.classList.contains('cds--list--nested')).to.be.true;
65+
expect(nestedOl.classList.contains('cds--list--ordered')).to.be.true;
66+
});
67+
68+
it('should render native lists', async () => {
69+
const list = html` <cds-ordered-list native>
70+
<cds-list-item>Item</cds-list-item>
71+
</cds-ordered-list>`;
72+
73+
const el = await fixture(list);
74+
75+
const ol = el.shadowRoot.querySelector('ol');
76+
expect(ol.classList.contains('cds--list--ordered--native')).to.be.true;
77+
});
78+
79+
it('should render expressive lists', async () => {
80+
const list = html` <cds-ordered-list is-expressive>
81+
<cds-list-item>Item</cds-list-item>
82+
</cds-ordered-list>`;
83+
84+
const el = await fixture(list);
85+
86+
const ol = el.shadowRoot.querySelector('ol');
87+
expect(ol.classList.contains('cds--list--expressive')).to.be.true;
88+
});
89+
});
90+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright IBM Corp. 2025, 2025
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
import { expect, fixture, html } from '@open-wc/testing';
8+
import '@carbon/web-components/es/components/list/index.js';
9+
10+
describe('cds-unordered-list', () => {
11+
it('should be an ul element', async () => {
12+
const list = html` <cds-unordered-list>
13+
<cds-list-item>Item</cds-list-item>
14+
</cds-unordered-list>`;
15+
16+
const el = await fixture(list);
17+
18+
const ul = el.shadowRoot.querySelector('ul');
19+
expect(ul).to.exist;
20+
21+
expect(el).dom.to.equalSnapshot();
22+
});
23+
24+
it('should render children as expected', async () => {
25+
const list = html` <cds-unordered-list>
26+
<cds-list-item>Item</cds-list-item>
27+
</cds-unordered-list>`;
28+
29+
const el = await fixture(list);
30+
31+
const slot = el.shadowRoot.querySelector('slot');
32+
const assigned = slot.assignedNodes({ flatten: true });
33+
34+
const listItem = assigned.find(
35+
(node) =>
36+
node.nodeType === Node.ELEMENT_NODE &&
37+
node.tagName.toLowerCase() === 'cds-list-item'
38+
);
39+
40+
expect(listItem).to.exist;
41+
expect(listItem.getAttribute('role')).to.equal('listitem');
42+
expect(listItem.textContent.trim()).to.equal('Item');
43+
});
44+
45+
it('should render nested lists', async () => {
46+
const list = html` <cds-unordered-list>
47+
<cds-list-item>
48+
Ordered List level 1
49+
<cds-unordered-list>
50+
<cds-list-item>Ordered List level 2</cds-list-item>
51+
<cds-list-item>Ordered List level 2</cds-list-item>
52+
</cds-unordered-list>
53+
</cds-list-item>
54+
</cds-unordered-list>`;
55+
56+
const el = await fixture(list);
57+
58+
const ul = el.shadowRoot.querySelector('ul');
59+
expect(ul.classList.contains('cds--list--nested')).to.be.false;
60+
61+
const nested = document.querySelector('cds-unordered-list[slot="nested"');
62+
const nestedUl = nested.shadowRoot.querySelector('ul');
63+
64+
expect(nestedUl.classList.contains('cds--list--nested')).to.be.true;
65+
expect(nestedUl.classList.contains('cds--list--unordered')).to.be.true;
66+
});
67+
68+
it('should render expressive lists', async () => {
69+
const list = html` <cds-unordered-list is-expressive>
70+
<cds-list-item>Item</cds-list-item>
71+
</cds-unordered-list>`;
72+
73+
const el = await fixture(list);
74+
75+
const ul = el.shadowRoot.querySelector('ul');
76+
expect(ul.classList.contains('cds--list--expressive')).to.be.true;
77+
});
78+
});

0 commit comments

Comments
 (0)