Skip to content

Commit

Permalink
Merge pull request #1711 from AletheiaFact/setup-cypress-component-te…
Browse files Browse the repository at this point in the history
…sting

feat: Configuration branch to enable the component testing area
  • Loading branch information
thesocialdev authored Feb 19, 2025
2 parents eadf1ae + a1d0fc1 commit 7fe75d4
Show file tree
Hide file tree
Showing 45 changed files with 445 additions and 90 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ jobs:
wait-on: "http://localhost:3000"
env:
CI: true
- name: Run Cypress Component Tests
- run: yarn cypress run --component
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/qs-npm-6.10.4-9b6a538d57-31e4fedd75.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ export default defineConfig({
screenshotOnRunFailure: false,
pageLoadTimeout: 480000,
defaultCommandTimeout: 65000,

e2e: {
baseUrl: process.env.CYPRESS_BASE_URL || "http://localhost:3000",
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require("./cypress/plugins/index.ts").default(on, config);
},
},

component: {
devServer: {
framework: "next",
bundler: "webpack",
},
},
});
14 changes: 14 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
<!-- Used by Next.js to inject CSS. -->
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>
<div data-cy-root></div>
</body>
</html>
39 changes: 39 additions & 0 deletions cypress/support/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')

import { mount } from "cypress/react18";

// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount;
}
}
}

Cypress.Commands.add("mount", mount);

// Example use:
// cy.mount(<MyComponent />)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@babel/polyfill": "^7.12.1",
"@casl/ability": "^6.0.0",
"@compodoc/compodoc": "^1.1.21",
"@cypress/react": "^9.0.0",
"@dhaiwat10/react-link-preview": "^1.9.1",
"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
Expand Down Expand Up @@ -203,7 +204,7 @@
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.5",
"concurrently": "^8.2.1",
"cypress": "^12.17.4",
"cypress": "13.17.0",
"env-cmd": "^10.1.0",
"eslint": "7.0.0",
"eslint-config-next": "^12.0.10",
Expand Down
62 changes: 62 additions & 0 deletions src/components/BackButton.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// <reference types="cypress" />

import React from "react";
import { mount } from "cypress/react";
import BackButton from "../../src/components/BackButton";

describe("BackButton Component", () => {
describe("should not render on home page or root path", () => {
const mockRouter = (pathname: string) => {
cy.stub(require("next/router"), "useRouter").returns({
pathname,
route: pathname,
query: {},
asPath: pathname,
push: cy.stub(),
replace: cy.stub(),
prefetch: cy.stub().resolves(),
});
};

it("should render and handle callback", () => {
const callbackSpy = cy.spy().as("callbackSpy");

mount(<BackButton callback={callbackSpy} isVisible={true} />);

cy.get('[data-cy="testBackButton"]').should("be.visible").click();
cy.get("@callbackSpy").should("have.been.calledOnce");
});

it("should call router.back when no callback is provided", () => {
const routerBackSpy = cy.spy().as("routerBackSpy");

cy.stub(require("next/router"), "useRouter").returns({
pathname: "/another-page",
back: routerBackSpy,
});

mount(<BackButton isVisible={true} />);

cy.get('[data-cy="testBackButton"]').click();
cy.get("@routerBackSpy").should("have.been.calledOnce");
});

it("should not render when isVisible is false", () => {
mount(<BackButton isVisible={false} />);

cy.get('[data-cy="testBackButton"]').should("not.exist");
});

it("should not render on the root path", () => {
mockRouter("/");
mount(<BackButton isVisible={true} />);
cy.get('[data-cy="testBackButton"]').should("not.exist");
});

it("should not render on the home page path", () => {
mockRouter("/home-page");
mount(<BackButton isVisible={true} />);
cy.get('[data-cy="testBackButton"]').should("not.exist");
});
});
});
27 changes: 19 additions & 8 deletions src/components/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { useTranslation } from "next-i18next";
import React, { CSSProperties } from "react";
import { ArrowBackOutlined } from "@mui/icons-material";
import { ArrowBackOutlined } from "@mui/icons-material";
import { useRouter } from "next/router";
import colors from "../styles/colors";

function BackButton({ style, callback }: { style?: CSSProperties; callback?: () => void }) {
function BackButton({
style,
callback,
isVisible = true,
}: {
style?: CSSProperties;
callback?: () => void;
isVisible?: boolean;
}) {
const { t } = useTranslation();
const router = useRouter();
const pathname = router.pathname || "";
const pathname = router?.pathname || "";

if (pathname !== "/" && pathname !== "/home-page") {
console.log("pthname:", pathname);
console.log("isVisible:", isVisible);

if (pathname !== "/" && pathname !== "/home-page" && isVisible) {
return (
<a
style={{
display:"flex",
alignContent:"center",
display: "flex",
alignContent: "center",
fontWeight: "bold",
color: colors.secondary,
...style
...style,
}}
data-cy="testBackButton"
onClick={() => {
Expand All @@ -30,7 +41,7 @@ function BackButton({ style, callback }: { style?: CSSProperties; callback?: ()
}
}}
>
<ArrowBackOutlined fontSize="small"/> {t("common:back_button")}
<ArrowBackOutlined fontSize="small" /> {t("common:back_button")}
</a>
);
} else {
Expand Down
Loading

0 comments on commit 7fe75d4

Please sign in to comment.