diff --git a/jest.config.json b/jest.config.json index 7993dc4..224feb9 100644 --- a/jest.config.json +++ b/jest.config.json @@ -7,13 +7,5 @@ "extensionsToTreatAsEsm": [".ts"], "moduleNameMapper": { "^(\\.{1,2}/.*)\\.js$": "$1" - }, - "transform": { - "^.+\\.[tj]sx?$": [ - "ts-jest", - { - "useESM": true - } - ] } } diff --git a/source/parser.ts b/source/parser.ts index d0c6903..08785db 100644 --- a/source/parser.ts +++ b/source/parser.ts @@ -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, ): Date | undefined => { @@ -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) diff --git a/test/parser-test.ts b/test/parser-test.ts index 172bad1..966703f 100644 --- a/test/parser-test.ts +++ b/test/parser-test.ts @@ -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 = { @@ -24,15 +25,15 @@ 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 }) @@ -40,13 +41,13 @@ describe('input tests', () => { }, ) - 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', @@ -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', @@ -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, @@ -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()) + }) +})