Skip to content

Commit

Permalink
test: cypress setup and dropdown test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lombardoc4 committed Jan 25, 2024
1 parent 0aac508 commit a709db0
Show file tree
Hide file tree
Showing 12 changed files with 797 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ module.exports = {
},
],
//#endregion //*======== Import Sort ===========

// #region //*========= Cypress files =========
'@typescript-eslint/no-namespace': [
'error',
{
allowDeclarations: true,
},
],
// #endregion //*========= Cypress files =========
},
globals: {
React: true,
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ This project is using [conventional commits](https://www.conventionalcommits.org

// TODO [cypress](https://www.cypress.io/)

- **Running Cypress**

- `pnpm run cypress:open`

## Deployment

// TODO [vercel](https://vercel.com/)
Expand Down
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: 'next',
bundler: 'webpack',
},
},
});
37 changes: 37 additions & 0 deletions cypress/component/Dropdown.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable simple-import-sort/imports */

import { Dropdown } from '@/app/ui/Dropdown';
import '@/app/globals.css';

describe('Dropdown.cy.tsx', () => {
const initialVal = 'Initial Val';
const options = ['Option 1', 'Options2'];

it('dropdown selects first option', () => {
const action = cy.spy().as('onDropdownChange');
cy.mount(<Dropdown value={initialVal} items={options} action={action} />);
cy.get('[data-cy="dropdown"]').click();
cy.get('[data-cy="dropdown-item"]').first().click();
cy.get('@onDropdownChange').should('have.been.calledWith', options[0]);
});

it('dropdown selects second option', () => {
const action = cy.spy().as('onDropdownChange');
cy.mount(<Dropdown value={initialVal} items={options} action={action} />);
cy.get('[data-cy="dropdown"]').click();
cy.get('[data-cy="dropdown-item"]').first().next().click();
cy.get('@onDropdownChange').should('have.been.calledWith', options[1]);
});

it('dropdown with no items and close dropdown', () => {
const action = cy.spy().as('onDropdownChange');
cy.mount(<Dropdown value={initialVal} items={[]} action={action} />);
cy.get('[data-cy="dropdown"]').click();
cy.get('[data-cy="dropdown-item"]').should('not.exist');
cy.get('@onDropdownChange').should('not.have.been.calledWith', options[0]);
cy.get('@onDropdownChange').should('not.have.been.calledWith', options[1]);

// Outside click
cy.get('body').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"
}
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>
// }
// }
// }
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
// ***********************************************************

/* eslint-disable simple-import-sort/imports */
// Import commands.js using ES2015 syntax:
import './commands';

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

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 />)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"release": "release-it",
"release-dry": "release-it --dry-run",
"start": "next start",
"typecheck": "tsc --pretty --noEmit --incremental false"
"typecheck": "tsc --pretty --noEmit --incremental false",
"cypress:open": "cypress open"
},
"dependencies": {
"axios": "latest",
Expand All @@ -41,6 +42,7 @@
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"autoprefixer": "latest",
"cypress": "latest",
"daisyui": "latest",
"eslint": "latest",
"eslint-config-next": "latest",
Expand Down
Loading

0 comments on commit a709db0

Please sign in to comment.