Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cypress e2e component tests #952

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Cypress
on: [push]
jobs:
cypress-run-windows:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Cypress run
uses: cypress-io/github-action@v5.0.9
with:
component: true
build: npm run build
start: npm start

cypress-run-linux:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Cypress run
uses: cypress-io/github-action@v5.0.9
with:
component: true
build: npm run build
start: npm start

cypress-run-mac:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Cypress run
uses: cypress-io/github-action@v5.0.9
with:
component: true
build: npm run build
start: npm start
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({
component: {
devServer: {
framework: "react",
bundler: "vite",
},
},
});
192 changes: 192 additions & 0 deletions cypress/component/IntegratedUseHotkeys.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { useState } from 'react'
import { useHotkeys } from '../../src'
import { Keys, OptionsOrDependencyArray } from '../../src/types'

type Props = {
keys: Keys
options?: OptionsOrDependencyArray
dependencies?: OptionsOrDependencyArray
}

function trigger(key: string | string[]) {
cy.get('[data-cy=hotkeys]').realClick().realPress(key)
}

const IntegratedUseHotkeys = ({ keys, options, dependencies }: Props) => {
const [count, setCount] = useState(0)

useHotkeys(keys, () => setCount(count + 1), options, dependencies)

return (
<div>
<div data-cy="hotkeys">{count}</div>
</div>
)
}

describe('Combinations', async () => {
it('should integrate with cypress', () => {
cy.mount(<IntegratedUseHotkeys keys={'a'}/>)

trigger('a')

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to mod keys', () => {
cy.mount(<IntegratedUseHotkeys keys={'meta, ctrl, shift, alt'}/>)

trigger('Meta')
trigger('ControlLeft')
trigger('ControlRight')
trigger('ShiftLeft')
trigger('ShiftRight')
trigger('AltLeft')
trigger('AltRight')

cy.get('[data-cy=hotkeys]').should('have.text', '7')
})

it('should listen to combinations with meta', () => {
cy.mount(<IntegratedUseHotkeys keys={'meta+a'}/>)

trigger(['Meta', 'a'])
trigger(['Meta', 'b'])
trigger(['Meta', 'a'])
trigger(['Meta'])
trigger(['a'])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})

it('should listen to combinations with shift', () => {
cy.mount(<IntegratedUseHotkeys keys={'shift+a'}/>)

trigger(['ShiftLeft', 'a'])
trigger(['ShiftLeft', 'b'])
trigger(['ShiftLeft', 'a'])
trigger(['ShiftLeft'])
trigger(['a'])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})

it('should listen to combinations with alt', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+a'}/>)

trigger(['AltLeft', 'a'])
trigger(['AltLeft', 'b'])
trigger(['AltLeft', 'a'])
trigger(['AltLeft'])
trigger(['a'])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})

it('should listen to combinations with backtick', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+`'}/>)

trigger(['AltLeft', '`'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with escape', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+esc'}/>)

trigger(['AltLeft', 'Escape'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with return/enter', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+enter, enter'}/>)

trigger(['AltLeft', 'Enter'])
trigger(['Enter'])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})

it('should listen to combinations with period', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+.'}/>)

trigger(['AltLeft', '.'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with comma', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+,'} options={{splitKey: '-'}}/>)

trigger(['AltLeft', ','])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with slash', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+-'}/>)

trigger(['AltLeft', '-'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with space', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+ , space'}/>)

trigger(['AltLeft', ' '])
trigger([' '])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})

it('should listen to combinations with hashtag', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt+#'}/>)

trigger(['AltLeft', '#'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with +', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt-+'} options={{combinationKey: '-'}}/>)

trigger(['AltLeft', '+'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to combinations with +', () => {
cy.mount(<IntegratedUseHotkeys keys={'alt-+'} options={{combinationKey: '-'}}/>)

trigger(['AltLeft', '+'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to !', () => {
cy.mount(<IntegratedUseHotkeys keys={'!'}/>)

trigger(['!'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to Shift+1', () => {
cy.mount(<IntegratedUseHotkeys keys={'Shift+1'}/>)

trigger(['Shift', '1'])

cy.get('[data-cy=hotkeys]').should('have.text', '1')
})

it('should listen to both Shift+1 and !', () => {
cy.mount(<IntegratedUseHotkeys keys={'Shift+1, !'}/>)

trigger(['Shift', '1'])
trigger(['!'])

cy.get('[data-cy=hotkeys]').should('have.text', '2')
})
})
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": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
39 changes: 39 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <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>
// }
// }
// }

import "cypress-real-events"
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/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 />)
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"license": "MIT",
"scripts": {
"start": "vite",
"build": "tsdx build",
"test": "jest",
"publish": "np",
Expand Down Expand Up @@ -98,13 +99,17 @@
"react-test-renderer": "18.2.0",
"tsdx": "0.14.1",
"tslib": "2.5.0",
"typescript": "4.9.5"
"typescript": "4.9.5",
"vite": "^4.1.2"
},
"peerDependencies": {
"react": ">=16.8.1",
"react-dom": ">=16.8.1"
},
"dependencies": {
"@vitejs/plugin-react": "^3.1.0",
"cypress": "^12.6.0",
"cypress-real-events": "^1.7.6",
"eslint": "^8.34.0",
"eslint-plugin-react": "^7.32.2"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/isHotkeyPressed.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import userEvent from '@testing-library/user-event'
import { isHotkeyPressed } from '../src/isHotkeyPressed'
import { isHotkeyPressed } from '../src'

test('should return true if hotkey is currently pressed down', async () => {
const user = userEvent.setup()
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strict": true,
"allowSyntheticDefaultImports": true,
"outDir": "./dist",
"types": ["cypress", "node", "cypress-real-events"]
},
"include": ["src", "./setupTests.ts"]
}
7 changes: 7 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})
Loading