Skip to content

Commit

Permalink
Set up Cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
lowercasename committed Oct 6, 2023
1 parent f80e509 commit aad4162
Show file tree
Hide file tree
Showing 13 changed files with 997 additions and 61 deletions.
25 changes: 23 additions & 2 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build test
name: Build and test
on:
pull_request:
workflow_dispatch:
Expand All @@ -7,7 +7,7 @@ on:
- main

jobs:
build:
build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -31,3 +31,24 @@ jobs:
pnpm start &
sleep 5
curl -sSf http://localhost:3000
- name: Upload artefacts
uses: actions/upload-artifact@v3
with:
name: build
path: |
dist
node_modules
cypress:
needs: build_and_test
runs-on: ubuntu-latest
steps:
- name: Download artefacts
uses: actions/download-artifact@v3
with:
name: build
- name: Cypress run
uses: cypress-io/github-action@v6
with:
start: pnpm start
browser: chrome

10 changes: 10 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
baseUrl: "http://localhost:3000",
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
98 changes: 98 additions & 0 deletions cypress/e2e/event.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const eventData = {
eventName: "Your Event Name",
eventLocation: "Event Location",
timezone: "Europe/London",
eventDescription: "Event Description",
eventURL: "https://example.com",
imagePath: "path/to/your/image.jpg", // If you have an image to upload
hostName: "Your Name",
creatorEmail: "[email protected]",
eventGroupCheckbox: false,
eventGroupID: "YourEventGroupID",
eventGroupEditToken: "YourEventGroupEditToken",
interactionCheckbox: true,
joinCheckbox: true,
maxAttendeesCheckbox: true,
maxAttendees: 10,
eventStart: "",
eventEnd: "",
};

describe("Events", () => {
beforeEach(() => {
cy.clearLocalStorage();

cy.visit("/new/event/public");
cy.get("#showNewEventFormButton").click();

cy.get("#eventName").type(eventData.eventName);
cy.get("#eventLocation").type(eventData.eventLocation);
cy.get("#eventStart").click();
// This opens a datepicker, so find the first non-disabled day and click it
cy.get(".datepicker--cell-day:not(.-disabled-)").first().click();
cy.get("#eventStart").invoke("val").as("eventStart");
// Click away from the datepicker to close it
cy.get("#eventName").click();
cy.get("#eventEnd").click();
// This opens a datepicker, so find the last non-disabled day and click it
cy.get(".datepicker--cell-day:not(.-disabled-)").last().click();
cy.get("#eventEnd").invoke("val").as("eventEnd");
// Click away from the datepicker to close it
cy.get("#eventName").click();
// #timezone is a Select2 dropdown, so select the option you want
cy.get("#timezone").select(eventData.timezone, { force: true });

cy.get("#eventDescription").type(eventData.eventDescription);
cy.get("#eventURL").type(eventData.eventURL);
// Upload an image
// if (eventData.imagePath) {
// cy.get("#eventImageUpload").attachFile(eventData.imagePath);
// }

cy.get("#hostName").type(eventData.hostName);
cy.get("#creatorEmail").type(eventData.creatorEmail);

// Check checkboxes based on eventData
if (eventData.eventGroupCheckbox) {
cy.get("#eventGroupCheckbox").check();
cy.get("#eventGroupID").type(eventData.eventGroupID);
cy.get("#eventGroupEditToken").type(eventData.eventGroupEditToken);
}

if (eventData.interactionCheckbox) {
cy.get("#interactionCheckbox").check();
}

if (eventData.joinCheckbox) {
cy.get("#joinCheckbox").check();
}

if (eventData.maxAttendeesCheckbox) {
cy.get("#maxAttendeesCheckbox").check();
cy.get("#maxAttendees").type(eventData.maxAttendees.toString());
}

// Submit the form
cy.get("#newEventFormSubmit").click();
});
it("creates a new event", function () {
// Check that all the data is correct
cy.get(".p-name").should("have.text", eventData.eventName);
cy.get(".p-location").should("have.text", eventData.eventLocation);
cy.get(".p-summary").should("contain.text", eventData.eventDescription);
cy.get("#hosted-by").should(
"contain.text",
`Hosted by ${eventData.hostName}`
);
cy.get("#attendees-alert").should("contain.text", "10 spots remaining");
let [startDate, startTime] = this.eventStart.split(", ");
let [endDate, endTime] = this.eventEnd.split(", ");
// Remove leading zeroes from the times
startTime = startTime.replace(/^0+/, "");
endTime = endTime.replace(/^0+/, "");
cy.get(".dt-duration").should("contain.text", startDate);
cy.get(".dt-duration").should("contain.text", endDate);
cy.get(".dt-duration").should("contain.text", startTime);
cy.get(".dt-duration").should("contain.text", endTime);
});
});
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
Binary file added cypress/fixtures/test-header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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>
// }
// }
// }
20 changes: 20 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.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')
8 changes: 8 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "tsc",
"start": "node dist/start.js",
"dev": "nodemon -e ts,js --watch src --exec \"pnpm run build ; pnpm run start\"",
"test": "echo \"Error: no test specified\" && exit 1"
"test:dev": "pnpm run dev & wait-on http://localhost:3000 && cypress open --e2e --browser chrome",
"test": "pnpm run build || true && pnpm run start & wait-on http://localhost:3000 && cypress run --e2e --browser chrome"
},
"engines": {
"node": ">=16.16.0"
Expand Down Expand Up @@ -38,10 +39,12 @@
"randomstring": "^1.3.0",
"request": "^2.88.2",
"sanitize-html": "^2.11.0",
"toml": "^3.0.0"
"toml": "^3.0.0",
"wait-on": "^7.0.1"
},
"devDependencies": {
"@types/express": "^4.17.18",
"cypress": "^13.3.0",
"eslint": "^8.50.0",
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
Expand Down
Loading

0 comments on commit aad4162

Please sign in to comment.