Skip to content

Commit

Permalink
create animation frame test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Sep 2, 2022
1 parent 9dcf260 commit 0e5c8bf
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions test/lazy-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {expect, fixture, html} from '@open-wc/testing'
import {spy} from 'sinon'
import {lazyDefine} from '../src/lazy-define.js'

const animationFrame = () => new Promise<unknown>(resolve => requestAnimationFrame(resolve))

describe('lazyDefine', () => {
describe('ready strategy', () => {
it('calls define for a lazy component', async () => {
const onDefine = spy()
lazyDefine('scan-document-test', onDefine)
await fixture(html`<scan-document-test></scan-document-test>`)

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()

expect(onDefine).to.be.callCount(1)
})
Expand All @@ -19,7 +21,7 @@ describe('lazyDefine', () => {
await fixture(html`<later-defined-element-test></later-defined-element-test>`)
lazyDefine('later-defined-element-test', onDefine)

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()

expect(onDefine).to.be.callCount(1)
})
Expand All @@ -39,7 +41,7 @@ describe('lazyDefine', () => {
<twice-defined-element></twice-defined-element>
`)

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()

expect(onDefine).to.be.callCount(2)
})
Expand All @@ -51,12 +53,12 @@ describe('lazyDefine', () => {
lazyDefine('scan-document-test', onDefine)
await fixture(html`<scan-document-test data-load-on="firstInteraction"></scan-document-test>`)

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()
expect(onDefine).to.be.callCount(0)

document.dispatchEvent(new Event('mousedown'))

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()
expect(onDefine).to.be.callCount(1)
})
})
Expand All @@ -68,12 +70,12 @@ describe('lazyDefine', () => {
html`<div style="height: calc(100vh + 256px)"></div>
<scan-document-test data-load-on="visible"></scan-document-test>`
)
await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()
expect(onDefine).to.be.callCount(0)

document.documentElement.scrollTo({top: 10})

await new Promise<unknown>(resolve => requestAnimationFrame(resolve))
await animationFrame()
expect(onDefine).to.be.callCount(1)
})
})
Expand Down

0 comments on commit 0e5c8bf

Please sign in to comment.