Skip to content

Commit

Permalink
fix: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Oct 2, 2024
1 parent 5847d55 commit 37c5b90
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bottom-bar/form-expiry-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function FormExpiryInfo() {
calendar,
timezone,
})
const dateTime = `lockDate (${timezone})`
const dateTime = `${lockDate} (${timezone})`

return (
<>
Expand Down
78 changes: 78 additions & 0 deletions src/bottom-bar/form-expiry-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useConfig } from '@dhis2/app-runtime'
import { render as testingLibraryRender } from '@testing-library/react'
import React from 'react'
import { useLockedContext } from '../shared/locked-status/use-locked-context.js'
import { render } from '../test-utils/render.js'
import FormExpiryInfo from './form-expiry-info.js'

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

jest.mock('../shared/locked-status/use-locked-context.js', () => ({
...jest.requireActual('../shared/locked-status/use-locked-context.js'),
useLockedContext: jest.fn(() => ({
locked: false,
lockStatus: { lockDate: '2024-03-15T14:15:00' },
})),
}))

describe('FormExpiryInfo', () => {
beforeEach(() => {
jest.useFakeTimers('modern')
jest.setSystemTime(new Date('2024-03-15T12:15:00'))
})

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

it('shows nothing if locked', () => {
useLockedContext.mockImplementationOnce(() => ({
locked: true,
lockStatus: { lockDate: '2024-03-15T14:15:00' },
}))
// render without the wrapper as we just want to test this element is empty and wrapper introduces some extra divs
const { container } = testingLibraryRender(<FormExpiryInfo />)

expect(container).toBeEmptyDOMElement()
})

it('shows relative time for lockedDate, if not locked, there is a lockDate, and calendar:gregory', () => {
const { getByText } = render(<FormExpiryInfo />)

expect(getByText('Closes in 2 hours')).toBeInTheDocument()
})

it('shows absolute time for lockedDate, if not locked, there is a lockDate, and calendar not gregory', () => {
useConfig.mockImplementation(() => ({
systemInfo: {
serverTimeZoneId: 'Africa/Addis_Ababa',
calendar: 'ethiopian',
},
}))
const { getByText } = render(<FormExpiryInfo />)

expect(
getByText('Closes 2024-03-15T14:15:00 (Africa/Addis_Ababa)')
).toBeInTheDocument()
})

it('corrects relative time for time zone differences', () => {
useConfig.mockImplementation(() => ({
systemInfo: {
serverTimeZoneId: 'America/Port-au-Prince',
calendar: 'gregory',
},
}))
const { getByText } = render(<FormExpiryInfo />)

// current browser time: 2024-03-15T12:15:00 UTC
// which is 2024-03-15T08:15:00 Port-au-Prince (GMT-4 due to DST)
expect(getByText('Closes in 6 hours')).toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ jest.mock('@dhis2/app-runtime', () => ({
})),
}))

// jest.mock('../../shared/date/get-now-in-calendar.js', () => ({
// getNowInCalendarString: jest.fn(() => '2020-07-01'),
// }))

jest.mock(
'../../shared/use-context-selection/use-context-selection.js',
() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { generateMatrixGroupedByCategory } from './generate-matrix-grouped-by-ca
import { generateMatrixTransposed } from './generate-matrix-transposed.js'

export const generateFormMatrix = (options, displayOptions) => {
console.debug('generating pivoted form', displayOptions)
const groupedBy =
displayOptions.pivotMode === 'move_categories'
? [displayOptions.pivotedCategory]
Expand Down
4 changes: 3 additions & 1 deletion src/data-workspace/data-details-sidebar/audit-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useDataValueContext from './use-data-value-context.js'
jest.mock('@dhis2/app-runtime', () => ({
...jest.requireActual('@dhis2/app-runtime'),
useConfig: jest.fn(() => ({
systemInfo: { serverTimeZoneId: 'Etc/UTC' },
systemInfo: { serverTimeZoneId: 'Africa/Cairo' },
})),
}))

Expand Down Expand Up @@ -132,5 +132,7 @@ describe('<AuditLog />', () => {
{ selector: '.entry:nth-child(1) .entryMessage' }
)
expect(thirdChangeEl).toBeInTheDocument()
// check that note about time zone appears
expect('audit dates are given in Africa/Cairo time').toBeInTheDocument()
})
})
25 changes: 25 additions & 0 deletions src/data-workspace/data-details-sidebar/basic-information.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useConfig } from '@dhis2/app-runtime'
import React from 'react'
import useHighlightedField from '../../shared/highlighted-field/use-highlighted-field.js'
import { render } from '../../test-utils/index.js'
Expand Down Expand Up @@ -96,6 +97,30 @@ describe('<BasicInformation />', () => {
expect(getByText('a minute ago', { exact: false })).toBeInTheDocument()
})

it('renders when item was last updated (absolute time stamp with time zone) if non-gregory calendar', () => {
useConfig.mockImplementation(() => ({
systemInfo: {
serverTimeZoneId: 'Africa/Abidjan',
calendar: 'nepali',
},
}))

const { getByText } = render(
<BasicInformation
item={item}
onMarkForFollowup={noop}
onUnmarkForFollowup={noop}
/>
)

expect(getByText(item.storedBy, { exact: false })).toBeInTheDocument()
expect(
getByText('2022-06-28T14:51:14.435 (Africa/Abidjan)', {
exact: false,
})
).toBeInTheDocument()
})

it('renders the item description if one is provided', () => {
const description = 'this is the very helpful description'
const itemWithDescription = {
Expand Down
9 changes: 6 additions & 3 deletions src/shared/locked-status/use-check-lock-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ const getFrontendLockStatus = ({
return
}

let serverLockDateString
let serverLockDateString = null

// this will be a date string corrected for client/server time zone differences
const currentDateString = getNowInCalendarString({ calendar, timezone })
const currentDateString = getNowInCalendarString({
calendar,
timezone,
long: true,
})

if (dataInputPeriods.length > 0) {
const applicableDataInputPeriod =
Expand All @@ -70,7 +74,6 @@ const getFrontendLockStatus = ({
// as "server dates"

// date comparison

if (
(openingDate &&
isDateALessThanDateB(currentDateString, openingDate, {
Expand Down
Loading

0 comments on commit 37c5b90

Please sign in to comment.