Skip to content

Commit

Permalink
more fn-api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Feb 18, 2024
1 parent 76a6aca commit 16b7de6
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/temporal-polyfill/src/funcApi/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ describe('fromFields', () => {
})
})

// TODO: withFields
describe('withFields', () => {
it('modifies fields and returns a new object', () => {
const d0 = DurationFns.fromFields({ years: 1, months: 1 })
const d1 = DurationFns.withFields(d0, { months: 2 })
expectDurationEquals(d1, { years: 1, months: 2 })
})
})

describe('blank', () => {
it('gives true for blank duration', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/temporal-polyfill/src/funcApi/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { queryNativeTimeZone } from '../internal/timeZoneNative'
import { totalDuration } from '../internal/total'
import { NumberSign, bindArgs, identity } from '../internal/utils'

// TODO: rename to keep scope? Slots/Fields/Bag?
export type { DurationSlots, DurationBag }

export type RelativeToArg =
Expand Down
1 change: 1 addition & 0 deletions packages/temporal-polyfill/src/funcApi/instant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { bindArgs } from '../internal/utils'
import { prepCachedInstantFormat } from './intlFormatCache'
import { refineCalendarIdString, refineTimeZoneIdString } from './utils'

// TODO: rename to keep scope? Slots?
export type { InstantSlots }

export const create = constructInstantSlots
Expand Down
235 changes: 235 additions & 0 deletions packages/temporal-polyfill/src/funcApi/now.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
import { describe, expect, it } from 'vitest'
import { dayTimeNanoToBigInt } from '../internal/dayTimeNano'
import { isoToEpochNano } from '../internal/timeMath'
import * as InstantFns from './instant'
import * as NowFns from './now'
import * as PlainDateFns from './plainDate'
import * as PlainDateTimeFns from './plainDateTime'
import * as PlainTimeFns from './plainTime'
import * as ZonedDateTimeFns from './zonedDateTime'

describe('timeZoneId', () => {
it('returns the system time zone', () => {
expect(NowFns.timeZoneId()).toBe(systemTimeZoneId)
})
})

describe('instant', () => {
it('returns now', () => {
const inst0 = NowFns.instant()
const inst1 = getCurrentInstant()
expectInstantsSimilar(inst0, inst1)
})
})

describe('zonedDateTime', () => {
it('returns now, assuming system time zone', () => {
const zdt0 = NowFns.zonedDateTime('hebrew')
const zdt1 = getCurrentZonedDateTime('hebrew', systemTimeZoneId)
expectZonedDateTimesSimilar(zdt0, zdt1)
})

it('returns now with a given time zone', () => {
const zdt0 = NowFns.zonedDateTime('hebrew', 'America/Chicago')
const zdt1 = getCurrentZonedDateTime('hebrew', 'America/Chicago')
expectZonedDateTimesSimilar(zdt0, zdt1)
})
})

describe('zonedDateTimeISO', () => {
it('returns now, assuming system time zone', () => {
const zdt0 = NowFns.zonedDateTimeISO()
const zdt1 = getCurrentZonedDateTime('iso8601', systemTimeZoneId)
expectZonedDateTimesSimilar(zdt0, zdt1)
})

it('returns now with a given time zone', () => {
const zdt0 = NowFns.zonedDateTimeISO('America/Chicago')
const zdt1 = getCurrentZonedDateTime('iso8601', 'America/Chicago')
expectZonedDateTimesSimilar(zdt0, zdt1)
})
})

describe('plainDateTime', () => {
it('returns current date, assuming system time zone', () => {
const pdt0 = NowFns.plainDateTime('hebrew')
const pdt1 = ZonedDateTimeFns.toPlainDateTime(
getCurrentZonedDateTime('hebrew', systemTimeZoneId),
)
expectPlainDateTimesSimilar(pdt0, pdt1)
})

it('returns current date with a given time zone', () => {
const pdt0 = NowFns.plainDateTime('hebrew', 'America/Chicago')
const pdt1 = ZonedDateTimeFns.toPlainDateTime(
getCurrentZonedDateTime('hebrew', 'America/Chicago'),
)
expectPlainDateTimesSimilar(pdt0, pdt1)
})
})

describe('plainDateTimeISO', () => {
it('returns current date, assuming system time zone', () => {
const pdt0 = NowFns.plainDateTimeISO()
const pdt1 = ZonedDateTimeFns.toPlainDateTime(
getCurrentZonedDateTime('iso8601', systemTimeZoneId),
)
expectPlainDateTimesSimilar(pdt0, pdt1)
})

it('returns current date with a given time zone', () => {
const pdt0 = NowFns.plainDateTimeISO('America/Chicago')
const pdt1 = ZonedDateTimeFns.toPlainDateTime(
getCurrentZonedDateTime('iso8601', 'America/Chicago'),
)
expectPlainDateTimesSimilar(pdt0, pdt1)
})
})

describe('plainDate', () => {
it('returns current date, assuming system time zone', () => {
const pd0 = NowFns.plainDate('hebrew')
const pd1 = ZonedDateTimeFns.toPlainDate(
getCurrentZonedDateTime('hebrew', systemTimeZoneId),
)
expectPlainDatesSimilar(pd0, pd1)
})

it('returns current date with a given time zone', () => {
const pd0 = NowFns.plainDate('hebrew', 'America/Chicago')
const pd1 = ZonedDateTimeFns.toPlainDate(
getCurrentZonedDateTime('hebrew', 'America/Chicago'),
)
expectPlainDatesSimilar(pd0, pd1)
})
})

describe('plainDateISO', () => {
it('returns current date, assuming system time zone', () => {
const pd0 = NowFns.plainDateISO()
const pd1 = ZonedDateTimeFns.toPlainDate(
getCurrentZonedDateTime('iso8601', systemTimeZoneId),
)
expectPlainDatesSimilar(pd0, pd1)
})

it('returns current date with a given time zone', () => {
const pd0 = NowFns.plainDateISO('America/Chicago')
const pd1 = ZonedDateTimeFns.toPlainDate(
getCurrentZonedDateTime('iso8601', 'America/Chicago'),
)
expectPlainDatesSimilar(pd0, pd1)
})
})

describe('plainTimeISO', () => {
it('returns current time, assuming system time zone', () => {
const pt0 = NowFns.plainTimeISO()
const pt1 = ZonedDateTimeFns.toPlainTime(
getCurrentZonedDateTime('iso8601', systemTimeZoneId),
)
expectPlainTimesSimilar(pt0, pt1)
})

it('returns current time with a given time zone', () => {
const pt0 = NowFns.plainTimeISO('America/Chicago')
const pt1 = ZonedDateTimeFns.toPlainTime(
getCurrentZonedDateTime('iso8601', 'America/Chicago'),
)
expectPlainTimesSimilar(pt0, pt1)
})
})

// Utils
// -----------------------------------------------------------------------------

const systemResolvedOptions = new Intl.DateTimeFormat().resolvedOptions()
const systemTimeZoneId = systemResolvedOptions.timeZone

function getCurrentInstant() {
return InstantFns.fromEpochMilliseconds(Date.now())
}

function getCurrentZonedDateTime(
calendar: string,
timeZone: string,
): ZonedDateTimeFns.ZonedDateTimeSlots<string, string> {
return InstantFns.toZonedDateTime(getCurrentInstant(), { timeZone, calendar })
}

function expectEpochNanosSimilar(
epochNano0: bigint,
epochNano1: bigint,
): boolean {
return Math.abs(Number(epochNano0 - epochNano1)) < 1000
}

function expectInstantsSimilar(
inst0: InstantFns.InstantSlots,
inst1: InstantFns.InstantSlots,
): void {
expect(inst0.branding).toBe('Instant')
expect(inst1.branding).toBe('Instant')
expectEpochNanosSimilar(
InstantFns.epochNanoseconds(inst0),
InstantFns.epochNanoseconds(inst1),
)
}

function expectZonedDateTimesSimilar(
zdt0: ZonedDateTimeFns.ZonedDateTimeSlots<string, string>,
zdt1: ZonedDateTimeFns.ZonedDateTimeSlots<string, string>,
): void {
expect(zdt0.branding).toBe('ZonedDateTime')
expect(zdt1.branding).toBe('ZonedDateTime')
expect(zdt0.calendar).toBe(zdt1.calendar)
expect(zdt0.timeZone).toBe(zdt1.timeZone)
expectEpochNanosSimilar(
ZonedDateTimeFns.epochNanoseconds(zdt0),
ZonedDateTimeFns.epochNanoseconds(zdt1),
)
}

function expectPlainDateTimesSimilar(
pdt0: PlainDateTimeFns.PlainDateTimeSlots<string>,
pdt1: PlainDateTimeFns.PlainDateTimeSlots<string>,
): void {
expect(pdt0.branding).toBe('PlainDateTime')
expect(pdt1.branding).toBe('PlainDateTime')
expect(pdt0.calendar).toBe(pdt1.calendar)
expectEpochNanosSimilar(
dayTimeNanoToBigInt(isoToEpochNano(pdt0)!),
dayTimeNanoToBigInt(isoToEpochNano(pdt1)!),
)
}

function expectPlainDatesSimilar(
pd0: PlainDateFns.PlainDateSlots<string>,
pd1: PlainDateFns.PlainDateSlots<string>,
): void {
expect(pd0.branding).toBe('PlainDate')
expect(pd1.branding).toBe('PlainDate')
expect(pd0.calendar).toBe(pd1.calendar)
expectEpochNanosSimilar(
dayTimeNanoToBigInt(isoToEpochNano(pd0)!),
dayTimeNanoToBigInt(isoToEpochNano(pd1)!),
)
}

function expectPlainTimesSimilar(
pt0: PlainTimeFns.PlainTimeSlots,
pt1: PlainTimeFns.PlainTimeSlots,
): void {
expect(pt0.branding).toBe('PlainTime')
expect(pt1.branding).toBe('PlainTime')
expectEpochNanosSimilar(
dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt0 })!),
dayTimeNanoToBigInt(isoToEpochNano({ ...isoDateDefaults, ...pt1 })!),
)
}

