Skip to content

Commit ee6fde4

Browse files
authored
adding a slotchange listener and adding tests (#407)
1 parent bf8790f commit ee6fde4

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

elements/pfe-cta/package-lock.json

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

elements/pfe-cta/src/pfe-cta.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,38 @@ class PfeCta extends PFElement {
1515

1616
constructor() {
1717
super(PfeCta);
18+
this._init = this._init.bind(this);
1819
}
1920

2021
connectedCallback() {
2122
super.connectedCallback();
23+
this._slot = this.shadowRoot.querySelector("slot");
24+
this._slot.addEventListener("slotchange", this._init);
25+
}
26+
27+
disconnectedCallback() {
28+
this._slot.removeEventListener("slotchange", this._init);
29+
}
2230

31+
_init() {
2332
const firstChild = this.children[0];
2433

2534
if (!firstChild) {
2635
console.warn(
27-
"The first child in the light DOM must be an anchor tag (<a>)"
36+
`${
37+
PfeCta.tag
38+
}:The first child in the light DOM must be an anchor tag (<a>)`
2839
);
2940
} else if (firstChild && firstChild.tagName.toLowerCase() !== "a") {
3041
console.warn(
31-
"The first child in the light DOM must be an anchor tag (<a>)"
42+
`${
43+
PfeCta.tag
44+
}:The first child in the light DOM must be an anchor tag (<a>)`
3245
);
3346
} else {
3447
this.link = this.querySelector("a");
3548
}
3649
}
37-
38-
disconnectedCallback() {}
3950
}
4051

4152
PFElement.create(PfeCta);

elements/pfe-cta/test/pfe-cta_test.html

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body>
1111

1212
<pfe-cta>
13-
This is the element content.
13+
<a href="https://redhat.com">redhat.com</a>
1414
</pfe-cta>
1515

1616
<script>
@@ -22,6 +22,46 @@
2222
'the <pfe-cta> should be an instance of PfeCta'
2323
);
2424
});
25+
26+
test("it should log a warning if there are no children in the light DOM", done => {
27+
const spy = sinon.spy(console, 'warn');
28+
const pfeCta = document.createElement("pfe-cta");
29+
pfeCta.textContent = "This is wrong";
30+
31+
document.body.appendChild(pfeCta);
32+
33+
flush(() => {
34+
sinon.assert.calledWith(spy, 'pfe-cta:The first child in the light DOM must be an anchor tag (<a>)');
35+
spy.restore();
36+
done();
37+
});
38+
});
39+
40+
test("it should log a warning if the first child in the light DOM is not an anchor", done => {
41+
const spy = sinon.spy(console, 'warn');
42+
const pfeCta = document.createElement("pfe-cta");
43+
pfeCta.innerHTML = `<p>Something</p><a href="#">A link</a>`;
44+
45+
document.body.appendChild(pfeCta);
46+
47+
flush(() => {
48+
sinon.assert.calledWith(spy, 'pfe-cta:The first child in the light DOM must be an anchor tag (<a>)');
49+
spy.restore();
50+
done();
51+
});
52+
});
53+
54+
test("it should properly initialize when the contents of the slot change", done => {
55+
const pfeCta = document.querySelector("pfe-cta");
56+
assert.equal(pfeCta.link.getAttribute("href"), "https://redhat.com");
57+
58+
pfeCta.innerHTML = `<a href="https://access.redhat.com">Customer Portal</a>`;
59+
60+
flush(() => {
61+
assert.equal(pfeCta.link.getAttribute("href"), "https://access.redhat.com");
62+
done();
63+
});
64+
});
2565
});
2666
</script>
2767
</body>

0 commit comments

Comments
 (0)