Fixed unit tests

This commit is contained in:
Daniel Gonzalez
2021-05-08 18:12:54 -04:00
parent cae8d64f6e
commit 3032c7b1be
2 changed files with 36 additions and 1 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ class RateLimit {
console.log(
`this.rateLimitOptions: ${JSON.stringify(this.rateLimitOptions, null, 2)}`
)
this.rateLimit = this.RateLimitLib.middleware(this.rateLimitOptiolimiterns)
this.rateLimit = this.RateLimitLib.middleware(this.rateLimitOptions)
}
// This function is called when the user hits their rate limits.
+35
View File
@@ -25,7 +25,42 @@ describe('#rate-limit', () => {
})
afterEach(() => sandbox.restore())
describe('#constructor', () => {
it('should use the options provided', async () => {
try {
const options = {
interval: { min: 10 },
delayAfter: 1,
timeWait: { sec: 5 },
max: 2,
onLimitReached: () => {
throw new Error('custom message error')
}
}
const _uut = new RateLimit(options)
// Assert options
assert.equal(_uut.rateLimitOptions.interval.min, options.interval.min)
assert.equal(_uut.rateLimitOptions.delayAfter, options.delayAfter)
assert.equal(_uut.rateLimitOptions.timeWait.sec, options.timeWait.sec)
const from = 'constructor test'
const firstRequest = await _uut.limiter(from)
assert.isTrue(firstRequest)
const secondRequest = await _uut.limiter(from)
assert.isTrue(secondRequest)
await _uut.limiter(from)
assert.fail('unexpected error')
} catch (error) {
assert.include(
error.message,
'custom message error'
)
}
})
})
describe('#onLimitReached', () => {
it('should throw error', async () => {
try {