Skip to content

Commit

Permalink
Flere tester for dateutils. Litt rydding (#6779)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallvardastark authored Nov 9, 2024
1 parent 6707a81 commit 5da965d
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
minLength,
required,
} from '@fpsak-frontend/utils';
import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { ISO_DATE_FORMAT } from '@k9-sak-web/lib/dateUtils/formats.js';
import { Aksjonspunkt, Behandling, SubmitCallback } from '@k9-sak-web/types';
import OpplysningerFraSøknaden, { Måned } from '@k9-sak-web/types/src/opplysningerFraSoknaden';
Expand Down Expand Up @@ -146,11 +146,11 @@ const OppgittOpptjeningRevurderingForm = (props: Partial<Props> & InjectedFormPr
<Tabs.List>
{oppgittOpptjening.måneder.map((currentOppgittOpptjening, currentOppgittOpptjeningIndex) => (
<Tabs.Tab
key={`${dateFormat(currentOppgittOpptjening.måned.fom)} - ${dateFormat(
key={`${formatDate(currentOppgittOpptjening.måned.fom)} - ${formatDate(
currentOppgittOpptjening.måned.tom,
)}`}
value={`${currentOppgittOpptjeningIndex}`}
label={`${dateFormat(currentOppgittOpptjening.måned.fom)} - ${dateFormat(
label={`${formatDate(currentOppgittOpptjening.måned.fom)} - ${formatDate(
currentOppgittOpptjening.måned.tom,
)}`}
onClick={() => setActiveTab(currentOppgittOpptjeningIndex)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import advarselIcon from '@fpsak-frontend/assets/images/advarsel.svg';
import vilkarUtfallType from '@fpsak-frontend/kodeverk/src/vilkarUtfallType';
import { Image } from '@fpsak-frontend/shared-components';
import hentAktivePerioderFraVilkar from '@fpsak-frontend/utils/src/hentAktivePerioderFraVilkar';
import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { Aksjonspunkt, Fagsak, Opptjening, OpptjeningBehandling, SubmitCallback, Vilkar } from '@k9-sak-web/types';
import { SideMenu } from '@navikt/ft-plattform-komponenter';
import classNames from 'classnames/bind';
Expand Down Expand Up @@ -77,7 +77,7 @@ const OpptjeningVilkarProsessIndex = ({
<SideMenu
links={perioder.map(({ periode, vilkarStatus }, index) => ({
active: activeTab === index,
label: `${dateFormat(periode.fom)} - ${dateFormat(periode.tom)}`,
label: `${formatDate(periode.fom)} - ${formatDate(periode.tom)}`,
iconSrc:
isAksjonspunktOpen && vilkarStatus.kode === vilkarUtfallType.IKKE_VURDERT ? (
<Image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import vilkarUtfallType from '@fpsak-frontend/kodeverk/src/vilkarUtfallType';
import { useFeatureToggles } from '@fpsak-frontend/shared-components';
import hentAktivePerioderFraVilkar from '@fpsak-frontend/utils/src/hentAktivePerioderFraVilkar';
import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { Aksjonspunkt, Behandling, KodeverkMedNavn, SubmitCallback, Vilkar } from '@k9-sak-web/types';
import { SideMenu } from '@navikt/ft-plattform-komponenter';
import classNames from 'classnames/bind';
Expand Down Expand Up @@ -102,7 +102,7 @@ const VilkarresultatMedOverstyringProsessIndex = ({
<SideMenu
links={perioder.map((periode, index) => ({
active: activeTab === index,
label: `${dateFormat(periode.periode.fom)} - ${dateFormat(periode.periode.tom)}`,
label: `${formatDate(periode.periode.fom)} - ${formatDate(periode.periode.tom)}`,
}))}
onClick={setActiveTab}
theme="arrow"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SideMenu } from '@navikt/ft-plattform-komponenter';
import hentAktivePerioderFraVilkar from '@fpsak-frontend/utils/src/hentAktivePerioderFraVilkar';
import { utledInnsendtSoknadsfrist } from './utils';

import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { initializeDate } from '@k9-sak-web/lib/dateUtils/initializeDate.js';
import messages from '../i18n/nb_NO.json';
import styles from './SoknadsfristVilkarProsessIndex.module.css';
Expand Down Expand Up @@ -164,7 +164,7 @@ const SoknadsfristVilkarProsessIndex = ({
<SideMenu
links={perioder.map(({ periode, vilkarStatus }, index) => ({
active: activeTab === index,
label: `${dateFormat(periode.fom)} - ${dateFormat(periode.tom)}`,
label: `${formatDate(periode.fom)} - ${formatDate(periode.tom)}`,
icon:
(erOverstyrt || harÅpentAksjonspunkt) && vilkarStatus.kode !== vilkarUtfallType.OPPFYLT ? (
<Image src={advarselIcon} className={styles.warningIcon} alt="Aksjonspunkt" />
Expand Down
4 changes: 2 additions & 2 deletions packages/prosess-vilkar-sykdom/src/SykdomProsessIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FlexColumn, FlexContainer } from '@fpsak-frontend/shared-components/ind
import VerticalSpacer from '@fpsak-frontend/shared-components/src/VerticalSpacer';
import FlexRow from '@fpsak-frontend/shared-components/src/flexGrid/FlexRow';
import { Lovreferanse } from '@k9-sak-web/gui/shared/lovreferanse/Lovreferanse.js';
import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import Vilkarperiode from '@k9-sak-web/types/src/vilkarperiode';
import { CheckmarkCircleFillIcon, XMarkOctagonFillIcon } from '@navikt/aksel-icons';
import { Detail, Heading, HStack, Label } from '@navikt/ds-react';
Expand Down Expand Up @@ -78,7 +78,7 @@ const SykdomProsessIndex = ({ perioder, panelTittelKode, lovReferanse }: SykdomP
<SideMenu
links={perioder.map((currentPeriode, currentPeriodeIndex) => ({
active: perioder.findIndex(p => isEqual(p, activePeriode)) === currentPeriodeIndex,
label: `${dateFormat(perioder[currentPeriodeIndex].periode.fom)} - ${dateFormat(
label: `${formatDate(perioder[currentPeriodeIndex].periode.fom)} - ${formatDate(
perioder[currentPeriodeIndex].periode.tom,
)}`,
}))}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dateFormat, timeFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate, timeFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import ErrorEventType from './errorEventType';
import ErrorMessage from './ErrorMessage';
import Formatter from './Formatter';
Expand All @@ -25,7 +25,7 @@ class RestHaltedOrDelayedFormatter implements Formatter<ErrorData> {
}
if (status === 'DELAYED') {
return ErrorMessage.withMessageCode(DELAYED_PROCESS_TASK_MESSAGE_CODE, {
date: dateFormat(eta),
date: formatDate(eta),
time: timeFormat(eta),
message,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/sak-visittkort/src/components/VisittkortLabels.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import diskresjonskodeType from '@fpsak-frontend/kodeverk/src/diskresjonskodeType';
import { Tooltip } from '@fpsak-frontend/shared-components';
import { dateFormat } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { formatDate } from '@k9-sak-web/lib/dateUtils/dateUtils.js';
import { Personopplysninger } from '@k9-sak-web/types';
import { Tag } from '@navikt/ds-react';
import moment from 'moment';
Expand Down Expand Up @@ -30,7 +30,7 @@ const VisittkortLabels = ({
{personopplysninger && personopplysninger.dodsdato && (
<Tooltip content={intl.formatMessage({ id: 'VisittkortLabels.DodTittel' })} alignBottom>
<Tag variant="info" className={styles.etikett}>
<FormattedMessage id="VisittkortLabels.Dod" values={{ dato: dateFormat(personopplysninger.dodsdato) }} />
<FormattedMessage id="VisittkortLabels.Dod" values={{ dato: formatDate(personopplysninger.dodsdato) }} />
</Tag>
</Tooltip>
)}
Expand Down
154 changes: 151 additions & 3 deletions packages/v2/lib/src/dateUtils/dateUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import dayjs from 'dayjs';
import { describe, expect, it } from 'vitest';
import {
TIDENES_ENDE,
addDaysToDate,
calcDays,
calcDaysAndWeeks,
calcDaysAndWeeksWithWeekends,
checkDays,
convertHoursToDays,
dateFormat,
findDifferenceInMonthsAndDays,
formatDate,
formatereLukketPeriode,
getRangeOfMonths,
isValidDate,
Expand All @@ -13,6 +18,20 @@ import {
} from './dateUtils';

describe('dateUtils', () => {
it('Skal kalkulere antall dager mellom to datoer inkludert helger og skrive det ut som uker og dager', () => {
const fom = '2018-04-17';
const tom = '2018-06-02';
const message = '6 uker 5 dager';
expect(calcDaysAndWeeksWithWeekends(fom, tom)).toEqual(message);
});

it('Skal kalkulere antall dager mellom to datoer uten helger og skrive det ut som uker og dager', () => {
const fom = '2018-04-17';
const tom = '2018-06-02';
const message = '6 uker 4 dager';
expect(calcDaysAndWeeks(fom, tom)).toEqual(message);
});

it('Skal splitte et sett med uker og dager i to', () => {
const days = 33;
const weeks = 2;
Expand All @@ -23,7 +42,7 @@ describe('dateUtils', () => {
});
it('Skal formatere en dato til ISO', () => {
const dateTime = '2017-08-02T01:54:25.455';
expect(dateFormat(dateTime)).toEqual('02.08.2017');
expect(formatDate(dateTime)).toEqual('02.08.2017');
});

it('Skal formatere et dato til å vise kun klokkeslett', () => {
Expand Down Expand Up @@ -62,13 +81,25 @@ describe('dateUtils', () => {
expect(findDifferenceInMonthsAndDays(fomDate, tomDate)).toBeUndefined();
});

it('skal kalkulere dager uten å regne med helger', () => {
const start = dayjs('2023-01-01');
const end = dayjs('2023-01-10');
expect(calcDays(start, end)).toBe(7);
});

it('skal kalkulere dager inkludert helger', () => {
const start = dayjs('2023-01-01');
const end = dayjs('2023-01-10');
expect(calcDays(start, end, false)).toBe(10);
});

it('skal konvertere timer til dager og timer', () => {
expect(convertHoursToDays(15)).toEqual({ days: 2, hours: 0 });
expect(convertHoursToDays(16)).toEqual({ days: 2, hours: 1 });
});

it('skal formatere dato korrekt', () => {
expect(dateFormat('2023-01-01')).toBe('01.01.2023');
expect(formatDate('2023-01-01')).toBe('01.01.2023');
});

it('skal dele opp en periode i måneder', () => {
Expand All @@ -87,4 +118,121 @@ describe('dateUtils', () => {
it('skal formatere en lukket periode', () => {
expect(formatereLukketPeriode('2023-01-01/2023-01-10')).toBe('01.01.2023 - 10.01.2023');
});

describe('checkDays', () => {
it('should return formatted string for weeks and days', () => {
expect(checkDays(2, 3)).toBe('2 uker 3 dager');
expect(checkDays(1, 1)).toBe('1 uke 1 dager');
expect(checkDays(0, 1)).toBe('1 dag');
expect(checkDays(1, 0)).toBe('1 uke 0 dager');
expect(checkDays(undefined, undefined)).toBe('Antall uker og dager -');
});
});

describe('calcDays', () => {
it('should calculate days excluding weekends', () => {
expect(calcDays('2022-01-01', '2022-01-10')).toBe(6);
});

it('should calculate days including weekends', () => {
expect(calcDays('2022-01-01', '2022-01-10', false)).toBe(10);
});

it('should return "Antall uker og dager -" if tilDatoPeriode is TIDENES_ENDE', () => {
expect(calcDays('2022-01-01', TIDENES_ENDE)).toBe('Antall uker og dager -');
});
});

describe('convertHoursToDays', () => {
it('should convert hours to days and hours', () => {
expect(convertHoursToDays(16)).toEqual({ days: 2, hours: 1 });
});
});

describe('calcDaysAndWeeks', () => {
it('should calculate days and weeks excluding weekends', () => {
expect(calcDaysAndWeeks('2022-01-01', '2022-01-10')).toBe('1 uke 1 dager');
});

it('should return "Antall uker og dager -" if dates are missing', () => {
expect(calcDaysAndWeeks(undefined, undefined)).toBe('Antall uker og dager -');
});
});

describe('calcDaysAndWeeksWithWeekends', () => {
it('should calculate days and weeks including weekends', () => {
expect(calcDaysAndWeeksWithWeekends('2022-01-01', '2022-01-10')).toBe('1 uke 3 dager');
});
});

describe('splitWeeksAndDays', () => {
it('should split weeks and days correctly', () => {
expect(splitWeeksAndDays(2, 3)).toEqual([
{ weeks: 1, days: 1 },
{ weeks: 1, days: 2 },
]);
});
});

describe('timeFormat', () => {
it('should format time correctly', () => {
expect(timeFormat('2022-01-01T12:34:56')).toBe('12:34');
});
});

describe('addDaysToDate', () => {
it('should add days to date correctly', () => {
expect(addDaysToDate('2022-01-01', 10)).toBe('2022-01-11');
});

it('should return TIDENES_ENDE if dateString is TIDENES_ENDE', () => {
expect(addDaysToDate(TIDENES_ENDE, 10)).toBe(TIDENES_ENDE);
});
});

describe('findDifferenceInMonthsAndDays', () => {
it('should find difference in months and days correctly', () => {
expect(findDifferenceInMonthsAndDays('2022-01-01', '2022-03-15')).toEqual({ months: 2, days: 15 });
});

it('should return undefined if dates are invalid', () => {
expect(findDifferenceInMonthsAndDays('invalid-date', '2022-03-15')).toBeUndefined();
});
});

describe('getRangeOfMonths', () => {
it('should get range of months correctly', () => {
expect(getRangeOfMonths('2022-01', '2022-03')).toEqual([
{ month: 'January', year: '22' },
{ month: 'February', year: '22' },
{ month: 'March', year: '22' },
]);
});
});

describe('isValidDate', () => {
it('should return true for valid date', () => {
expect(isValidDate('2022-01-01')).toBe(true);
});

it('should return false for invalid date', () => {
expect(isValidDate('invalid-date')).toBe(false);
});
});

describe('formatDate', () => {
it('should format date correctly', () => {
expect(formatDate('2022-01-01')).toBe('01.01.2022');
});
});

describe('formatereLukketPeriode', () => {
it('should format closed period correctly', () => {
expect(formatereLukketPeriode('2022-01-01/2022-01-10')).toBe('01.01.2022 - 10.01.2022');
});

it('should return input if period is invalid', () => {
expect(formatereLukketPeriode('invalid-period')).toBe('invalid-period');
});
});
});
4 changes: 1 addition & 3 deletions packages/v2/lib/src/dateUtils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export const calcDaysAndWeeks = (fraDatoPeriode?: string, tilDatoPeriode?: strin
return checkDays(weeks, days);
};

export const formatDate = (date: string): string => initializeDate(date, ISO_DATE_FORMAT).format(DDMMYYYY_DATE_FORMAT);

export const formatPeriod = (fomDate: string, tomDate: string): string =>
`${formatDate(fomDate)} - ${formatDate(tomDate)}`;

Expand All @@ -118,7 +116,7 @@ export const splitWeeksAndDays = (weeks: number, days: number) => {
return returnArray;
};

export const dateFormat = (date: string) => initializeDate(date).format(DDMMYYYY_DATE_FORMAT);
export const formatDate = (date: string) => initializeDate(date).format(DDMMYYYY_DATE_FORMAT);

export const timeFormat = (date: string) => initializeDate(date, '', false, true).format(HHMM_TIME_FORMAT);

Expand Down

0 comments on commit 5da965d

Please sign in to comment.