Skip to content

Commit

Permalink
Check when client side has rendered globally
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-robitaille committed Apr 30, 2024
1 parent 6080ea4 commit 1665d86
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 25 deletions.
8 changes: 7 additions & 1 deletion app/(gcforms)/[locale]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { auth } from "@lib/auth";
import { ClientContexts } from "@clientComponents/globals/ClientContexts";
import { HydrationCheck } from "@clientComponents/globals";

export default async function Layout({ children }: { children: React.ReactNode }) {
const session = await auth();
return <ClientContexts session={session}>{children}</ClientContexts>;
return (
<>
<HydrationCheck />
<ClientContexts session={session}>{children}</ClientContexts>;
</>
);
}
3 changes: 2 additions & 1 deletion app/(language selection)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { languages } from "@i18n/settings";
import { useTranslation } from "@i18n/client";
import Link from "next/link";
import { Fip } from "@clientComponents/globals";
import { themes } from "@clientComponents/globals";
import { themes, HydrationCheck } from "@clientComponents/globals";

import { SiteLogo } from "@serverComponents/icons";

Expand Down Expand Up @@ -46,6 +46,7 @@ const Home = () => {
{" "}
<Fip className="my-0 py-6" />
</header>
<HydrationCheck />
<div className="flex flex-col h-full">
<div id="page-container">
<main id="content">
Expand Down
4 changes: 0 additions & 4 deletions components/clientComponents/forms/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const SubmitButton: React.FC<SubmitButtonProps> = ({
const [formTimerState, { startTimer, checkTimer, disableTimer }] = useFormTimer();
const [submitTooEarly, setSubmitTooEarly] = useState(false);
const screenReaderRemainingTime = useRef(formTimerState.remainingTime);
const [formReady, setFormReady] = useState(false);

// calculate initial delay for submit timer
const secondsBaseDelay = 2;
Expand All @@ -44,13 +43,11 @@ const SubmitButton: React.FC<SubmitButtonProps> = ({
useEffect(() => {
if (!formTimerEnabled && !formTimerState.canSubmit) {
disableTimer();
setFormReady(true);
}
}, [disableTimer, formTimerEnabled, formTimerState.canSubmit]);

useEffect(() => {
if (formTimerEnabled) {
if (formTimerState.timerDelay) setFormReady(true);
// Initiate a callback to ensure that state of submit button is correctly displayed

// Calling the checkTimer modifies the state of the formTimerState
Expand All @@ -65,7 +62,6 @@ const SubmitButton: React.FC<SubmitButtonProps> = ({

return (
<>
{formReady && <div id="form-ready-indicator" aria-hidden={true} />}
<div
className={classNames({
"border-l-2": submitTooEarly,
Expand Down
1 change: 1 addition & 0 deletions components/clientComponents/globals/ClientContexts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";

import { SessionProvider } from "next-auth/react";
import { Session } from "next-auth";
import { AccessControlProvider } from "@lib/hooks/useAccessControl";
Expand Down
12 changes: 12 additions & 0 deletions components/clientComponents/globals/HydrationCheck.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use client";
import { useEffect, useState } from "react";

export const HydrationCheck = () => {
const [hydrationComplete, setHydrationComplete] = useState(false);

useEffect(() => {
setHydrationComplete(true);
}, []);

return <>{hydrationComplete && <div id="hydration-complete" />}</>;
};
1 change: 1 addition & 0 deletions components/clientComponents/globals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { Footer } from "./Footer";
export { SkipLink } from "./SkipLink";
export * as Alert from "./Alert/Alert";
export { ErrorStatus } from "./Alert/Alert";
export { HydrationCheck } from "./HydrationCheck";
2 changes: 0 additions & 2 deletions cypress/e2e/form-builder/add-elements.cy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
describe("Test FormBuilder Add Elements", () => {
beforeEach(() => {
cy.visitPage("/en/form-builder/0000/edit");
// Lang switcher only appears after page hydration
cy.get('[data-testid="lang-switcher"]').should("be.visible");
});

it("Adds a Page Text element", () => {
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/form-builder/form-builder-modal-description.cy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
describe("Form builder modal description", () => {
beforeEach(() => {
cy.visitPage("/en/form-builder/0000/edit");
// Lang switcher only appears after page hydration
cy.get('[data-testid="lang-switcher"]').should("be.visible");
});

it("Renders matching element description in more modal", () => {
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/form-builder/form-builder.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ describe("Test FormBuilder", () => {

it("Designs a form", () => {
cy.visitPage("/en/form-builder/0000/edit");
// Lang switcher only appears after page hydration
cy.get('[data-testid="lang-switcher"]').should("be.visible");
cy.typeInField("#formTitle", "Cypress Test Form");
cy.typeInField(`[aria-label="Introduction"]`, "form description");
cy.get("button").contains("Add").click();
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/form-builder/names-and-titles.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ describe("Form builder names and titles", () => {
beforeEach(() => {
cy.userSession();
cy.visitPage("/en/form-builder/0000/edit");
// Lang switcher only appears after page hydration
cy.get('[data-testid="lang-switcher"]').should("be.visible");
});

it("Autocompletes name with title on focus", () => {
Expand Down
8 changes: 5 additions & 3 deletions cypress/e2e/form-builder/ownership.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ describe("Form Ownership", () => {
describe("Admin User", () => {
beforeEach(() => {
cy.userSession({ admin: true });
cy.visitPage(`/en/form-builder/${formID}/settings/manage`);
});
it("Admin can manage Form Ownership", () => {
cy.visitPage(`/en/form-builder/${formID}/settings/manage`);
cy.get("h2").contains("Manage ownership").should("be.visible");
cy.get("[data-testid='form-ownership']").should("exist");
});

it("Must have at least one owner", () => {
cy.visitPage(`/en/form-builder/${formID}/settings/manage`);
cy.get("h2").contains("Manage ownership").should("be.visible");
cy.get("[data-testid='form-ownership']").should("exist");

cy.get("[aria-label='Remove test.user@cds-snc.ca']").click();
cy.get("[aria-label='Remove test.user@cds-snc.ca']").should("not.exist");
cy.get("[data-testid='form-ownership']").should("not.contain", "test.user@cds-snc.ca");

cy.get("[data-testid='save-ownership']").click();
cy.get("[data-testid='alert']")
.contains("Must assign at least one user")
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/form-builder/share.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ describe("Form builder share", () => {
beforeEach(() => {
cy.userSession();
cy.visitPage("/en/form-builder/0000/edit");
// Lang switcher only appears after page hydration
cy.get('[data-testid="lang-switcher"]').should("be.visible");
});

it("Renders share flyout with name check", () => {
Expand Down
4 changes: 0 additions & 4 deletions cypress/e2e/your_account_user_profile.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe("User profile", () => {

it("Can navigate to Profile page", () => {
cy.visitPage("/en/forms");
cy.get('[data-testid="resume-editing-form"]').should("not.exist");
cy.get("button[id^='radix-']").click();
cy.get("[data-testid='yourAccountDropdownContent']").contains("Profile").click();
cy.url().should("include", "/profile");
Expand All @@ -22,7 +21,6 @@ describe("User profile", () => {
});
it("Renders the My Account dropdown as non-admin", () => {
cy.visitPage("/en/forms");
cy.get('[data-testid="resume-editing-form"]').should("not.exist");
cy.get("[data-testid='yourAccountDropdownContent']").should("not.exist");
cy.get("button[id^='radix-']").click();
cy.get("[data-testid='yourAccountDropdownContent']").should("be.visible");
Expand All @@ -38,7 +36,6 @@ describe("User profile", () => {
});
it("Can navigate to Profile page", () => {
cy.visitPage("/en/forms");
cy.get('[data-testid="resume-editing-form"]').should("not.exist");
cy.get("button[id^='radix-']").click();
cy.get("[data-testid='yourAccountDropdownContent']").contains("Profile").click();
cy.url().should("include", "/profile");
Expand All @@ -49,7 +46,6 @@ describe("User profile", () => {
});
it("Renders the My Account dropdown as admin", () => {
cy.visitPage("/en/forms");
cy.get('[data-testid="resume-editing-form"]').should("not.exist");
cy.get("[data-testid='yourAccountDropdownContent']").should("not.exist");
cy.get("button[id^='radix-']").click();
cy.get("[data-testid='yourAccountDropdownContent']").should("be.visible");
Expand Down
3 changes: 1 addition & 2 deletions cypress/support/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ Cypress.Commands.add("useForm", (file, published = true) => {
*/
Cypress.Commands.add("visitForm", (formID, language = "en") => {
cy.visitPage(`/${language}/id/${formID}`);
cy.get("main").should("be.visible");
cy.get("#form-ready-indicator").should("exist");
});

/**
Expand All @@ -134,6 +132,7 @@ Cypress.Commands.add("visitPage", (path) => {

cy.get("#react-hydration-loader").should("not.exist");
cy.get("main").should("be.visible");
cy.get("#hydration-complete").should("exist");
// cy.waitForIdleNetwork();
});

Expand Down

0 comments on commit 1665d86

Please sign in to comment.