Skip to content

Commit 80da419

Browse files
test: adds test and snapshots for Loading in WC (#19098)
* test: adds test and snapshots for Loading in WC * Update packages/web-components/src/components/loading/__tests__/loading-test.js Co-authored-by: Taylor Jones <[email protected]> --------- Co-authored-by: Taylor Jones <[email protected]>
1 parent 24dc74c commit 80da419

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots['cds-loading should render'] = `<cds-loading
5+
description="Loading"
6+
inactive=""
7+
>
8+
</cds-loading>
9+
`;
10+
/* end snapshot cds-loading should render */
11+
snapshots['cds-loading should render with default settings'] = `<cds-loading
12+
description="Loading"
13+
inactive=""
14+
>
15+
</cds-loading>
16+
`;
17+
/* end snapshot cds-loading should render with default settings */
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* Copyright IBM Corp. 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/loading/index.js';
9+
import { LOADING_TYPE } from '@carbon/web-components/es/components/loading/defs.js';
10+
11+
describe('cds-loading', function () {
12+
const loadling = html`<cds-loading></cds-loading>`;
13+
const loadingWithDescription = html`<cds-loading
14+
description="Custom loading text"></cds-loading>`;
15+
const loadingWithLabel = html`<cds-loading
16+
assistive-text="Assistive loading text"></cds-loading>`;
17+
const loadingSmall = html`<cds-loading small></cds-loading>`;
18+
const loadingWithOverlay = html`<cds-loading overlay></cds-loading>`;
19+
const loadingActive = html`<cds-loading active></cds-loading>`;
20+
21+
it('should render', async () => {
22+
const el = await fixture(loadling);
23+
expect(el).dom.to.equalSnapshot();
24+
});
25+
26+
describe('Component API', () => {
27+
it('should have default description text', async () => {
28+
const el = await fixture(loadling);
29+
expect(el.description).to.equal('Loading');
30+
});
31+
32+
it('should change description text when property is set', async () => {
33+
const el = await fixture(loadingWithDescription);
34+
const title = el.shadowRoot.querySelector('title');
35+
36+
expect(title.textContent).to.equal('Custom loading text');
37+
});
38+
39+
it('should support assistive-text as alias for description', async () => {
40+
const el = await fixture(loadingWithLabel);
41+
expect(el.description).to.equal('Assistive loading text');
42+
const title = el.shadowRoot.querySelector('title');
43+
44+
expect(title.textContent).to.equal('Assistive loading text');
45+
});
46+
47+
it('should add small attribute when small property is set', async () => {
48+
const el = await fixture(loadingSmall);
49+
expect(el.small).to.be.true;
50+
});
51+
52+
it('should support type as alias for small property', async () => {
53+
const el = await fixture(loadling);
54+
expect(el.type).to.equal(LOADING_TYPE.REGULAR);
55+
56+
el.type = LOADING_TYPE.SMALL;
57+
await el.updateComplete;
58+
expect(el.small).to.be.true;
59+
});
60+
});
61+
62+
describe('Active state', () => {
63+
it('should support setting active state', async () => {
64+
const el = await fixture(loadling);
65+
expect(el.active).to.be.false;
66+
67+
el.active = true;
68+
await el.updateComplete;
69+
70+
expect(el.active).to.be.true;
71+
});
72+
73+
it('should support inactive as inverse of active property', async () => {
74+
const el = await fixture(loadling);
75+
expect(el.inactive).to.be.true;
76+
expect(el.active).to.be.false;
77+
78+
el.inactive = false;
79+
await el.updateComplete;
80+
expect(el.active).to.be.true;
81+
});
82+
83+
it('should reflect active state to attribute', async () => {
84+
const el = await fixture(loadling);
85+
expect(el.hasAttribute('active')).to.be.false;
86+
el.active = true;
87+
await el.updateComplete;
88+
expect(el.hasAttribute('active')).to.be.true;
89+
});
90+
91+
it('should reflect inactive state to attribute', async () => {
92+
const el = await fixture(loadling);
93+
expect(el.hasAttribute('inactive')).to.be.true;
94+
el.inactive = false;
95+
await el.updateComplete;
96+
expect(el.hasAttribute('inactive')).to.be.false;
97+
});
98+
});
99+
100+
describe('Overlay variant', () => {
101+
it('should render with overlay when overlay attribute is set', async () => {
102+
const el = await fixture(loadingWithOverlay);
103+
expect(el.overlay).to.be.true;
104+
105+
const divElement = el.shadowRoot.querySelector('div');
106+
expect(divElement).to.exist;
107+
});
108+
109+
it('should render without overlay when overlay attribute is not set', async () => {
110+
const el = await fixture(loadling);
111+
expect(el.overlay).to.be.false;
112+
const svgElement = el.shadowRoot.querySelector('svg');
113+
expect(svgElement).to.exist;
114+
});
115+
});
116+
117+
describe('SVG structure', () => {
118+
it('should have correct structure for default loading', async () => {
119+
const el = await fixture(loadling);
120+
const svg = el.shadowRoot.querySelector('svg');
121+
expect(svg).to.exist;
122+
123+
const circle = svg.querySelector('circle');
124+
expect(circle).to.exist;
125+
expect(circle.getAttribute('cx')).to.equal('50%');
126+
expect(circle.getAttribute('cy')).to.equal('50%');
127+
});
128+
129+
it('should have correct structure for small loading', async () => {
130+
const el = await fixture(loadingSmall);
131+
const svg = el.shadowRoot.querySelector('svg');
132+
expect(svg).to.exist;
133+
134+
const circles = svg.querySelectorAll('circle');
135+
expect(circles.length).to.be.at.least(1);
136+
});
137+
});
138+
139+
describe('automated verification testing', () => {
140+
it('should have no Axe violations', async () => {
141+
const el = await fixture(loadling);
142+
await expect(el).to.be.accessible();
143+
});
144+
145+
it('should have no Axe violations with overlay', async () => {
146+
const el = await fixture(loadingWithOverlay);
147+
await expect(el).to.be.accessible();
148+
});
149+
150+
it('should have no Axe violations when active', async () => {
151+
const el = await fixture(loadingActive);
152+
await expect(el).to.be.accessible();
153+
});
154+
155+
it('should have no Axe violations when small', async () => {
156+
const el = await fixture(loadingSmall);
157+
await expect(el).to.be.accessible();
158+
});
159+
});
160+
});

0 commit comments

Comments
 (0)