Skip to content

Commit

Permalink
feat: Add Cypress Component Tests 🧪 pull request #24 from 1-Harshit/c…
Browse files Browse the repository at this point in the history
…ypress

feat: Add Cypress Component Tests 🧪
  • Loading branch information
rashmigr01 authored Oct 10, 2023
2 parents 6077f90 + c9d9401 commit f6d41a3
Show file tree
Hide file tree
Showing 28 changed files with 1,083 additions and 40 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/cypress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Cypress Tests

on: push

jobs:
cypress-run:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
# Install NPM dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action@v6
with:
build: yarn build
start: yarn start
record: true
parallel: true
component: true
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 0 additions & 2 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:

- name: Build
run: npm run build
env:
CI: false

- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v3
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/pr_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ jobs:

- name: Build and check lint
run: npm run build

# - name: Test
# run: npm run test
11 changes: 11 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "cypress";

export default defineConfig({
projectId: 'ug3igq',
component: {
devServer: {
framework: "create-react-app",
bundler: "webpack",
},
},
});
37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
12 changes: 12 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!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>
</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/react'

// 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 />)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"apexcharts": "^3.35.2",
"cypress": "^13.3.0",
"framer-motion": "^4.1.17",
"react": "^17.0.2",
"react-apexcharts": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/AlignCenterButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={handleAlignCenterClick}>
<Button color={textColor} onClick={handleAlignCenterClick} id="align-center-button">
<IconBox
icon={
<Icon
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/AlignJustifyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={handleAlignJustifyClick}>
<Button color={textColor} onClick={handleAlignJustifyClick} id="align-justify-button">
<IconBox
icon={
<Icon
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/AlignLeftButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={handleAlignLeftClick}>
<Button color={textColor} onClick={handleAlignLeftClick} id="align-left-button">
<IconBox
icon={
<Icon w="18px" h="18px" as={MdFormatAlignLeft} color={textColor} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/AlignRightButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={handleAlignRightClick}>
<Button color={textColor} onClick={handleAlignRightClick} id="align-right-button">
<IconBox
icon={
<Icon w="18px" h="18px" as={MdFormatAlignRight} color={textColor} />
Expand Down
7 changes: 6 additions & 1 deletion src/components/button/BoldButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button fontWeight="bold" color={textColor} onClick={handleBoldClick}>
<Button
fontWeight="bold"
color={textColor}
onClick={handleBoldClick}
id="bold-button"
>
B
</Button>
</Tooltip>
Expand Down
7 changes: 6 additions & 1 deletion src/components/button/ItalicButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button fontStyle="italic" color={textColor} onClick={handleBoldClick}>
<Button
fontStyle="italic"
color={textColor}
onClick={handleBoldClick}
id="italic-button"
>
I
</Button>
</Tooltip>
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/OrderedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={OrderedList}>
<Button color={textColor} onClick={OrderedList} id="ordered-list-button">
<IconBox
icon={
<Icon
Expand Down
6 changes: 3 additions & 3 deletions src/components/button/UnderlineButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export default function Default() {
bg={boxBg}
>
<Button
// underline
textDecoration="underline"
textDecoration="underline"
color={textColor}
onClick={handleUnderlineClick}
id="underline-button"
>
U
</Button>
</Tooltip>
);
}
}
2 changes: 1 addition & 1 deletion src/components/button/UnorderedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Default() {
color={textColor}
bg={boxBg}
>
<Button color={textColor} onClick={UnorderedList}>
<Button color={textColor} onClick={UnorderedList} id="unordered-list-button">
<IconBox
icon={
<Icon
Expand Down
1 change: 1 addition & 0 deletions src/components/card/MiniStatistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function Default(props: {
{name}
</StatLabel>
<StatNumber
id="word-count"
color={textColor}
fontSize={{
base: '2xl'
Expand Down
11 changes: 11 additions & 0 deletions src/types/cypress.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mount } from "cypress/react";

declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount;
}
}
}

Cypress.Commands.add("mount", mount);
1 change: 1 addition & 0 deletions src/views/editor/default/components/EditorArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function EditorArea(props: Props) {
id="editor-main"
onInput={handleContentChange}
onBlur={handleContentChange}
onFocus={handleContentChange}
>
{editorDefaultData}
</Box>
Expand Down
2 changes: 1 addition & 1 deletion src/views/editor/default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { MdFileCopy } from "react-icons/md";
import EditorArea from "views/editor/default/components/EditorArea";
import { useState } from "react";

export default function UserReports() {
export default function EditorHome() {
// Chakra Color Mode
const brandColor = useColorModeValue("brand.500", "white");
const boxBg = useColorModeValue("secondaryGray.300", "whiteAlpha.100");
Expand Down
87 changes: 87 additions & 0 deletions src/views/editor/default/indexEditorHome-alignment.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from "react";
import EditorHome from "./index";
import { wordCount } from "./variables/editorDefaultData";
import { ChakraProvider } from "@chakra-ui/react";
import theme from "../../../theme/theme";

beforeEach(() => {
cy.viewport(1920, 1080);
// see: https://on.cypress.io/mounting-react
cy.mount(
<ChakraProvider theme={theme}>
<EditorHome />
</ChakraProvider>,
);

// focus on editor-area
cy.get("#editor-main").click();

// count number words in defaultData whihc is jsx element rendering
cy.get("#word-count").should("have.text", wordCount);

// clear editor
// click on cy.get('#menu-button-24')
cy.get("#menu-button-24").click();
cy.get("#menu-list-24-menuitem-22 > .css-70qvj9 > .chakra-text").click();

// editor should be empty
cy.get("#editor-main").should("have.text", "");

// add text to editor
cy.get("#editor-main").type("Ed Eater Testing");

// count number words in editor
cy.get("#word-count").should("have.text", "3");

// select text
cy.get("#editor-main").type("{selectall}");
});

describe("<EditorHome Align-Left />", () => {
it("renders", () => {
// text align - left
cy.get("#align-left-button").click();
// the html formatting must be empty as text-align: left is default
cy.get("#editor-main").should("contain.html", 'Ed Eater Testing')
});
});

describe("<EditorHome Align-Right />", () => {
it("renders", () => {
// text align - right
cy.get("#align-right-button").click();
// the html formatting must be text-align:right as set by the button
cy.get("#editor-main").should("contain.html", '<div style="text-align: right;">');
});
});

describe("<EditorHome Align-Left-After-Right />", () => {
it("renders", () => {
// text align -right
cy.get("#align-right-button").click();
// select all text
cy.get("#editor-main").type("{selectall}");
// text align - left
cy.get("#align-left-button").click();
// the html formatting must be empty as text-align:right is removed when
cy.get("#editor-main").should("contain.html", 'Ed Eater Testing')
});
});

describe("<EditorHome Align-Center />", () => {
it("renders", () => {
// text align - right
cy.get("#align-center-button").click();
// the html formatting must be text-align:center as set by the button
cy.get("#editor-main").should("contain.html", '<div style="text-align: center;">');
});
});

describe("<EditorHome Align-Justify />", () => {
it("renders", () => {
// text align - right
cy.get("#align-justify-button").click();
// the html formatting must be text-align:justify as set by the button
cy.get("#editor-main").should("contain.html", '<div style="text-align: justify;">');
});
});
Loading

0 comments on commit f6d41a3

Please sign in to comment.