Skip to content

Commit

Permalink
MHV-62731 fixed skipped unit tests (#32658)
Browse files Browse the repository at this point in the history
* MHV-62731 fixed skipped unit tests

* MHV-62731 fixed formatDateAndTime()
  • Loading branch information
KolbyVA authored Oct 28, 2024
1 parent d0ea2d3 commit a40739b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ describe('LabsAndTestsListItem component with radiology record', () => {
});

// This test will give different results when run in different time zones.
it.skip('should display the date of the record', () => {
it('should display the date of the record', () => {
const date = screen.getByText('January 6, 2004, 7:27 p.m.', {
selector: 'div',
exact: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Radiology details component', () => {
});

// This test will give different results when run in different time zones.
it.skip('should display the formatted date', () => {
it('should display the formatted date', () => {
const formattedDate = screen.getByText('January 6, 2004, 7:27 p.m.', {
exact: true,
selector: 'span',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('App', () => {
expect(screen.getByRole('navigation', { name: 'My HealtheVet' }));
});

it.skip('renders the global downtime notification', () => {
it('renders the global downtime notification', () => {
const screen = renderWithStoreAndRouter(<App />, {
initialState: {
...initialState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('getNote', () => {

describe('getDateSigned', () => {
// This test is time-zone dependent and will fail in certain circumstances. Skipping for now.
it.skip('returns formatted date when extension array has an item with valueDateTime', () => {
it('returns formatted date when extension array has an item with valueDateTime', () => {
const mockRecord = {
authenticator: {
extension: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('buildRadiologyResults', () => {
});
});

describe.skip('labsAndTestsReducer', () => {
describe('labsAndTestsReducer', () => {
it('creates a list', () => {
const labsAndTestsResponse = {
entry: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ describe('Date formatter with no timezone', () => {
expect(formattedDate).to.eq('September 29, 2023, 11:04 a.m.');
});

// This test will give different results when run in different time zones.
it.skip('formats an epoch date in the original time without a timezone', () => {
it('formats an epoch date in the original time without a timezone', () => {
const timeStamp = 1605300748000;
const formattedDate = dateFormatWithoutTimezone(timeStamp);
expect(formattedDate).to.eq('November 13, 2020, 8:52 p.m.');
Expand Down Expand Up @@ -451,16 +450,24 @@ describe('getStatusExtractPhase', () => {
});

describe('getLastUpdatedText', () => {
// This test is time-zone dependent and will fail in certain circumstances. Skipping for now.
it.skip('should return the last updated string when the refreshStateStatus contains the extractType and lastSuccessfulCompleted', () => {
it('should return the last updated string when the refreshStateStatus contains the extractType and lastSuccessfulCompleted', () => {
const refreshStateStatus = [
{ extract: 'type1', lastSuccessfulCompleted: '2024-09-15T10:00:00Z' },
];
const extractType = 'type1';

const result = getLastUpdatedText(refreshStateStatus, extractType);

expect(result).to.equal('Last updated at 10:00 AM on 2024-09-15');
const testDate = new Date('2024-09-15T10:00:00Z');

expect(result).to.equal(
`Last updated at ${testDate.getHours() % 12 ||
12}:00 a.m. on ${testDate.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})}`,
);
});

it('should return null when no matching extractType is found', () => {
Expand Down
33 changes: 26 additions & 7 deletions src/applications/mhv-medical-records/util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,29 @@ export const dateFormatWithoutTimezone = datetime => {
if (typeof datetime === 'string' && datetime.includes('-')) {
// Check if datetime has a timezone and strip it off if present
if (datetime.includes('T')) {
withoutTimezone = datetime.substring(0, datetime.lastIndexOf('-'));
withoutTimezone = datetime
.substring(datetime.indexOf('T'), datetime.length)
.includes('-')
? datetime.substring(0, datetime.lastIndexOf('-'))
: datetime.replace('Z', '');
} else {
// Handle the case where the datetime is just a date (e.g., "2000-08-09")
const parsedDate = parseISO(datetime);
if (isValid(parsedDate)) {
return dateFnsFormat(parsedDate, 'MMMM d, yyyy');
return dateFnsFormat(parsedDate, 'MMMM d, yyyy', { in: 'UTC' });
}
}
} else {
withoutTimezone = new Date(datetime).toISOString().replace('Z', '');
}

const parsedDateTime = parseISO(withoutTimezone);
if (isValid(parsedDateTime)) {
const formattedDate = dateFnsFormat(parsedDateTime, 'MMMM d, yyyy, h:mm a');
const formattedDate = dateFnsFormat(
parsedDateTime,
'MMMM d, yyyy, h:mm a',
{ in: 'UTC' },
);
return formattedDate.replace(/AM|PM/, match =>
match.toLowerCase().replace('m', '.m.'),
);
Expand Down Expand Up @@ -343,7 +353,7 @@ export const formatDate = str => {
return str;
}
if (monthRegex.test(str)) {
return dateFnsFormat(parseISO(str), 'MMMM, yyyy');
return dateFnsFormat(parseISO(str), 'MMMM, yyyy', { in: 'UTC' });
}
return formatDateLong(str);
};
Expand All @@ -353,15 +363,24 @@ export const formatDate = str => {
*
* @param {Date} date
*/
export const formatDateAndTime = date => {
export const formatDateAndTime = rawDate => {
let date = rawDate;
if (typeof rawDate === 'string') {
date = new Date(rawDate);
}

const hours = date.getHours();
const minutes = date.getMinutes();
const period = hours >= 12 ? 'p.m.' : 'a.m.';
const formattedHours = hours % 12 || 12; // Convert to 12-hour format
const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes;
const timePart = `${formattedHours}:${formattedMinutes} ${period} ET`;
const timePart = `${formattedHours}:${formattedMinutes} ${period}`;

const options = { year: 'numeric', month: 'long', day: 'numeric' };
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
const datePart = date.toLocaleDateString('en-US', options);

return {
Expand Down

0 comments on commit a40739b

Please sign in to comment.