Skip to content

Commit

Permalink
more fn api tests, including plainMonthDay
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Feb 29, 2024
1 parent f45c69e commit 03ecd30
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 10 deletions.
5 changes: 1 addition & 4 deletions packages/temporal-polyfill/src/funcApi/plainDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,7 @@ describe('toLocaleString', () => {
it('works', () => {
const pd = PlainDateFns.create(2023, 12, 31)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
dateStyle: 'full',
timeZone: 'America/New_York',
}
const options: Intl.DateTimeFormatOptions = { dateStyle: 'full' }
const s = testHotCache(() =>
PlainDateFns.toLocaleString(pd, locale, options),
)
Expand Down
192 changes: 192 additions & 0 deletions packages/temporal-polyfill/src/funcApi/plainMonthDay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { describe, expect, it } from 'vitest'
import * as PlainMonthDayFns from './plainMonthDay'
import {
expectPlainDateEquals,
expectPlainMonthDayEquals,
testHotCache,
} from './testUtils'

describe('create', () => {
it('works with a referenceYear', () => {
const pmd = PlainMonthDayFns.create(6, 18, 'gregory', 2024)
expectPlainMonthDayEquals(pmd, {
calendar: 'gregory',
isoYear: 2024,
isoMonth: 6,
isoDay: 18,
})
})

it('works without a referenceYear', () => {
const pmd = PlainMonthDayFns.create(6, 18)
expectPlainMonthDayEquals(pmd, {
calendar: 'iso8601',
isoMonth: 6,
isoDay: 18,
})
})
})

describe('fromString', () => {
it('works', () => {
const pmd = PlainMonthDayFns.fromString('2024-06-18[u-ca=gregory]')
expectPlainMonthDayEquals(pmd, {
calendar: 'gregory',
isoMonth: 6,
isoDay: 18,
})
})
})

describe('fromFields', () => {
it('works', () => {
const pmd = PlainMonthDayFns.fromFields({
calendar: 'gregory',
monthCode: 'M06',
day: 18,
})
expectPlainMonthDayEquals(pmd, {
calendar: 'gregory',
isoMonth: 6,
isoDay: 18,
})
})
})

describe('getFields', () => {
it('works', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const fields = PlainMonthDayFns.getFields(pmd)
expect(fields).toEqual({
monthCode: 'M06',
month: 6,
day: 18,
})
})
})

describe('withFields', () => {
it('works', () => {
const pmd0 = PlainMonthDayFns.create(6, 18)
const pmd1 = PlainMonthDayFns.withFields(pmd0, {
day: 11,
})
expectPlainMonthDayEquals(pmd1, {
isoMonth: 6,
isoDay: 11,
})
})
})

describe('equals', () => {
it('works', () => {
const pmd0 = PlainMonthDayFns.create(6, 18)
const pmd1 = PlainMonthDayFns.create(7, 20)
expect(PlainMonthDayFns.equals(pmd0, pmd1)).toBe(false)
expect(PlainMonthDayFns.equals(pmd0, pmd0)).toBe(true)
})
})

describe('toPlainDate', () => {
it('works', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const pd = PlainMonthDayFns.toPlainDate(pmd, { year: 2023 })
expectPlainDateEquals(pd, {
isoYear: 2023,
isoMonth: 6,
isoDay: 18,
})
})
})

describe('toString', () => {
it('works without options', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const s = PlainMonthDayFns.toString(pmd)
expect(s).toBe('06-18')
})

it('works with options', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const s = PlainMonthDayFns.toString(pmd, { calendarName: 'always' })
expect(s).toBe('1972-06-18[u-ca=iso8601]')
})
})

describe('toLocaleString', () => {
it('works', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
PlainMonthDayFns.toLocaleString(pmd, locale, options),
)
expect(s).toBe('June 18')
})
})

describe('toLocaleStringParts', () => {
it('works', () => {
const pmd = PlainMonthDayFns.create(6, 18)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
PlainMonthDayFns.toLocaleStringParts(pmd, locale, options),
)
expect(parts).toEqual([
{ type: 'month', value: 'June' },
{ type: 'literal', value: ' ' },
{ type: 'day', value: '18' },
])
})
})

describe('rangeToLocaleString', () => {
it('works', () => {
const pmd0 = PlainMonthDayFns.create(6, 18)
const pmd1 = PlainMonthDayFns.create(10, 3)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
PlainMonthDayFns.rangeToLocaleString(pmd0, pmd1, locale, options),
)
expect(s).toBe('June 18 – October 3')
})
})

describe('rangeToLocaleStringParts', () => {
it('works', () => {
const pmd0 = PlainMonthDayFns.create(6, 18)
const pmd1 = PlainMonthDayFns.create(10, 3)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
PlainMonthDayFns.rangeToLocaleStringParts(pmd0, pmd1, locale, options),
)
expect(parts).toEqual([
{ source: 'startRange', type: 'month', value: 'June' },
{ source: 'startRange', type: 'literal', value: ' ' },
{ source: 'startRange', type: 'day', value: '18' },
{ source: 'shared', type: 'literal', value: ' – ' },
{ source: 'endRange', type: 'month', value: 'October' },
{ source: 'endRange', type: 'literal', value: ' ' },
{ source: 'endRange', type: 'day', value: '3' },
])
})
})
4 changes: 2 additions & 2 deletions packages/temporal-polyfill/src/funcApi/plainMonthDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ export function withFields(

export const equals = plainMonthDaysEqual<string>

export const toString = formatPlainMonthDayIso<string>

export function toPlainDate(
plainMonthDaySlots: PlainMonthDaySlots<string>,
bag: YearFields,
Expand All @@ -88,6 +86,8 @@ export function toPlainDate(
)
}

export const toString = formatPlainMonthDayIso<string>

export function toLocaleString(
slots: PlainMonthDaySlots<string>,
locales?: LocalesArg,
Expand Down
4 changes: 0 additions & 4 deletions packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ describe('toLocaleString', () => {
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
Expand All @@ -226,7 +225,6 @@ describe('toLocaleStringParts', () => {
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
Expand All @@ -248,7 +246,6 @@ describe('rangeToLocaleString', () => {
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
Expand All @@ -266,7 +263,6 @@ describe('rangeToLocaleStringParts', () => {
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
Expand Down

0 comments on commit 03ecd30

Please sign in to comment.