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
|
test("length", () => {
expect(Array.prototype[Symbol.iterator].length).toBe(0);
});
test("same function as Array.prototype.values", () => {
expect(Array.prototype[Symbol.iterator]).toBe(Array.prototype.values);
});
test("basic functionality", () => {
const a = [1, 2, 3];
const it = a[Symbol.iterator]();
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: 2, done: false });
expect(it.next()).toEqual({ value: 3, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
test("works when applied to non-object", () => {
[true, false, 9, 2n, Symbol()].forEach(primitive => {
const it = [][Symbol.iterator].call(primitive);
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
});
test("item added to array before exhaustion is accessible", () => {
const a = [1, 2];
const it = a[Symbol.iterator]();
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: 2, done: false });
a.push(3);
expect(it.next()).toEqual({ value: 3, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
});
test("item added to array after exhaustion is inaccesible", () => {
const a = [1, 2];
const it = a[Symbol.iterator]();
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: 2, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
a.push(3);
expect(it.next()).toEqual({ value: undefined, done: true });
});
|