Skip to content

Commit 608704d

Browse files
committed
test: simplify async cycles
1 parent d9c1fff commit 608704d

1 file changed

Lines changed: 15 additions & 28 deletions

File tree

test/bind.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import {bind, listenForBind} from '../lib/bind.js'
22

3-
async function waitForNextAnimationFrame() {
4-
return new Promise(resolve => {
5-
window.requestAnimationFrame(resolve)
6-
})
7-
}
8-
93
describe('bind', () => {
104
window.customElements.define('bind-test-element', class extends HTMLElement {})
115

@@ -150,9 +144,9 @@ describe('bind', () => {
150144
bind(instance)
151145

152146
instance.append(el1, el2)
153-
// We need to wait for a couple of frames after injecting the HTML into to
147+
// We need to wait for one microtask after injecting the HTML into to
154148
// controller so that the actions have been bound to the controller.
155-
await waitForNextAnimationFrame()
149+
await Promise.resolve()
156150
document.body.removeChild(instance)
157151

158152
expect(instance.foo).to.have.not.been.called()
@@ -190,10 +184,9 @@ describe('bind', () => {
190184
bind(instance)
191185
instance.shadowRoot.append(el1)
192186
instance.shadowRoot.append(el2)
193-
// We need to wait for a couple of frames after injecting the HTML into to
187+
// We need to wait for one microtask after injecting the HTML into to
194188
// controller so that the actions have been bound to the controller.
195-
await waitForNextAnimationFrame()
196-
await waitForNextAnimationFrame()
189+
await Promise.resolve()
197190
expect(instance.foo).to.have.not.been.called()
198191
el1.click()
199192
expect(instance.foo).to.have.been.called.exactly(1)
@@ -211,10 +204,9 @@ describe('bind', () => {
211204
const button = document.createElement('button')
212205
button.setAttribute('data-action', 'click:bind-test-element#foo')
213206
instance.appendChild(button)
214-
// We need to wait for a couple of frames after injecting the HTML into to
207+
// We need to wait for one microtask after injecting the HTML into to
215208
// controller so that the actions have been bound to the controller.
216-
await waitForNextAnimationFrame()
217-
await waitForNextAnimationFrame()
209+
await Promise.resolve()
218210
button.click()
219211
expect(instance.foo).to.have.been.called.exactly(1)
220212
})
@@ -228,10 +220,9 @@ describe('bind', () => {
228220
const button = document.createElement('button')
229221
button.setAttribute('data-action', 'click:bind-test-element#foo')
230222
instance.appendChild(button)
231-
// We need to wait for a couple of frames after injecting the HTML into to
223+
// We need to wait for one microtask after injecting the HTML into to
232224
// controller so that the actions have been bound to the controller.
233-
await waitForNextAnimationFrame()
234-
await waitForNextAnimationFrame()
225+
await Promise.resolve()
235226
button.click()
236227
expect(instance.foo).to.have.been.called.exactly(0)
237228
})
@@ -245,10 +236,9 @@ describe('bind', () => {
245236
const button = document.createElement('button')
246237
button.setAttribute('data-action', 'click:bind-test-not-element#foo')
247238
instance.appendChild(button)
248-
// We need to wait for a couple of frames after injecting the HTML into to
239+
// We need to wait for one microtask after injecting the HTML into to
249240
// controller so that the actions have been bound to the controller.
250-
await waitForNextAnimationFrame()
251-
await waitForNextAnimationFrame()
241+
await Promise.resolve()
252242
button.click()
253243
expect(instance.foo).to.have.been.called.exactly(0)
254244
})
@@ -270,8 +260,7 @@ describe('bind', () => {
270260
instance.appendChild(button)
271261
root.appendChild(instance)
272262
// wait for processQueue
273-
await waitForNextAnimationFrame()
274-
await waitForNextAnimationFrame()
263+
await Promise.resolve()
275264
button.click()
276265
expect(instance.foo).to.have.been.called.exactly(1)
277266
})
@@ -290,10 +279,9 @@ describe('bind', () => {
290279
</div>
291280
</div>
292281
`
293-
// We need to wait for a couple of frames after injecting the HTML into to
282+
// We need to wait for one microtask after injecting the HTML into to
294283
// controller so that the actions have been bound to the controller.
295-
await waitForNextAnimationFrame()
296-
await waitForNextAnimationFrame()
284+
await Promise.resolve()
297285
instance.querySelector('button').click()
298286
expect(instance.foo).to.have.been.called.exactly(1)
299287
})
@@ -322,12 +310,11 @@ describe('bind', () => {
322310
instance.appendChild(button)
323311
bind(instance)
324312
listenForBind(root)
325-
await waitForNextAnimationFrame()
313+
await Promise.resolve()
326314
button.click()
327315
expect(instance.foo).to.have.been.called.exactly(0)
328316
button.setAttribute('data-action', 'click:bind-test-element#foo')
329-
await waitForNextAnimationFrame()
330-
await waitForNextAnimationFrame()
317+
await Promise.resolve()
331318
button.click()
332319
expect(instance.foo).to.have.been.called.exactly(1)
333320
})

0 commit comments

Comments
 (0)