forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument-visible.test.ts
More file actions
184 lines (154 loc) · 4.71 KB
/
document-visible.test.ts
File metadata and controls
184 lines (154 loc) · 4.71 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {afterEach, beforeEach, expect, test, vi} from 'vitest';
import {getDocumentVisibilityWatcher} from './document-visible.ts';
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.restoreAllMocks();
});
class Document extends EventTarget {
#visibilityState: DocumentVisibilityState = 'visible';
set visibilityState(v) {
if (this.#visibilityState === v) {
return;
}
this.#visibilityState = v;
this.dispatchEvent(new Event('visibilitychange'));
}
get visibilityState() {
return this.#visibilityState;
}
}
test('waitForVisible', async () => {
const doc = new Document();
doc.visibilityState = 'hidden';
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, signal);
const p = w.waitForVisible();
doc.visibilityState = 'visible';
await p;
});
test('waitForHidden', async () => {
const doc = new Document();
let resolved = false;
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, signal);
const p = w.waitForHidden().then(() => {
resolved = true;
});
doc.visibilityState = 'hidden';
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(1000);
expect(resolved).toBe(true);
await p;
});
test('waitForHidden flip back to visible', async () => {
const doc = new Document();
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, signal);
let resolved = false;
void w.waitForHidden().then(() => {
resolved = true;
});
doc.visibilityState = 'hidden';
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(500);
expect(resolved).toBe(false);
// Flip back to visible.
doc.visibilityState = 'visible';
expect(resolved).toBe(false);
// And wait a bit more.
await vi.advanceTimersByTimeAsync(50_000);
expect(resolved).toBe(false);
});
test('waitForHidden flip back and forth', async () => {
const doc = new Document();
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, signal);
let resolved = false;
const p = w.waitForHidden().then(() => {
resolved = true;
});
doc.visibilityState = 'hidden';
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(500);
expect(resolved).toBe(false);
// Flip back to visible.
doc.visibilityState = 'visible';
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(500);
expect(resolved).toBe(false);
doc.visibilityState = 'hidden';
await vi.advanceTimersByTimeAsync(500);
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(500);
expect(resolved).toBe(true);
await p;
});
test('waitForVisible no document', async () => {
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(undefined, 1_000, signal);
await w.waitForVisible();
// resolves "immediately"
});
test('waitForHidden no document', async () => {
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(undefined, 1_000, signal);
let resolved = false;
void w.waitForHidden().then(() => {
resolved = true;
});
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(1000);
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(100_000);
expect(resolved).toBe(false);
});
test('DocumentVisibleWatcher', async () => {
const doc = new Document();
const {signal} = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, signal);
await w.waitForVisible();
await w.waitForVisible();
let resolved = false;
const p = w.waitForHidden().then(() => {
resolved = true;
});
expect(resolved).toBe(false);
doc.visibilityState = 'hidden';
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(1_000 - 1);
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(1);
expect(resolved).toBe(true);
await p;
await w.waitForHidden();
await w.waitForHidden();
{
let resolved = false;
const p = w.waitForVisible().then(() => {
resolved = true;
});
expect(resolved).toBe(false);
doc.visibilityState = 'visible';
await vi.advanceTimersByTimeAsync(0);
expect(resolved).toBe(true);
await p;
}
});
test('DocumentVisibleWatcher controller abort', async () => {
const doc = new Document();
doc.visibilityState = 'hidden';
const controller = new AbortController();
const w = getDocumentVisibilityWatcher(doc, 1_000, controller.signal);
controller.abort();
let resolved = false;
void w.waitForVisible().then(() => {
resolved = true;
});
doc.visibilityState = 'visible';
await vi.advanceTimersByTimeAsync(0);
expect(resolved).toBe(false);
await vi.advanceTimersByTimeAsync(10_000);
expect(resolved).toBe(false);
});