Skip to content

Commit

Permalink
Merge pull request #1245 from danskernesdigitalebibliotek/DDFFORM-807…
Browse files Browse the repository at this point in the history
…-optimer-opening-hours-cypress-test

DDFFORM-807 - Enable `initialDate` parameter in opening hours apps (For Cypress testing)
  • Loading branch information
kasperbirch1 authored Nov 8, 2024
2 parents c51cdbd + 848af19 commit 38dff1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/apps/opening-hours-editor/OpeningHoursEditor.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import OpeningHoursEditor, {
OpeningHoursEditorType
} from "./OpeningHoursEditor";
import { withConfig } from "../../core/utils/config";
import { getInitialDateFromUrl } from "./helper";

interface OpeningHoursEditorEntryTextProps {
openingHoursRemoveEventButtonText: string;
Expand Down Expand Up @@ -36,8 +37,13 @@ const OpeningHoursEditorEntry: React.FC<
OpeningHoursEditorEntryTextProps &
OpeningHoursEditorType &
OpeningHoursEditorEntryConfigProps
> = ({ initialDate = new Date() }) => {
return <OpeningHoursEditor initialDate={initialDate} />;
> = ({ initialDate }) => {
const initialDateParam = getInitialDateFromUrl();
return (
<OpeningHoursEditor
initialDate={initialDate ?? (initialDateParam || new Date())}
/>
);
};

export default withConfig(withUrls(withText(OpeningHoursEditorEntry)));
22 changes: 22 additions & 0 deletions src/apps/opening-hours-editor/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,25 @@ export const isOpeningHourWeeklyRepetition = (
DplOpeningHoursListGET200ItemRepetitionType.weekly
);
};

// This used for cypress testing in in cms where we test the complete flow of
// opening hours between the editor (Admin) and the calendar (Frontend)
export const getInitialDateFromUrl = (): Date | null => {
const query = new URLSearchParams(window.location.search);
const initialDateString = query.get("initialDate");

if (!initialDateString) {
return null;
}

const date = new Date(initialDateString);
if (!Number.isNaN(date.getTime())) {
return date;
}
// eslint-disable-next-line no-console
console.debug(
"Invalid date format in URL parameter: initialDate =",
initialDateString
);
return null;
};
12 changes: 9 additions & 3 deletions src/apps/opening-hours/OpeningHours.entry.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import GuardedApp from "../../components/guarded-app";
import { GlobalEntryTextProps } from "../../core/storybook/globalTextArgs";
import { withConfig } from "../../core/utils/config";
import { withText } from "../../core/utils/text";
import { withUrls } from "../../core/utils/url";
import OpeningHours from "./OpeningHours";
import { getInitialDateFromUrl } from "../opening-hours-editor/helper";

export interface OpeningHoursEntryProps {
branchId: number;
Expand All @@ -17,8 +17,14 @@ export interface OpeningHoursEntryProps {

const OpeningHoursEntry: React.FC<
OpeningHoursEntryProps & GlobalEntryTextProps
> = ({ branchId, initialDate = new Date() }) => {
return <OpeningHours branchId={branchId} initialDate={initialDate} />;
> = ({ branchId, initialDate }) => {
const initialDateParam = getInitialDateFromUrl();
return (
<OpeningHours
branchId={branchId}
initialDate={initialDate ?? (initialDateParam || new Date())}
/>
);
};

export default withConfig(withUrls(withText(OpeningHoursEntry)));

0 comments on commit 38dff1f

Please sign in to comment.