Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as imageLoaded from 'image-loaded'
import * as raf from 'raf'
import Spriteling from '../spriteling'
jest.setTimeout(1000)
// Mock imageLoaded
imageLoaded.default = jest.fn((a, b) => {
a.width = 100
a.height = 100
b()
})
// Mock dummy RAF
raf.default = jest.fn()
raf.default.cancel = jest.fn()
// Mock RAF to play x amount of frames
let workingRafTimeout
const workingRaf = (framesToRender: number, delay: number) => {
let time = 0
return (a) => {
if (framesToRender) {
framesToRender--
workingRafTimeout = setTimeout(() => {
a(time += delay)
}, delay)
}
}
}
// Mock element.offsetParent property
it('should play script reversed', async (done) => {
const delay = 10
const simpleScript = [
{sprite: 1},
{sprite: 2},
{sprite: 3},
{sprite: 4}
]
let counter = 3
raf.default.mockImplementation(workingRaf(4, delay))
await instance.play({
run: 1,
delay,
script: simpleScript,
reversed: true,
onFrame: (frameNumber) => {
expect(frameNumber).toBe(counter)
counter--
},
onStop: () => {
clearTimeout(workingRafTimeout)
done()
}
})
})