Skip to content

Commit 36ed114

Browse files
asynclizcopybara-github
authored andcommitted
chore: refactor custom state set unit tests for code coverage
PiperOrigin-RevId: 877423793
1 parent 9c2a5eb commit 36ed114

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

labs/behaviors/custom-state-set_test.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,35 @@ import {LitElement} from 'lit';
1010
import {customElement} from 'lit/decorators.js';
1111

1212
import {hasState, mixinCustomStateSet, toggleState} from './custom-state-set.js';
13-
import {mixinElementInternals} from './element-internals.js';
13+
import {internals, mixinElementInternals} from './element-internals.js';
1414

1515
// A more reliable test would use `forceElementInternalsPolyfill()` from
1616
// `element-internals-polyfill`, but our GitHub test build doesn't
1717
// support it since the polyfill changes global types.
1818

1919
/* A simplified version of element-internals-polyfill CustomStateSet. */
20-
class PolyfilledCustomStateSet extends Set<string> {
21-
constructor(private readonly ref: HTMLElement) {
22-
super();
20+
class PolyfilledCustomStateSet {
21+
private readonly internalSet = new Set<string>();
22+
23+
constructor(private readonly ref: HTMLElement) {}
24+
25+
has(state: string): boolean {
26+
return this.internalSet.has(state);
2327
}
2428

25-
override add(state: string) {
29+
add(state: string): this {
2630
if (!/^--/.test(state) || typeof state !== 'string') {
2731
throw new DOMException(
2832
`Failed to execute 'add' on 'CustomStateSet': The specified value ${state} must start with '--'.`,
2933
);
3034
}
31-
const result = super.add(state);
35+
this.internalSet.add(state);
3236
this.ref.toggleAttribute(`state${state}`, true);
33-
return result;
37+
return this;
3438
}
3539

36-
override clear() {
37-
for (const [entry] of this.entries()) {
38-
this.delete(entry);
39-
}
40-
super.clear();
41-
}
42-
43-
override delete(state: string) {
44-
const result = super.delete(state);
40+
delete(state: string): boolean {
41+
const result = this.internalSet.delete(state);
4542
this.ref.toggleAttribute(`state${state}`, false);
4643
return result;
4744
}
@@ -53,16 +50,15 @@ class TestCustomStateSet extends mixinCustomStateSet(
5350
) {
5451
static testWithPolyfill = false;
5552

56-
override attachInternals() {
57-
const internals = super.attachInternals();
53+
constructor() {
54+
super();
5855
if (TestCustomStateSet.testWithPolyfill) {
59-
Object.defineProperty(internals, 'states', {
56+
Object.defineProperty(this[internals], 'states', {
6057
enumerable: true,
6158
configurable: true,
6259
value: new PolyfilledCustomStateSet(this),
6360
});
6461
}
65-
return internals;
6662
}
6763
}
6864

0 commit comments

Comments
 (0)