summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/state/tentative/ElementInternals-states.html
blob: 96dcb841eec28ed001dba1ff8994ba3b209e28a3 (plain)
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
78
79
80
81
82
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
class TestElement extends HTMLElement {
  constructor() {
    super();
    this._internals = this.attachInternals();
  }

  get internals() {
    return this._internals;
  }
}
customElements.define("test-element", TestElement);

test(() => {
  let i = (new TestElement()).internals;

  assert_true(i.states instanceof CustomStateSet);
  assert_equals(i.states.size, 0);
  assert_false(i.states.has('foo'));
  assert_false(i.states.has('--foo'));
  assert_equals(i.states.toString(), '[object CustomStateSet]');
}, 'CustomStateSet behavior of ElementInternals.states: Initial state');

test(() => {
  let i = (new TestElement()).internals;
  assert_throws_js(TypeError, () => { i.states.supports('foo'); });
  assert_throws_dom('SyntaxError', () => { i.states.add(''); });
  assert_throws_dom('SyntaxError', () => { i.states.add('--a\tb'); });
}, 'CustomStateSet behavior of ElementInternals.states: Exceptions');

test(() => {
  let i = (new TestElement()).internals;
  i.states.add('--foo');
  i.states.add('--bar');
  i.states.add('--foo');
  assert_equals(i.states.size, 2);
  assert_true(i.states.has('--foo'));
  assert_true(i.states.has('--bar'));
  assert_array_equals([...i.states], ['--foo', '--bar']);
  i.states.delete('--foo');
  assert_array_equals([...i.states], ['--bar']);
  i.states.add('--foo');
  assert_array_equals([...i.states], ['--bar', '--foo']);
  i.states.delete('--bar');
  i.states.add('--baz');
  assert_array_equals([...i.states], ['--foo', '--baz']);
}, 'CustomStateSet behavior of ElementInternals.states: Modifications');

test(() => {
  let i = (new TestElement()).internals;
  i.states.add('--one');
  i.states.add('--two');
  i.states.add('--three');
  let iter = i.states.values();

  // Delete the next item.
  i.states.delete('--one');
  let item = iter.next();
  assert_false(item.done);
  assert_equals(item.value, '--two');

  // Clear the set.
  i.states.clear();
  item = iter.next();
  assert_true(item.done);

  // Delete the previous item.
  i.states.add('--one');
  i.states.add('--two');
  i.states.add('--three');
  iter = i.states.values();
  item = iter.next();
  assert_equals(item.value, '--one');
  i.states.delete('--one');
  item = iter.next();
  assert_equals(item.value, '--two');
}, 'Updating a CustomStateSet while iterating it should work');
</script>