forked from andreasbm/weightless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.test.ts
77 lines (64 loc) · 2.15 KB
/
label.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { assignedNodesMap, createContainer, removeContainer, waitForElement, waitForElements } from "../../test/testing-helpers";
import "../icon";
import "../checkbox";
import { Checkbox } from "../checkbox/checkbox";
import { Label } from "./label";
describe("wl-label", () => {
const { expect } = chai;
let $container: HTMLElement;
let $label1: Label;
let $checkbox1: Checkbox;
let $label2: Label;
let $checkbox2: Checkbox;
before(() => {
$container = createContainer();
});
beforeEach(async () => {
$container.innerHTML = `
<!-- Technique 1 -->
<wl-checkbox id="cb1"></wl-checkbox>
<wl-label id="l1" for="cb1">This is a label</wl-label>
<!-- Technique 2 -->
<wl-label id="l2">
<wl-checkbox id="cb2"></wl-checkbox>
This is another label
</wl-label>
`;
await waitForElements(["wl-label", "wl-checkbox"]);
$label1 = $container.querySelector<Label>("#l1")!;
$checkbox1 = $container.querySelector<Checkbox>("#cb1")!;
$label2 = $container.querySelector<Label>("#l2")!;
$checkbox2 = $container.querySelector<Checkbox>("#cb2")!;
});
after(() => removeContainer($container));
/**
* Tests the refiring of click.
* @param $label
* @param $checkbox
* @param done
*/
function expectRefireClick($label: Label, $checkbox: Checkbox, done: () => void) {
// Listen for click event (prevent the default reload behavior).
$checkbox.addEventListener("click", e => {
e.preventDefault();
done();
});
// Ensure that the targeted elements are correct
expect($label.getTargetElement()).to.be.equal($checkbox);
// Simulate a click
$label.click();
}
it("should render the slots", async () => {
const assignedNodes = assignedNodesMap($label2.shadowRoot!);
expect(assignedNodes[""].length).to.be.above(0);
});
it("should interact with associated element via the for attribute", done => {
expectRefireClick($label1, $checkbox1, done);
});
it("should interact with associated element via the slot", done => {
expectRefireClick($label2, $checkbox2, done);
});
it("should add aria-labelledby when using the for attribute", () => {
expect($checkbox1.getAttribute("aria-labelledby")).to.equal($label1.id);
});
});