diff --git a/__tests__/test-utils/index.js b/__tests__/test-utils/index.ts similarity index 78% rename from __tests__/test-utils/index.js rename to __tests__/test-utils/index.ts index 4c5466f88..e562441af 100644 --- a/__tests__/test-utils/index.js +++ b/__tests__/test-utils/index.ts @@ -1,8 +1,8 @@ // Sun Aug 04 2019 19:34:56 GMT-0700 const DEFAULT_TEST_TIME = Date.UTC(2019, 7, 5, 2, 34, 56, 78) -export function timeoutPromise (ms) { - return new Promise((resolve, reject) => { +export function timeoutPromise(ms: number): Promise { + return new Promise((resolve) => { setTimeout(resolve, ms) }) } @@ -17,7 +17,7 @@ export function timeoutPromise (ms) { * https://stackoverflow.com/a/42787232/915811 (basically, moment.js uses * Date#now internally). */ -export function setTestTime (time) { +export function setTestTime(time: number): void { const date = new Date(time) // Log human-readable date to help out human testers. console.log(`Setting test time to ${date}`) @@ -28,7 +28,7 @@ export function setTestTime (time) { * Sets the default mock test time for a variety of tests such that various * calculations and feed version statuses resolve to a certain state. */ -export function setDefaultTestTime () { +export function setDefaultTestTime(): void { setTestTime(DEFAULT_TEST_TIME) } @@ -36,6 +36,7 @@ export function setDefaultTestTime () { * Restore the standard functionality of Date library. This should be used in * the afterEach clause in test suites that require a mocked date. */ -export function restoreDateNowBehavior () { - Date.now.mockRestore && Date.now.mockRestore() +export function restoreDateNowBehavior(): void { + const now = Date.now as jest.Mock + now.mockRestore && now.mockRestore() } diff --git a/__tests__/util/itinerary.js b/__tests__/util/itinerary.js deleted file mode 100644 index 61452f0be..000000000 --- a/__tests__/util/itinerary.js +++ /dev/null @@ -1,152 +0,0 @@ -/* globals describe, expect, it */ - -import { - getItineraryDefaultMonitoredDays, - itineraryCanBeMonitored -} from '../../lib/util/itinerary' -import { WEEKDAYS, WEEKEND_DAYS } from '../../lib/util/monitored-trip' - -const walkLeg = { - mode: 'WALK' -} - -describe('util > itinerary', () => { - describe('itineraryCanBeMonitored', () => { - const transitLeg = { - mode: 'BUS', - transitLeg: true - } - const rentalBikeLeg = { - mode: 'BICYCLE_RENT', - rentedBike: true - } - const rentalCarLeg = { - mode: 'CAR_RENT', - rentedCar: true - } - const rentalMicromobilityLeg = { - mode: 'MICROMOBILITY_RENT', - rentedVehicle: true - } - const rideHailLeg = { - hailedCar: true, - mode: 'CAR_HAIL' - } - - const testCases = [{ - expected: true, - itinerary: { - legs: [transitLeg, walkLeg] - }, - title: 'should be true for an itinerary with transit, no rentals/ride hail.' - }, { - expected: false, - itinerary: { - legs: [walkLeg, rentalBikeLeg] - }, - title: 'should be false for an itinerary without transit.' - }, { - expected: false, - itinerary: { - legs: [walkLeg, transitLeg, rentalBikeLeg] - }, - title: 'should be false for an itinerary with transit and rental bike.' - }, { - expected: false, - itinerary: { - legs: [walkLeg, transitLeg, rentalCarLeg] - }, - title: 'should be false for an itinerary with transit and rental car.' - }, { - expected: false, - itinerary: { - legs: [walkLeg, transitLeg, rentalMicromobilityLeg] - }, - title: 'should be false for an itinerary with transit and rental micromobility.' - }, { - expected: false, - itinerary: { - legs: [walkLeg, transitLeg, rideHailLeg] - }, - title: 'should be false for an itinerary with transit and ride hail.' - }, { - expected: false, - itinerary: {}, - title: 'should be false for a blank itinerary.' - }, { - expected: false, - itinerary: null, - title: 'should be false for a null itinerary.' - }] - - testCases.forEach(({ expected, itinerary, title }) => { - it(title, () => { - expect(itineraryCanBeMonitored(itinerary)).toBe(expected) - }) - }) - }) - describe('getItineraryDefaultMonitoredDays', () => { - const transitLegWeekday = { - mode: 'BUS', - serviceDate: '20210609', // Wednesday - transitLeg: true - } - const transitLegSaturday = { - mode: 'BUS', - serviceDate: '20210612', // Saturday - transitLeg: true - } - const transitLegSunday = { - mode: 'BUS', - serviceDate: '20210613', // Sunday - transitLeg: true - } - - const testCases = [{ - expected: WEEKDAYS, - itinerary: { - legs: [walkLeg, transitLegWeekday] - }, - title: 'should be [\'monday\' thru \'friday\'] for an itinerary starting on a weekday.' - }, { - expected: WEEKEND_DAYS, - itinerary: { - legs: [walkLeg, transitLegSaturday] - }, - title: 'should be [\'saturday\', \'sunday\'] for an itinerary starting on a Saturday.' - }, { - expected: WEEKEND_DAYS, - itinerary: { - legs: [walkLeg, transitLegSunday] - }, - title: 'should be [\'saturday\', \'sunday\'] for an itinerary starting on a Sunday.' - }, { - expected: WEEKDAYS, - itinerary: { - legs: [walkLeg], - startTime: 1623341891000 // Thursday 2021-06-10 12:18 pm EDT - }, - title: 'should be [\'monday\' thru \'friday\'] for an itinerary without transit starting on a weekday (fallback case).' - }, { - expected: WEEKEND_DAYS, - itinerary: { - legs: [walkLeg], - startTime: 1623514691000 // Saturday 2021-06-12 12:18 pm EDT - }, - title: 'should be [\'saturday\', \'sunday\'] for an itinerary without transit starting on a Saturday (fallback case).' - }, { - expected: WEEKEND_DAYS, - itinerary: { - legs: [walkLeg], - startTime: 1623601091000 // Sunday 2021-06-13 12:18 pm EDT - }, - title: 'should be [\'saturday\', \'sunday\'] for an itinerary without transit starting on a Sunday (fallback case).' - }] - - testCases.forEach(({ expected, itinerary, title }) => { - it(title, () => { - expect(getItineraryDefaultMonitoredDays(itinerary)).toBe(expected) - }) - }) - }) -}) diff --git a/__tests__/util/itinerary.ts b/__tests__/util/itinerary.ts new file mode 100644 index 000000000..526d3edf0 --- /dev/null +++ b/__tests__/util/itinerary.ts @@ -0,0 +1,176 @@ +/* globals describe, expect, it */ + +import { + getItineraryDefaultMonitoredDays, + itineraryCanBeMonitored +} from '../../lib/util/itinerary' +import { WEEKDAYS, WEEKEND_DAYS } from '../../lib/util/monitored-trip' + +const walkLeg = { + mode: 'WALK' +} + +describe('util > itinerary', () => { + describe('itineraryCanBeMonitored', () => { + const transitLeg = { + mode: 'BUS', + transitLeg: true + } + const rentalBikeLeg = { + mode: 'BICYCLE_RENT', + rentedBike: true + } + const rentalCarLeg = { + mode: 'CAR_RENT', + rentedCar: true + } + const rentalMicromobilityLeg = { + mode: 'MICROMOBILITY_RENT', + rentedVehicle: true + } + const rideHailLeg = { + hailedCar: true, + mode: 'CAR_HAIL' + } + + const testCases = [ + { + expected: true, + itinerary: { + legs: [transitLeg, walkLeg] + }, + title: + 'should be true for an itinerary with transit, no rentals/ride hail.' + }, + { + expected: false, + itinerary: { + legs: [walkLeg, rentalBikeLeg] + }, + title: 'should be false for an itinerary without transit.' + }, + { + expected: false, + itinerary: { + legs: [walkLeg, transitLeg, rentalBikeLeg] + }, + title: 'should be false for an itinerary with transit and rental bike.' + }, + { + expected: false, + itinerary: { + legs: [walkLeg, transitLeg, rentalCarLeg] + }, + title: 'should be false for an itinerary with transit and rental car.' + }, + { + expected: false, + itinerary: { + legs: [walkLeg, transitLeg, rentalMicromobilityLeg] + }, + title: + 'should be false for an itinerary with transit and rental micromobility.' + }, + { + expected: false, + itinerary: { + legs: [walkLeg, transitLeg, rideHailLeg] + }, + title: 'should be false for an itinerary with transit and ride hail.' + }, + { + expected: false, + itinerary: {}, + title: 'should be false for a blank itinerary.' + }, + { + expected: false, + itinerary: null, + title: 'should be false for a null itinerary.' + } + ] + + testCases.forEach(({ expected, itinerary, title }) => { + it(`${title}`, () => { + expect(itineraryCanBeMonitored(itinerary)).toBe(expected) + }) + }) + }) + describe('getItineraryDefaultMonitoredDays', () => { + const transitLegWeekday = { + mode: 'BUS', + serviceDate: '20210609', // Wednesday + transitLeg: true + } + const transitLegSaturday = { + mode: 'BUS', + serviceDate: '20210612', // Saturday + transitLeg: true + } + const transitLegSunday = { + mode: 'BUS', + serviceDate: '20210613', // Sunday + transitLeg: true + } + + const testCases = [ + { + expected: WEEKDAYS, + itinerary: { + legs: [walkLeg, transitLegWeekday] + }, + title: + "should be ['monday' thru 'friday'] for an itinerary starting on a weekday." + }, + { + expected: WEEKEND_DAYS, + itinerary: { + legs: [walkLeg, transitLegSaturday] + }, + title: + "should be ['saturday', 'sunday'] for an itinerary starting on a Saturday." + }, + { + expected: WEEKEND_DAYS, + itinerary: { + legs: [walkLeg, transitLegSunday] + }, + title: + "should be ['saturday', 'sunday'] for an itinerary starting on a Sunday." + }, + { + expected: WEEKDAYS, + itinerary: { + legs: [walkLeg], + startTime: 1623341891000 // Thursday 2021-06-10 12:18 pm EDT + }, + title: + "should be ['monday' thru 'friday'] for an itinerary without transit starting on a weekday (fallback case)." + }, + { + expected: WEEKEND_DAYS, + itinerary: { + legs: [walkLeg], + startTime: 1623514691000 // Saturday 2021-06-12 12:18 pm EDT + }, + title: + "should be ['saturday', 'sunday'] for an itinerary without transit starting on a Saturday (fallback case)." + }, + { + expected: WEEKEND_DAYS, + itinerary: { + legs: [walkLeg], + startTime: 1623601091000 // Sunday 2021-06-13 12:18 pm EDT + }, + title: + "should be ['saturday', 'sunday'] for an itinerary without transit starting on a Sunday (fallback case)." + } + ] + + testCases.forEach(({ expected, itinerary, title }) => { + it(`${title}`, () => { + expect(getItineraryDefaultMonitoredDays(itinerary)).toBe(expected) + }) + }) + }) +})