|
| 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