Skip to content

Commit 5bc86a7

Browse files
authored
Enhance link tests (#6291)
1 parent 5fa5bd9 commit 5bc86a7

File tree

3 files changed

+117
-13
lines changed

3 files changed

+117
-13
lines changed

components/link/test/link-mixin.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,83 @@ describe('LinkMixin', () => {
7474
});
7575
});
7676

77+
describe('getNewWindowDescription', () => {
78+
79+
it('should return localized string when target is _blank and label is provided', async() => {
80+
const elem = await fixture(newWindowFixture);
81+
const description = elem.getNewWindowDescription('My Label');
82+
expect(description).to.equal('Opens in a new window.');
83+
});
84+
85+
it('should return undefined when target is not _blank', async() => {
86+
const elem = await fixture(emptyFixture);
87+
const description = elem.getNewWindowDescription('My Label');
88+
expect(description).to.be.undefined;
89+
});
90+
91+
it('should return undefined when label is not provided', async() => {
92+
const elem = await fixture(newWindowFixture);
93+
const description = elem.getNewWindowDescription();
94+
expect(description).to.be.undefined;
95+
});
96+
97+
});
98+
99+
describe('_render parameters', () => {
100+
101+
it('should apply rel attribute when provided', async() => {
102+
const tagNameWithRel = defineCE(
103+
class extends LinkMixin(LitElement) {
104+
render() {
105+
return this._render(html`Link Test`, { rel: 'noopener noreferrer' });
106+
}
107+
}
108+
);
109+
const elem = await fixture(`<${tagNameWithRel}></${tagNameWithRel}>`);
110+
const anchor = elem.shadowRoot.querySelector('a');
111+
expect(anchor.getAttribute('rel')).to.equal('noopener noreferrer');
112+
});
113+
114+
it('should apply custom link classes', async() => {
115+
const tagNameWithClasses = defineCE(
116+
class extends LinkMixin(LitElement) {
117+
render() {
118+
return this._render(html`Link Test`, { linkClasses: { 'custom-class': true, 'another-class': true } });
119+
}
120+
}
121+
);
122+
const elem = await fixture(`<${tagNameWithClasses}></${tagNameWithClasses}>`);
123+
const anchor = elem.shadowRoot.querySelector('a');
124+
expect(anchor.classList.contains('custom-class')).to.be.true;
125+
expect(anchor.classList.contains('another-class')).to.be.true;
126+
});
127+
128+
it('should apply custom tabindex', async() => {
129+
const tagNameWithTabindex = defineCE(
130+
class extends LinkMixin(LitElement) {
131+
render() {
132+
return this._render(html`Link Test`, { tabindex: '-1' });
133+
}
134+
}
135+
);
136+
const elem = await fixture(`<${tagNameWithTabindex}></${tagNameWithTabindex}>`);
137+
const anchor = elem.shadowRoot.querySelector('a');
138+
expect(anchor.getAttribute('tabindex')).to.equal('-1');
139+
});
140+
141+
it('should apply ariaLabel when provided', async() => {
142+
const tagNameWithAriaLabel = defineCE(
143+
class extends LinkMixin(LitElement) {
144+
render() {
145+
return this._render(html`Link Test`, { ariaLabel: 'Custom Label' });
146+
}
147+
}
148+
);
149+
const elem = await fixture(`<${tagNameWithAriaLabel}></${tagNameWithAriaLabel}>`);
150+
const anchor = elem.shadowRoot.querySelector('a');
151+
expect(anchor.getAttribute('aria-label')).to.equal('Custom Label');
152+
});
153+
154+
});
155+
77156
});

components/link/test/link.axe.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,29 @@ describe('d2l-link', () => {
2525
await expect(elem).to.be.accessible();
2626
});
2727

28+
it('new-window with target="_blank"', async() => {
29+
const elem = await fixture(html`<d2l-link href="https://www.d2l.com" target="_blank">Opens New Window</d2l-link>`);
30+
await expect(elem).to.be.accessible();
31+
});
32+
33+
it('with aria-label', async() => {
34+
const elem = await fixture(html`<d2l-link href="https://www.d2l.com" aria-label="Custom accessible label">Link</d2l-link>`);
35+
await expect(elem).to.be.accessible();
36+
});
37+
38+
it('with download attribute', async() => {
39+
const elem = await fixture(html`<d2l-link href="https://www.d2l.com/file.pdf" download>Download File</d2l-link>`);
40+
await expect(elem).to.be.accessible();
41+
});
42+
43+
it('truncated with lines property', async() => {
44+
const elem = await fixture(html`<div style="width: 200px;"><d2l-link href="https://www.d2l.com" lines="2">This is a very long link that will be truncated after two lines</d2l-link></div>`);
45+
await expect(elem).to.be.accessible();
46+
});
47+
48+
it('without href', async() => {
49+
const elem = await fixture(html`<d2l-link>Link without href</d2l-link>`);
50+
await expect(elem).to.be.accessible();
51+
});
52+
2853
});

components/link/test/link.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ describe('d2l-link', () => {
1111

1212
describe('attribute binding', () => {
1313

14-
['download', 'href'].forEach((attrName) => {
15-
it(`should bind "${attrName}" attribute to anchor attribute`, async() => {
16-
const elem = await fixture(normalFixture);
17-
elem.setAttribute(attrName, attrName);
18-
await elem.updateComplete;
19-
expect(getAnchor(elem).hasAttribute(attrName)).to.be.true;
20-
});
21-
});
22-
2314
it('should bind "aria-label" attribute to anchor attribute', async() => {
2415
const elem = await fixture(html`<d2l-link aria-label="My Aria Label"></d2l-link>`);
2516
expect(getAnchor(elem).getAttribute('aria-label')).to.equal('My Aria Label');
2617
});
2718

19+
it('should bind "download" attribute to anchor attribute', async() => {
20+
const elem = await fixture(html`<d2l-link download></d2l-link>`);
21+
expect(getAnchor(elem).hasAttribute('download')).to.be.true;
22+
});
23+
2824
it('should bind "href" attribute to anchor attribute', async() => {
2925
const elem = await fixture(html`<d2l-link href="https://www.d2l.com"></d2l-link>`);
3026
expect(getAnchor(elem).getAttribute('href')).to.equal('https://www.d2l.com');
@@ -91,11 +87,15 @@ describe('d2l-link', () => {
9187

9288
});
9389

94-
describe('new-window', () => {
95-
it('should add offscreen text', async() => {
96-
const elem = await fixture(html`<d2l-link target="_blank">link text</d2l-link>`);
97-
expect(elem.shadowRoot.querySelector('.d2l-offscreen').innerText).to.equal('Opens in a new window.');
90+
describe('focus', () => {
91+
92+
it('should focus the anchor element when focus() is called', async() => {
93+
const elem = await fixture(normalFixture);
94+
elem.focus();
95+
await elem.updateComplete;
96+
expect(elem.shadowRoot.activeElement).to.equal(getAnchor(elem));
9897
});
98+
9999
});
100100

101101
});

0 commit comments

Comments
 (0)