-
richard.petersen authoredrichard.petersen authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
util_test.js 1.96 KiB
import { asyncThrottle, hash } from '../src/util.js'
import { generateSimpleViteManifest, wait } from './util.js'
import { expect } from 'chai'
import sinon from 'sinon'
const sandbox = sinon.createSandbox()
describe('Util', function () {
describe('hash function', function () {
it('computes the hash', function () {
const manifest = generateSimpleViteManifest({
'example.js': { imports: ['test.css'] },
'test.css': { }
})
expect(hash(manifest)).to.equal('2245696177')
})
it('the hash changes when the manifest changes', function () {
const manifest = generateSimpleViteManifest({
'example.js': { imports: ['test.css'] },
'test.css': { }
})
const manifestChanged = generateSimpleViteManifest({
'example.js': { imports: ['test1.css'] },
'test1.css': { }
})
expect(hash(manifest)).not.to.equal(hash(manifestChanged))
expect(hash(manifest)).to.equal('2245696177')
expect(hash(manifestChanged)).to.equal('2547998666')
})
})
describe('asyncThrottle function', function () {
let spy
beforeEach(function () {
spy = sandbox.spy(function () {
return new Promise((resolve, reject) => {
// @ts-ignore
spy.resolve = resolve
// @ts-ignore
spy.reject = resolve
})
})
})
it('works', function () {
const throttled = asyncThrottle(spy)
expect(spy.callCount).to.equal(0)
throttled()
expect(spy.callCount).to.equal(1)
})
it('is only called once again on the trailing edge', async function () {
const throttled = asyncThrottle(spy)
expect(spy.callCount).to.equal(0)
throttled()
expect(spy.callCount).to.equal(1)
throttled()
expect(spy.callCount).to.equal(1)
spy.resolve()
await wait(0)
expect(spy.callCount).to.equal(2)
spy.resolve()
await wait(0)
expect(spy.callCount).to.equal(2)
})
})
})