const isoDateDefaults = {
isoYear: 0,
isoMonth: 0,
isoDay: 0,
}
4 changes: 3 additions & 1 deletion packages/temporal-polyfill/src/funcApi/now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export function plainDateISO(
)
}

export function plainTimeISO(timeZoneId: string): PlainTimeSlots {
export function plainTimeISO(
timeZoneId: string = getCurrentTimeZoneId(),
): PlainTimeSlots {
return createPlainTimeSlots(
getCurrentIsoDateTime(
queryNativeTimeZone(refineTimeZoneIdString(timeZoneId)),
Expand Down
3 changes: 3 additions & 0 deletions packages/temporal-polyfill/src/funcApi/plainDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ import {
// OR do it on the PlainDateSlots?
// TODO: name all args properly like 'plainDateSlots'???

// TODO: rename to keep scope? Slots/Fields/Bag?
export type { PlainDateSlots, DateBag }

export const create = bindArgs(
constructPlainDateSlots<string, string>,
refineCalendarIdString,
Expand Down
3 changes: 3 additions & 0 deletions packages/temporal-polyfill/src/funcApi/plainDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ import {
refineCalendarIdString,
} from './utils'

// TODO: rename to keep scope? Slots/Fields/Bag?
export type { PlainDateTimeSlots, DateTimeBag }

export const create = bindArgs(
constructPlainDateTimeSlots<string, string>,
refineCalendarIdString,
Expand Down
3 changes: 3 additions & 0 deletions packages/temporal-polyfill/src/funcApi/plainTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { NumberSign, bindArgs, identity } from '../internal/utils'
import { prepCachedPlainTimeFormat } from './intlFormatCache'
import { refineTimeZoneIdString } from './utils'

// TODO: rename to keep scope? Slots/Fields/Bag?
export type { PlainTimeSlots, TimeBag }

export const create = constructPlainTimeSlots

export const fromFields = refinePlainTimeBag
Expand Down
25 changes: 24 additions & 1 deletion packages/temporal-polyfill/src/funcApi/zonedDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ import {
import { moveZonedDateTime } from '../internal/move'
import { ZonedFieldOptions } from '../internal/optionsRefine'
import { roundZonedDateTime } from '../internal/round'
import { DateSlots, ZonedDateTimeSlots } from '../internal/slots'
import {
DateSlots,
ZonedDateTimeSlots,
getEpochMicroseconds,
getEpochMilliseconds,
getEpochNanoseconds,
getEpochSeconds,
} from '../internal/slots'
import { queryNativeTimeZone } from '../internal/timeZoneNative'
import {
ZonedDateTimeFields,
Expand All @@ -64,6 +71,9 @@ import {
refineTimeZoneIdString,
} from './utils'

// TODO: rename to keep scope? Slots/Fields/Bag?
export type { ZonedDateTimeSlots, ZonedDateTimeBag, DateTimeBag }

export const create = bindArgs(
constructZonedDateTimeSlots<string, string, string, string>,
refineCalendarIdString,
Expand All @@ -87,6 +97,19 @@ export function fromFields(
)
}

export const epochSeconds = getEpochSeconds as (
slots: ZonedDateTimeSlots<string, string>,
) => number
export const epochMilliseconds = getEpochMilliseconds as (
slots: ZonedDateTimeSlots<string, string>,
) => number
export const epochMicroseconds = getEpochMicroseconds as (
slots: ZonedDateTimeSlots<string, string>,
) => bigint
export const epochNanoseconds = getEpochNanoseconds as (
slots: ZonedDateTimeSlots<string, string>,
) => bigint

export const getISOFields = bindArgs(
buildZonedIsoFields<string, string>,
queryNativeTimeZone,
Expand Down

0 comments on commit 16b7de6

Please sign in to comment.