Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Feb 29, 2024
1 parent 5af4c1b commit f45c69e
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/funcApi/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expectDurationEquals } from './testUtils'
import * as ZonedDateTimeFns from './zonedDateTime'

describe('create', () => {
it('called with no args', () => {
it('called without args', () => {
const d = DurationFns.create()
expectDurationEquals(d, {})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/funcApi/instant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('since', () => {
})

describe('round', () => {
it('works with no simple string unit', () => {
it('works without simple string unit', () => {
const inst0 = InstantFns.fromString('2024-01-01T00:30:00+01:00')
const inst1 = InstantFns.round(inst0, 'hour')
expectInstantEquals(inst1, 1704067200000000000n)
Expand Down
6 changes: 3 additions & 3 deletions packages/temporal-polyfill/src/funcApi/plainDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('subtract', () => {
})

describe('until', () => {
it('works with no options', () => {
it('works without options', () => {
const pd0 = PlainDateFns.create(2023, 1, 25)
const pd1 = PlainDateFns.create(2024, 2, 27)
const d = PlainDateFns.until(pd0, pd1)
Expand All @@ -246,7 +246,7 @@ describe('until', () => {
})

describe('since', () => {
it('works with no options', () => {
it('works without options', () => {
const pd0 = PlainDateFns.create(2023, 1, 25)
const pd1 = PlainDateFns.create(2024, 2, 27)
const d = PlainDateFns.since(pd0, pd1)
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('toZonedDateTime', () => {
})

describe('toPlainDateTime', () => {
it('works with no arg', () => {
it('works without arg', () => {
const pd = PlainDateFns.create(2024, 2, 27, 'hebrew')
const pdt = PlainDateFns.toPlainDateTime(pd)
expectPlainDateTimeEquals(pdt, {
Expand Down
4 changes: 2 additions & 2 deletions packages/temporal-polyfill/src/funcApi/plainDateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('subtract', () => {
})

describe('until', () => {
it('works with no options', () => {
it('works without options', () => {
const pdt0 = PlainDateTimeFns.create(2023, 1, 25, 10)
const pdt1 = PlainDateTimeFns.create(2024, 2, 27, 12)
const d = PlainDateTimeFns.until(pdt0, pdt1)
Expand All @@ -314,7 +314,7 @@ describe('until', () => {
})

describe('since', () => {
it('works with no options', () => {
it('works without options', () => {
const pdt0 = PlainDateTimeFns.create(2023, 1, 25, 10)
const pdt1 = PlainDateTimeFns.create(2024, 2, 27, 12)
const d = PlainDateTimeFns.since(pdt0, pdt1)
Expand Down
283 changes: 283 additions & 0 deletions packages/temporal-polyfill/src/funcApi/plainYearMonth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
import { describe, expect, it } from 'vitest'
import * as DurationFns from './duration'
import * as PlainYearMonthFns from './plainYearMonth'
import {
expectDurationEquals,
expectPlainDateEquals,
expectPlainYearMonthEquals,
testHotCache,
} from './testUtils'

describe('create', () => {
it('works with a referenceDay', () => {
const pym = PlainYearMonthFns.create(2024, 6, 'gregory', 5)
expectPlainYearMonthEquals(pym, {
calendar: 'gregory',
isoYear: 2024,
isoMonth: 6,
isoDay: 5,
})
})

it('works without a referenceDay', () => {
const pym = PlainYearMonthFns.create(2024, 6, 'gregory')
expectPlainYearMonthEquals(pym, {
calendar: 'gregory',
isoYear: 2024,
isoMonth: 6,
})
})
})

describe('fromString', () => {
it('works', () => {
const pym = PlainYearMonthFns.fromString('2024-06-01[u-ca=gregory]')
expectPlainYearMonthEquals(pym, {
calendar: 'gregory',
isoYear: 2024,
isoMonth: 6,
})
})
})

describe('fromFields', () => {
it('works without options', () => {
const pym = PlainYearMonthFns.fromFields({
calendar: 'gregory',
year: 2024,
month: 6,
})
expectPlainYearMonthEquals(pym, {
calendar: 'gregory',
isoYear: 2024,
isoMonth: 6,
})
})
})

describe('getFields', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 6, 'gregory')
const fields = PlainYearMonthFns.getFields(pym)
expect(fields).toEqual({
era: 'ce',
eraYear: 2024,
year: 2024,
monthCode: 'M06',
month: 6,
})
})
})

describe('withFields', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2024, 6, 'gregory')
const pym1 = PlainYearMonthFns.withFields(pym0, {
year: 2009,
})
expectPlainYearMonthEquals(pym1, {
calendar: 'gregory',
isoYear: 2009,
isoMonth: 6,
})
})
})

describe('daysInMonth', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 2)
expect(PlainYearMonthFns.daysInMonth(pym)).toBe(29)
})
})

describe('daysInYear', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 2)
expect(PlainYearMonthFns.daysInYear(pym)).toBe(366)
})
})

describe('monthsInYear', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 2)
expect(PlainYearMonthFns.monthsInYear(pym)).toBe(12)
})
})

describe('inLeapYear', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 2)
expect(PlainYearMonthFns.inLeapYear(pym)).toBe(true)
})
})

describe('add', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2024, 2)
const pym1 = PlainYearMonthFns.add(pym0, DurationFns.create(1, 1))
expectPlainYearMonthEquals(pym1, {
isoYear: 2025,
isoMonth: 3,
})
})
})

describe('subtract', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2024, 2)
const pym1 = PlainYearMonthFns.subtract(pym0, DurationFns.create(1, 1))
expectPlainYearMonthEquals(pym1, {
isoYear: 2023,
isoMonth: 1,
})
})
})

describe('until', () => {
it('works without options', () => {
const pym0 = PlainYearMonthFns.create(2024, 4)
const pym1 = PlainYearMonthFns.create(2028, 2)
const d = PlainYearMonthFns.until(pym0, pym1)
expectDurationEquals(d, {
years: 3,
months: 10,
})
})
})

describe('since', () => {
it('works without options', () => {
const pym0 = PlainYearMonthFns.create(2024, 4)
const pym1 = PlainYearMonthFns.create(2028, 2)
const d = PlainYearMonthFns.since(pym0, pym1)
expectDurationEquals(d, {
years: -3,
months: -10,
})
})
})

describe('equals', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2024, 4)
const pym1 = PlainYearMonthFns.create(2028, 2)
expect(PlainYearMonthFns.equals(pym0, pym1)).toBe(false)
expect(PlainYearMonthFns.equals(pym0, pym0)).toBe(true)
})
})

describe('compare', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2024, 4)
const pym1 = PlainYearMonthFns.create(2028, 2)
expect(PlainYearMonthFns.compare(pym0, pym1)).toBe(-1)
expect(PlainYearMonthFns.compare(pym1, pym0)).toBe(1)
expect(PlainYearMonthFns.compare(pym0, pym0)).toBe(0)
})
})

describe('toPlainDate', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2024, 2)
const pd = PlainYearMonthFns.toPlainDate(pym, { day: 10 })
expectPlainDateEquals(pd, {
isoYear: 2024,
isoMonth: 2,
isoDay: 10,
})
})
})

