Skip to content

Commit

Permalink
fix: tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Sep 27, 2024
1 parent 968311e commit 7581c0e
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 28 deletions.
298 changes: 298 additions & 0 deletions src/context-selection/period-selector-bar-item/use-periods.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useConfig } from '@dhis2/app-runtime'
import { renderHook } from '@testing-library/react-hooks'
import usePeriods from './use-periods.js'

jest.mock('@dhis2/app-runtime', () => ({
...jest.requireActual('@dhis2/app-runtime'),
useConfig: jest.fn(() => ({
systemInfo: { serverTimeZoneId: 'Etc/UTC', calendar: 'gregory' },
})),
}))

jest.mock('../../shared/use-user-info/use-user-info.js', () => ({
useUserInfo: jest.fn(() => ({
data: {
Expand Down Expand Up @@ -279,3 +287,293 @@ describe('usePeriods', () => {
expect(result.current).toHaveLength(54)
})
})

describe('usePeriods (ethiopian)', () => {
beforeEach(() => {
jest.useFakeTimers('modern')
jest.setSystemTime(new Date('2024-07-15T12:00:00').getTime())
useConfig.mockImplementation(() => ({
systemInfo: { calendar: 'ethiopian', timeZone: 'Etc/UTC' },
}))
})

afterEach(() => {
jest.clearAllMocks()
jest.useRealTimers()
})

it('should return a list of daily periods', () => {
const periodType = 'DAILY'
const openFuturePeriods = 0
const year = 2015
const dateLimit = '2015-02-30'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(59)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2015-02-29',
startDate: '2015-02-29',
id: '20150229',
})
)
expect(result.current[58]).toEqual(
expect.objectContaining({
endDate: '2015-01-01',
startDate: '2015-01-01',
id: '20150101',
})
)
})

it('should return a list of weekly periods', () => {
const periodType = 'WEEKLY'
const openFuturePeriods = 0
const year = 2015
const dateLimit = '2015-02-30'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(9)
expect(result.current[0]).toEqual(
expect.objectContaining({
startDate: '2015-02-22',
id: '2015W8',
endDate: '2015-02-28',
})
)
expect(result.current[8]).toEqual(
expect.objectContaining({
startDate: '2014-13-01',
id: '2014W52',
endDate: '2015-01-02',
})
)
})

it('should return a list of monthly periods', () => {
const periodType = 'MONTHLY'
const openFuturePeriods = 0
const year = 2015
const dateLimit = '2015-07-16'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(6)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2015-06-30',
startDate: '2015-06-01',
id: '201506',
name: 'Yekatit 2015',
})
)
expect(result.current[5]).toEqual(
expect.objectContaining({
endDate: '2015-01-30',
startDate: '2015-01-01',
id: '201501',
name: 'Meskerem 2015',
})
)
})

it('should return a list of yearly periods', () => {
const periodType = 'YEARLY'
const openFuturePeriods = 0
const year = 2016
const dateLimit = '2016-08-16'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(53)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2015-13-06',
startDate: '2015-01-01',
id: '2015',
})
)
expect(result.current[52]).toEqual(
expect.objectContaining({
endDate: '1963-13-06',
startDate: '1963-01-01',
id: '1963',
})
)
})
})

describe('usePeriods (nepali)', () => {
beforeEach(() => {
jest.useFakeTimers('modern')
jest.setSystemTime(new Date('2024-07-15T12:00:00').getTime())
useConfig.mockImplementation(() => ({
systemInfo: { calendar: 'nepali', timeZone: 'Etc/UTC' },
}))
})

afterEach(() => {
jest.clearAllMocks()
jest.useRealTimers()
})

it('should return a list of daily periods', () => {
const periodType = 'DAILY'
const openFuturePeriods = 0
const year = 2084
const dateLimit = '2084-02-31'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(61)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2084-02-30',
startDate: '2084-02-30',
id: '20840230',
})
)
expect(result.current[60]).toEqual(
expect.objectContaining({
endDate: '2084-01-01',
startDate: '2084-01-01',
id: '20840101',
})
)
})

