Skip to content

Commit

Permalink
test: add date tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Sep 22, 2023
1 parent 4d6c318 commit fe96c7e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
8 changes: 0 additions & 8 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@
"extensionsToTreatAsEsm": [".ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"^.+\\.[tj]sx?$": [
"ts-jest",
{
"useESM": true
}
]
}
}
4 changes: 2 additions & 2 deletions source/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const parseDraft7Header = (header: string): RateLimitInfo => {
*
* @param header {string} - The header's contents.
*/
const parseResetHeader = (
export const parseResetHeader = (
passedHeader: string | undefined,
options: Partial<ParserOptions>,
): Date | undefined => {
Expand Down Expand Up @@ -290,7 +290,7 @@ const parseResetMilliseconds = (header: string | number): Date =>
* Find out what type of time is passed in the `RateLimit-Reset` header, and
* parse it into a `Date`.
*/
const parseResetAuto = (header: string): Date => {
export const parseResetAuto = (header: string): Date => {
// If it has any letters, assume it's a date string.
if (/[a-z]/i.test(header)) return parseResetDate(header)

Expand Down
50 changes: 38 additions & 12 deletions test/parser-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// /test/parser-test.ts
// Tests for the public API.

import { describe, it, expect } from '@jest/globals'
import { describe, test, expect } from '@jest/globals'
import {
parseDraft7Header,
parseResetAuto,
getRateLimit,
getRateLimits,
} from '../source/parser.js'

const itif = (condition: boolean) => (condition ? it : it.skip)
const testIf = (condition: boolean) => (condition ? test : test.skip)

describe('input tests', () => {
const headers = {
Expand All @@ -24,29 +25,29 @@ describe('input tests', () => {
}

// NOTE: the `Header` and `Response` classes don't exist in node 16 or older.
itif(typeof Headers !== 'undefined')(
'should handle headers in a fetch-style headers object',
testIf(typeof Headers !== 'undefined')(
'fetch-style headers object parsing',
() => {
expect(getRateLimit(new Headers(headers))).toMatchObject(info)
},
)

itif(typeof Response !== 'undefined')(
'should handle headers in a node `ServerResponse` object',
testIf(typeof Response !== 'undefined')(
'fetch-style response object parsing',
() => {
const response = new Response('Hallo!', { headers })

expect(getRateLimit(response)).toMatchObject(info)
},
)

it('should handle headers in a node-style headers object', () => {
test('node-style headers object parsing', () => {
expect(getRateLimit(headers)).toMatchObject(info)
})
})

describe('api tests', () => {
it('should parse a header and return the parsed info', () => {
test('json header object parsing', () => {
const headers = {
'X-Rate-Limit-Limit': '500',
'X-Rate-Limit-Remaining': '499',
Expand All @@ -62,7 +63,7 @@ describe('api tests', () => {
expect(getRateLimit(headers)).toMatchObject(info)
})

it('should parse multiple headers and return all of them', () => {
test('multiple header parsing', () => {
const headers = {
'X-RateLimit-ClientLimit': '1000',
'X-RateLimit-ClientRemaining': '999',
Expand Down Expand Up @@ -93,7 +94,7 @@ describe('api tests', () => {
expect(getRateLimits(headers)).toMatchObject(infos)
})

it('should parse a combined header', () => {
test('combined header (draft 7) parsing', () => {
const header = 'limit=100, remaining=25, reset=5'
const info = {
limit: 100,
Expand All @@ -106,5 +107,30 @@ describe('api tests', () => {
})
})

// TODO: test options
// TODO: test `parseResetAuto` with various formats
describe('date tests', () => {
const thatDay = new Date('2023-05-16T18:12:13.000Z')

test('date string auto-detection', () => {
const dateString = 'Tuesday, May 16, 2023 11:42:13 PM GMT+05:30'

const parsedDate = parseResetAuto(dateString)
expect(parsedDate.getTime()).toBe(thatDay.getTime())
})

test('unix date auto-detection', () => {
const dateString = '1684260733'

const parsedDate = parseResetAuto(dateString)
expect(parsedDate.getTime()).toBe(thatDay.getTime())
})

test('delta seconds auto-detection', () => {
const now = new Date()
const then = new Date()
then.setSeconds(now.getSeconds() + 42)
const dateString = '42'

const parsedDate = parseResetAuto(dateString)
expect(parsedDate.getTime()).toBe(then.getTime())
})
})

0 comments on commit fe96c7e

Please sign in to comment.