Skip to content

Commit

Permalink
Added Cypress tests and GitHub action
Browse files Browse the repository at this point in the history
  • Loading branch information
Spytex committed Jul 14, 2023
1 parent ec67f0b commit fe9393f
Show file tree
Hide file tree
Showing 14 changed files with 2,059 additions and 44 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Cypress Tests

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14

- name: Install dependencies
run: npm ci

- name: Run Cypress tests
run: npm run cypress:run
28 changes: 28 additions & 0 deletions cypress/e2e/layout.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe('Layout', () => {
beforeEach(() => {
cy.visit('http://localhost:3000/');
});

it('should navigate to home page when home link is clicked', () => {
cy.contains('Home').click();
cy.url().should('eq', 'http://localhost:3000/');

});
it('should navigate to home page when logo link is clicked', () => {
cy.get('[alt="logo"]').click({ multiple: true, force: true });
cy.url().should('eq', 'http://localhost:3000/');

});

it('should navigate to photos page when photos link is clicked', () => {
cy.contains('Photos').click();
cy.url().should('eq', 'http://localhost:3000/photos');
});

it('should toggle color mode when color mode button is clicked', () => {
cy.get('.chakra-stack > .chakra-button').click();
cy.get('html').should('have.attr', 'data-theme', 'dark');
cy.get('.chakra-stack > .chakra-button').click();
cy.get('html').should('have.attr', 'data-theme', 'light');
});
});
14 changes: 14 additions & 0 deletions cypress/e2e/photoAPI.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('Photo upload/delete', () => {


it('should load a photo and delete it', () => {
cy.visit('http://localhost:3000/');

cy.contains('Add Picture').first().click();
cy.get('input[type="file"]').first().selectFile("cypress/fixtures/image.jpg",{force: true})
cy.contains('File uploaded successfully.').should('be.visible');

cy.visit('http://localhost:3000/photos');
cy.get('[aria-label="Delete"]').first().click();
});
});
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"
}
Empty file added cypress/fixtures/example.txt
Empty file.
Binary file added cypress/fixtures/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'cypress-file-upload';
/// <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>
// }
// }
// }
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 />)
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')
Loading

0 comments on commit fe9393f

Please sign in to comment.