Skip to content

Commit

Permalink
test: add tests for limit-rate util
Browse files Browse the repository at this point in the history
  • Loading branch information
mantariksh committed Oct 7, 2020
1 parent 9ef716b commit 939ba0d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/app/utils/__tests__/limit-rate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import RateLimit from 'express-rate-limit'
import expressHandler from 'tests/unit/backend/helpers/jest-express'
import { mocked } from 'ts-jest/utils'

jest.mock('express-rate-limit')
const MockRateLimit = mocked(RateLimit, true)

// eslint-disable-next-line import/first
import { limitRate } from 'src/app/utils/limit-rate'

const MOCK_MAX = 5
const MOCK_WINDOW = 10

describe('limitRate', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should create a rate-limiting middleware with defaults', () => {
limitRate()
expect(MockRateLimit).toHaveBeenCalledWith(
expect.objectContaining({ windowMs: 60000, max: 1200 }),
)
})

it('should create a rate-limiting middleware with custom options', () => {
limitRate({ max: MOCK_MAX, windowMs: MOCK_WINDOW })
expect(MockRateLimit).toHaveBeenCalledWith(
expect.objectContaining({ windowMs: MOCK_WINDOW, max: MOCK_MAX }),
)
})

it('should call next() in the handler', () => {
limitRate()
const handler = MockRateLimit.mock.calls[0][0]!.handler!
const mockNext = jest.fn()
handler(
expressHandler.mockRequest(),
expressHandler.mockResponse(),
mockNext,
)
expect(mockNext).toHaveBeenCalled()
})
})

0 comments on commit 939ba0d

Please sign in to comment.