describe('toString', () => {
it('works without options', () => {
const pym = PlainYearMonthFns.create(2024, 2)
const s = PlainYearMonthFns.toString(pym)
expect(s).toBe('2024-02')
})

it('works with options', () => {
const pym = PlainYearMonthFns.create(2024, 2)
const s = PlainYearMonthFns.toString(pym, { calendarName: 'always' })
expect(s).toBe('2024-02-01[u-ca=iso8601]')
})
})

describe('toLocaleString', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2023, 12)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
PlainYearMonthFns.toLocaleString(pym, locale, options),
)
expect(s).toBe('December 2023')
})
})

describe('toLocaleStringParts', () => {
it('works', () => {
const pym = PlainYearMonthFns.create(2023, 12)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
PlainYearMonthFns.toLocaleStringParts(pym, locale, options),
)
expect(parts).toEqual([
{ type: 'month', value: 'December' },
{ type: 'literal', value: ' ' },
{ type: 'year', value: '2023' },
])
})
})

describe('rangeToLocaleString', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2023, 10)
const pym1 = PlainYearMonthFns.create(2023, 12)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const s = testHotCache(() =>
PlainYearMonthFns.rangeToLocaleString(pym0, pym1, locale, options),
)
expect(s).toBe('October – December 2023')
})
})

describe('rangeToLocaleStringParts', () => {
it('works', () => {
const pym0 = PlainYearMonthFns.create(2023, 10)
const pym1 = PlainYearMonthFns.create(2023, 12)
const locale = 'en'
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
timeZone: 'America/New_York',
calendar: 'iso8601', // required unfortunately
}
const parts = testHotCache(() =>
PlainYearMonthFns.rangeToLocaleStringParts(pym0, pym1, locale, options),
)
expect(parts).toEqual([
{ source: 'startRange', type: 'month', value: 'October' },
{ source: 'shared', type: 'literal', value: ' – ' },
{ source: 'endRange', type: 'month', value: 'December' },
{ source: 'shared', type: 'literal', value: ' ' },
{ source: 'shared', type: 'year', value: '2023' },
])
})
})
Loading

0 comments on commit f45c69e

Please sign in to comment.