it('should return a list of weekly periods', () => {
const periodType = 'WEEKLY'
const openFuturePeriods = 0
const year = 2084
const dateLimit = '2084-02-30'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(8)
expect(result.current[0]).toEqual(
expect.objectContaining({
startDate: '2084-02-17',
id: '2084W8',
endDate: '2084-02-23',
})
)
expect(result.current[7]).toEqual(
expect.objectContaining({
startDate: '2083-12-29',
id: '2084W1',
endDate: '2084-01-05',
})
)
})

it('should return a list of monthly periods', () => {
const periodType = 'MONTHLY'
const openFuturePeriods = 0
const year = 2084
const dateLimit = '2084-07-16'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(6)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2084-06-30',
startDate: '2084-06-01',
id: '208406',
name: 'Ashwin 2084',
})
)
expect(result.current[5]).toEqual(
expect.objectContaining({
endDate: '2084-01-31',
startDate: '2084-01-01',
id: '208401',
name: 'Baisakh 2084',
})
)
})

it.skip('should return a list of yearly periods', () => {
const periodType = 'YEARLY'
const openFuturePeriods = 0
const year = 2084
const dateLimit = '2084-08-16'

const { result } = renderHook(() =>
usePeriods({
periodType,
openFuturePeriods,
year,
dateLimit,
})
)

expect(result.current).toHaveLength(53)
expect(result.current[0]).toEqual(
expect.objectContaining({
endDate: '2083-12-30',
startDate: '2083-01-01',
id: '2083',
})
)
expect(result.current[52]).toEqual(
expect.objectContaining({
endDate: '1963-13-06',
startDate: '1963-01-01',
id: '1963',
})
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const BasicInformation = ({ item }) => {

// if item.lastUpdated is "undefined", getRelativeTime returns null
// and this will not be displayed
// may not be translated: https://dhis2.atlassian.net/browse/TECH-1461
const timeAgo = getRelativeTime({
startDate: item.lastUpdated,
calendar,
Expand Down
36 changes: 28 additions & 8 deletions src/shared/date/date-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ const GREGORY_CALENDARS = ['gregory', 'gregorian', 'iso8601', 'julian'] // calen
const DAY_MS = 24 * 60 * 60 * 1000
const DATE_ONLY_REGEX = new RegExp(/^\d{4}-\d{2}-\d{2}$/)

export const padWithZeros = (startValue, minLength) => {
try {
const startString = String(startValue)
return startString.padStart(minLength, '0')
} catch (e) {
console.error(e)
return startValue
}
}
const formatDate = (date, withoutTimeStamp) => {
const yearString = String(date.getFullYear())?.padStart(4, '0')
const monthString = String(date.getMonth() + 1)?.padStart(2, '0') // Jan = 0
const dayString = String(date.getDay())?.padStart(2, '0')
const hoursString = String(date.getHours())?.padStart(2, '0')
const minuteString = String(date.getMinutes())?.padStart(2, '0')
const secondsString = String(date.getSeconds())?.padStart(3, '0')
const yearString = padWithZeros(date.getFullYear(), 4)
const monthString = padWithZeros(date.getMonth() + 1, 2) // Jan = 0
const dateString = padWithZeros(date.getDate(), 2)
const hoursString = padWithZeros(date.getHours(), 2)
const minuteString = padWithZeros(date.getMinutes(), 2)
const secondsString = padWithZeros(date.getSeconds(), 2)

if (withoutTimeStamp) {
return `${yearString}-${monthString}-${dayString}`
return `${yearString}-${monthString}-${dateString}`
}
return `${yearString}-${monthString}-${dayString}T${hoursString}:${minuteString}:${secondsString}`
return `${yearString}-${monthString}-${dateString}T${hoursString}:${minuteString}:${secondsString}`
}

// returns string in either 'YYYY-MM-DD' or 'YYYY-MM-DDTHH:MM.SSS' format (depending on input)
Expand Down Expand Up @@ -89,6 +98,17 @@ export const isDateALessThanDateB = (
const dateADate = new Date(dateAString)
const dateBDate = new Date(dateBString)

// if dates are invalid, return null
if (isNaN(dateADate)) {
console.error(`Invalid date: ${dateA}`)
return null
}

if (isNaN(dateBDate)) {
console.error(`Invalid date: ${dateB}`)
return null
}

if (inclusive) {
return dateADate <= dateBDate
} else {
Expand Down
Loading

0 comments on commit 7581c0e

Please sign in to comment.